Passing parameter to action

i have a table of data in my application. i would like to be able to
sort the data in the table by clicking on the column header. the way
i was thinking i want to do this is just by making a ‘sort’ action,
and then calling the sort action from each link in the table header
(passing the respective column name). it doesn’t seem to be possible
to pass parameters to an action though. do i just have to use forms
for this, or how could it be done?

thanks,
cam

I think that any additional params you add to your link_to function call
that are not specifically expected in the function call (like :id => x)
will just be passed through in the GET query string. For instance…

link_to(:controller => ‘controller’, :action => ‘sort’, :sort_order =>
‘date’)
link_to(:controller => ‘controller’, :action => ‘sort’, :sort_order =>
‘firstname’)
link_to(:controller => ‘controller’, :action => ‘sort’, :sort_order =>
‘lastname’)

should produce links along the lines of:

http://yoursite.dom/controller/sort?sort_order=date
http://yoursite.dom/controller/sort?sort_order=firstname
http://yoursite.dom/controller/sort?sort_order=lastname

In your controller, params[:sort_order] will have a value of ‘date’,
‘firstname’, or ‘lastname’ depending on the link clicked. You can check
it and act appropriately with your Object.find call. If you really want
to DRY it up, the value of sort_order used in the links and passed in
can be the name of the field to sort by, so you could just use that in
your Object.find call.

c.

Cameron M. wrote:

i have a table of data in my application. i would like to be able to
sort the data in the table by clicking on the column header. the way
i was thinking i want to do this is just by making a ‘sort’ action,
and then calling the sort action from each link in the table header
(passing the respective column name). it doesn’t seem to be possible
to pass parameters to an action though. do i just have to use forms
for this, or how could it be done?

thanks,
cam

Thanks! I’ve been wondering how to use arbitrary GET parameters for a
while now.
Cam

http://wiki.rubyonrails.org/rails/pages/SortHelper

might help ya :slight_smile:

Cameron M. wrote:

i have a table of data in my application. i would like to be able to
sort the data in the table by clicking on the column header. the way
i was thinking i want to do this is just by making a ‘sort’ action,
and then calling the sort action from each link in the table header
(passing the respective column name). it doesn’t seem to be possible
to pass parameters to an action though. do i just have to use forms
for this, or how could it be done?

thanks,
cam