Topic: Passing additional variables from view to controller for layout.
In the view on for action index I have a table. The table can be sorted via the columns (ascending/descending). Say I have columns "date", "title", "age" that can be sorted. I was curious how I would pass variables like "column" ("date", "title", or "age") and "sort" ("asc" or "desc") to the controller to redisplay the same action and view but with the data reordered.
Since I have to do this on the index action and no params[:id] variable is needed, I am just passing something like "title-asc" through to the controller from the view in the :id variable place ...
<%= link_to "Title Ascending", :id => "title-asc" %>
and then in the controller under the 'index' action I am splitting the variables ...
(@column, @order) = params[:id].split('-')
@column ||= "title"
@order ||= "asc"
and then using these variables in my AR queries ...
Is there a better way to do this? I don't feel like this is a correct solution even though it works.
Last edited by jmesserer (2010-06-22 10:34:23)