# Need help sorting results using select\_tag

**URL:** <https://www.ruby-forum.com/t/need-help-sorting-results-using-select-tag/135938>\
**Category:** Rails\
**Created:** [April 26, 2008, 5:30pm UTC](https://www.ruby-forum.com/t/need-help-sorting-results-using-select-tag/135938 "2008-04-26T17:30:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [April 26, 2008, 5:30pm UTC](https://www.ruby-forum.com/t/need-help-sorting-results-using-select-tag/135938/1 "2008-04-26T17:30:40Z")

</div>

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](http://localhost:3000/pictures?tag=vacation) then the form  
above will get [http://localhost:3000/pictures?order=rating](http://localhost:3000/pictures?order=rating) when I  
really want [http://localhost:3000/pictures?tag=vacation&order=rating](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 🙂

---

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [April 26, 2008, 5:44pm UTC](https://www.ruby-forum.com/t/need-help-sorting-results-using-select-tag/135938/2 "2008-04-26T17:44:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [April 26, 2008, 5:51pm UTC](https://www.ruby-forum.com/t/need-help-sorting-results-using-select-tag/135938/3 "2008-04-26T17:51:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [April 26, 2008, 6:20pm UTC](https://www.ruby-forum.com/t/need-help-sorting-results-using-select-tag/135938/4 "2008-04-26T18:20:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [April 28, 2008, 5:02am UTC](https://www.ruby-forum.com/t/need-help-sorting-results-using-select-tag/135938/5 "2008-04-28T05:02:28Z")

</div>

Perhaps you could somehow get :overwrite\_params in there…

-Kyle

---

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [April 27, 2008, 9:30am UTC](https://www.ruby-forum.com/t/need-help-sorting-results-using-select-tag/135938/6 "2008-04-27T09:30:36Z")

</div>

ok. I think I’m getting close 🙂

\<%= 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](http://site.com?tags=vacation&order=date&order=rating)

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