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.

Thanks,

Jeremiah

Last edited by jmesserer (2010-06-22 10:34:23)

Re: Passing additional variables from view to controller for layout.

jmesserer wrote:

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

I think your post got truncated.  This seems to have been happening a lot lately, i wonder if the latest changes to the forum are breaking some people's posts.

###########################################
#If i've helped you then please recommend me at Working With Rails:
#http://www.workingwithrails.com/person/ … i-williams

Re: Passing additional variables from view to controller for layout.

I updated my original post. I have noticed the last few days when I post it truncates it.

Re: Passing additional variables from view to controller for layout.

Pass through a parameter called 'order', which is set to the value of whatever you want to put into the :order option in the find call, which in turn is whatever would follow 'order by' in an sql query.

As ever in this sort of situation, start with the controller and then tailor the view to the controller.  I'm using the example model name of cars since i don't know what your model is called.

#controller code
@cars = Car.find(:all, :order => (params[:order] || "name"))

Now you just need to make sure the view sends through a value for params[:order].  There's a lot of different ways of doing this.  A simple way is to make your 're-order' links to all just be links to the cars index page, and set the order parameter differently in each.  In this example i also assume you have a little up and down arrow graphic in images called 'up.png' and 'down.png'.  This is what the user will actually click on.

For the desc links, you usually want to show the asc (default) version in every situation except where params[:order] is currently equal to that field.  ie, when you sorted by that field last time.  eg

<% if params[:order] == "make" %>
  <%= link_to image_tag("down.png"), cars_path(:order => "make desc") %>
<% else %>
  <%= link_to image_tag("up.png"), cars_path(:order => "make") %>
<% end %>

###########################################
#If i've helped you then please recommend me at Working With Rails:
#http://www.workingwithrails.com/person/ … i-williams

Re: Passing additional variables from view to controller for layout.

Max,

That is exactly what I am doing :-).

To make 'order' available through the RESTful routes, I need to add that to the routes, correct?

Something like:
map.resources :cars (:order)

Re: Passing additional variables from view to controller for layout.

No, you don't have to mention it in the routes unless you want it to actually be part of the path, as opposed to just the params.  The links i did will generate urls like this

/cars?order=make

this

/cars

is the path, which is what routes gets programmed to recognise.  Any params that are just added in the usual way (ie after the ?) don't need to be described, they're just standard.

Last edited by Max Williams (2010-06-22 11:43:37)

###########################################
#If i've helped you then please recommend me at Working With Rails:
#http://www.workingwithrails.com/person/ … i-williams

Re: Passing additional variables from view to controller for layout.

Great! Thanks.