views:

30

answers:

2

I can't seem to grasp the Rails routing just yet. I want to be able to link the site to, for example, ...com/store/xbox360/Mass Effect 2 (which is /store(my controller)/:system/:title).

I have the database entries that include the :system and :title variables (?). How would I route these to show up and filter these entries? Or is this something I need to set up in the controller? I'm lost. Thanks so much for your help.

A: 

put following in your routes.rb & don't forget to restart server

map.stores 'stores/:system/:title/:action/:id', :controller => 'stores'

then following ur;

"com/store/xbox360/Mass" will call your index method of stores controller.

where, params[:system] = "xbox360"
params[:Mass] = "Mass"

Salil
+1  A: 

Hey there,

If you're following the RESTFUL resource conventions you might want use:

map.resources :system, :has_many => :title, :path_prefix => '/store/'

This would generate handy little routes that could prove very helpful:

system_title_index GET    /store/system/:system_id/title(.:format)          {:controller=>"title", :action=>"index"}
                   POST   /store/system/:system_id/title(.:format)          {:controller=>"title", :action=>"create"}
  new_system_title GET    /store/system/:system_id/title/new(.:format)      {:controller=>"title", :action=>"new"}
 edit_system_title GET    /store/system/:system_id/title/:id/edit(.:format) {:controller=>"title", :action=>"edit"}
      system_title GET    /store/system/:system_id/title/:id(.:format)      {:controller=>"title", :action=>"show"}
                   PUT    /store/system/:system_id/title/:id(.:format)      {:controller=>"title", :action=>"update"}
                   DELETE /store/system/:system_id/title/:id(.:format)      {:controller=>"title", :action=>"destroy"}
      system_index GET    /store/system(.:format)                           {:controller=>"system", :action=>"index"}
                   POST   /store/system(.:format)                           {:controller=>"system", :action=>"create"}
        new_system GET    /store/system/new(.:format)                       {:controller=>"system", :action=>"new"}
       edit_system GET    /store/system/:id/edit(.:format)                  {:controller=>"system", :action=>"edit"}
            system GET    /store/system/:id(.:format)                       {:controller=>"system", :action=>"show"}
                   PUT    /store/system/:id(.:format)                       {:controller=>"system", :action=>"update"}
                   DELETE /store/system/:id(.:format)                       {:controller=>"system", :action=>"destroy"}
                          /:controller/:action/:id  

Your URL will look slightly different though:/store/system/xbox360/title/Mass%20Effect

Remember to override the to_param method in your system and title models so that the id would be the actual names of the object instead of just numbers.

Hope this helps!

jonasespelita