I don’t understand the necessity for nested routes. There are many things about Rails routes that I DO understand.
Here is my simple setup in this Rails 3.2 application:
Categories habtm Products
In routes.rb:
resources :categories do
member do
get 'list_products_in'
post 'add_to'
post 'remove_from'
end
end
resources :products
I have basically created a page from which to add products to a category. There is a select box at the top containing the products, and at the bottom is the list. To add a product to the list, you choose one from the select box and click the “Add” button. “add_to” is called via Ajax. Works great.
My problem is that to make this app degrade gracefully, I want it to work without Ajax also. But none of my routes work properly.
I know from the above that my custom routes are not expecting a product_id to be passed in addition to the category id. My question is, how can I add more parameters to my custom routes?
You can use
:variablesegments in your routes to supply additional variable segments.For example, you could add a
product_idsegment to youradd_toandremove_fromroutes: