there is a situation where a have multiple controllers like a town_center controller, a sawmill controller, a quarry controller and so on.
The urls of my application would probably need to be something like /town_center/view or sawmill/view, quarry/view and the likes.
The problem is how to actually write such routes. For instance, for the town_center controller i have something like :
scope :path => '/town_center', :controller => :town_center do
get '/view/' => :view, :as => 'town_center'
end
While this works fine, there is a problem. When i actually execute the view action in the town_center controller, there is no simple way to get the id of the building, since it’s not present in the url as a params attribute.
One simple idea would be to create a route like :
get '/view/:name' => :view, :as => 'view_building'
But the problem with that approach is that everything gets redirected to a single controller. So, my question is, how can i make it so that there is an identifier on the url and still redirect to particular controllers ?
Or if you think this is not correct as a general idea, is there a better way to doing that ?
You could use RESTful routing, which has become the norm for Rails applications.
Edit Or for singular routes, as suggested in the comments:
This will automatically give you all of the routes needed for Create, Read, Update, and Destroy your various models.
Running
rake routeswill show you all of the routes that your application makes available. This will include GET routes of the formatGET /town_centersandGET /town_centers/:id, the latter of which will run a show action in your TownCentersController and render your /views/town_centers/show.html.erb view template.If you’re new to Rails and have not yet read the Getting Started or Routing guides, they can be found here:
Getting Started with Rails: http://guides.rubyonrails.org/getting_started.html
Rails Routing: http://guides.rubyonrails.org/routing.html