Nested URLs, controllers and views in Ruby on Rail

2019-08-19 22:14发布

问题:

What I am trying to do is to nest my pages in Rails for example:

www.mysite.com/fifa17/ps4

This way, once I am on the show.html.erb, I can use use the route to sell my product for specifically that game on that console platform..

At the moment, I have a Console model with has_many :games and a Game model with has_many :consoles, but I can't get the routing and views to work. Do I have just one controller or two?

I have both the Games and Consoles seeded under ActiveRecords by name. I seem to be able to call one of the actions (i.e. Console.name) but cannot call both the console name and Game name.

回答1:

A great place to read on this would be in Ruby on Rails Associations Guide and Routing Guide.

What you want to do, is to update your Console and Game associations, to use has_and_belongs_to_many. You can read more on that here

And double check your routing, check this StackOverflow Question for a similar case.