# Query\_string examples?

**URL:** <https://www.ruby-forum.com/t/query-string-examples/89225>\
**Category:** Rails\
**Created:** [March 18, 2007, 10:28pm UTC](https://www.ruby-forum.com/t/query-string-examples/89225 "2007-03-18T22:28:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dankitania](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e9a140/32.png) [@dankitania](https://www.ruby-forum.com/u/dankitania)\
**Post date:** [March 18, 2007, 10:28pm UTC](https://www.ruby-forum.com/t/query-string-examples/89225/1 "2007-03-18T22:28:56Z")

</div>

I’m listing something (people, say) in a column at the left of my page,  
and in a column to the right, I’d like to have some links that would let  
the site reader choose the listing order (by age, by name, etc.)

In PHP I’d do that by a query string, i.e. I’d keep using the same  
webpage, but my links would tack on strings such as

(pagelink)&orderby=age

etc. Is there a way to do this sort of thing in Rails? I note that the  
AWDWR2 index has only one item under “Q”, and this isn’t it! I also  
note that a search on this list doesn’t produce a lot of hits on  
“query\_string”. This is making me suspect that we’re supposed to do  
things differently in rails.

I’m hoping that someone advise me on this, before I start going down a  
path of not-best practice.

PS. I hope the answer isn’t that I should write a separate view method  
for each ordering scheme. That seems to violate the DRY principle,  
since the only thing that would be different in the methods be a single

:order

phrase in a single line of code

---

<div class="post-metadata">

**Author:** ![dankitania](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e9a140/32.png) [@dankitania](https://www.ruby-forum.com/u/dankitania)\
**Post date:** [March 19, 2007, 12:22am UTC](https://www.ruby-forum.com/t/query-string-examples/89225/2 "2007-03-19T00:22:32Z")

</div>

The parameters are stored in the params hash  
[http://localhost/people/show?order=name](http://localhost/people/show?order=name) will automatically initialize  
your  
params hash like this  
params={:controller=\>“people”,:action=\>“show”,:order=\>“name”}

In your view you would probably have \<%= link\_to(“Order by whatever”,  
:action=\>‘show’,:order=\>‘whatever’) %\>

When the user clicks that, in your controller params[:order] will be  
‘whatever’ and you can order by that

---

<div class="post-metadata">

**Author:** ![dankitania](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e9a140/32.png) [@dankitania](https://www.ruby-forum.com/u/dankitania)\
**Post date:** [March 19, 2007, 1:32am UTC](https://www.ruby-forum.com/t/query-string-examples/89225/3 "2007-03-19T01:32:02Z")

</div>

Bogdan I. wrote:

> The parameters are stored in the params hash

Bogdan, thanks for the tip. It works great! Dan.
