Spyros
Spyros

Reputation: 48676

How to handle this routing situation is Rails

i prefer writing the routes of my Rails applications by hand and i now have a situation where i am not sure what is the best way to do things. I want to have a building controller that shows a different page for every building like :

building/town_center
building/sawmill
..

Each of the above should have its own action and view page. I would normally create a route like:

  scope :path => '/building', :controller => :building do
    get '/view/:slug' => :view, :as => 'view_building'
  end  

But this only specifies a single action that would then need to call another internal controller method to redirect to the needed template to show. So, i would like your opinion, would you just specify a different route for every building(and action) explicitly ? Or just redirect in the view_building action ?

Upvotes: 1

Views: 39

Answers (1)

Andrew Cetinic
Andrew Cetinic

Reputation: 2835

I think you are after something like this:

  match "/building/:name", :to => "buildings#show", :as => :building

Then in your controller action 'show' just render template for the building name:

  render :template => 'buildings/#{params[:name]}'

Upvotes: 1

Related Questions