Query_string examples?


#1

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


#2

The parameters are stored in the params hash
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


#3

Bogdan I. wrote:

The parameters are stored in the params hash

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