need help sorting results using select_tag

I have a page that returns data and I need to let the user sort it. I have this:

<% form_tag courses_path, :method => :get do -%>   <%= select_tag :order, options_for_select(%w(date rating author)) %>   <%= submit_tag "Order", :name => nil %> <% end -%>

However, this erases any existing params in the url. For example, if the url is http://localhost:3000/pictures?tag=vacation then the form above will get http://localhost:3000/pictures?order=rating when I really want http://localhost:3000/pictures?tag=vacation&order=rating

I tried this, but it doesn't work.

<% form_tag :url => {:overwrite_params => :order, :method => :get} do - %>   <%= select_tag :order, options_for_select(%w(date rating author)) %>   <%= submit_tag "Order", :name => nil %> <% end -%>

This doesn't work either:

<%= select_tag :order, options_for_select(%w(date rating author)),                :onchange => remote_function( :url => {:overwrite_params => { :order => "order.value" }},                :method => :get ) %>

Can someone help? I've spent way too much time on this :slight_smile:

One quick and easy way to preserve your params would be to create a hidden field in the form which takes its name and value from the param that you'd like to preserve.

<% form_tag courses_path, :method => :get do -%>          <%= select_tag :order, options_for_select(%w(date rating author)) %>          <%= hidden_field_tag :tag, params[:tag] %>          <%= submit_tag "Order", :name => nil %> <% end -%>

Hope that helps,

kb

if possible, I'd like to use this as a last resort - I showed the tag param as an example, but I actually have a lot of params and it would be painful to create hidden fields for all of them.

I'm surprised at how little I was able to find on this topic since I've seen so many websites that let users sort results using a simple drop down. Am I approaching this in a completely wrong way?

thanks.

In that case, you might want to write a custom helper, something like

courses_path_with_params

which will append the appropriate params to courses_path.

I'd be equally interested to know if there's a better way.

ok. I think I'm getting close :slight_smile:

<%= select_tag :order, options_for_select(%w(date rating author)),              { :onchange => remote_function( :url => request.request_uri,                                              :method => :get,                                              :with => 'Form.Element.serialize(this)') } %>

the "problem" with this is that now the order param gets repeated, for example http://site.com?tags=vacation&order=date&order=rating

anyone know how to remove a param from a query string?

Perhaps you could somehow get :overwrite_params in there...

-Kyle