Reputation: 1758
If I have a model Product and a model Category.
I have a table in products index showing products with columns like:
<td><%= product.ID %></td>
<td><%= product.NAME %></td>
<td><%= product.category.NAME %></td>
It shows values like:
1,salad, vegetable
2,apple,fruit
I want to be able to click on vegetable or fruit to edit them.
I tried:
<td><%= link_to product.category.NAME, [:edit, product.category] %>
This takes me to a page
categories/1/edit
which returns
Couldn't find Product with 'ID'=
Instead it should go to
categories/edit/1
In my routes I have:
match ':controller(/:action(/:ID))', :via => [:get, :post]
resources :categories
What is the correct syntax to use in this case?
Upvotes: 1
Views: 318
Reputation: 3610
To invoke the edit action on the category controller with your modified route specify the controller, action and id values like so:
<%= link_to product.category.NAME, { controller: :categories, action: :edit, ID: product.category.id } %>
The link_to helper will build the path correctly to match the category/edit/1
path and route to the edit action of the category controller instead of the product controller (assuming category.id == 1)
Upvotes: 1