I’m currently reading “Agile Web D. with Rails (2nd Edition)”
and I’ve run into something I don’t quite understand.
In one section of the book they have link in the one of the views that
looks like this:
<%= link_to ‘Show’, :action => ‘show’, id: => product %>
This produces a link similar to the following:
I understand that product is the controller, show is the action and 1
is the product id.
Then in the show action of the product controller you would use
something like this to access the products id:
product = Product.find(params[:id])
What I don’t understand is how Rails is mapping 1 in the url I gave
above to params[:id].
What if I had something a little more complicated like categories for
products. Products might be able to fit into many different categories
so I have a controller called ProductCategories. I have a list action
which will list all the products for a given category.
A link like this may be
<%= link_to “category.name”, :action => ‘list’, :id => category %>
which I think will produce the following URL:
That’s straight forward. But what if a list of products in a category
is long and I want to be able to sort the order by say price
(ascending or decending) or name (ascending or descending).
Could I have links like the following:
<%= link_to ‘By Price’, :action => ‘list’, id: => category, :sort_by
=> ‘price’, :sort_direction => ‘asec’ %>
That the above link look like this:
and if so how is Rails mapping the URL to params?
I hope that make sense.