order by variable field name

What i have                            <th>Age</th>                            <th>name</th>                            <th>Sex</th> what i did                              <th><%= link_to 'Age',:order_by => 'age', :action => 'index'%></th>                              <th><%= link_to 'name',:order_by => 'name', :action => 'index'%></th>                               <th><%= link_to 'Sex',:order_by => 'Sex', :action => 'index'%></th> What i need                        when user Click On Age or name or sex records should be in that order as user clicked.if user clicked on age Twice Then record should reverse . that would be bonus . By default it Should Be As working now.

What i have

What i need

                   when user Click On Age or name or sex records

should be in that order as user clicked.if user clicked on age Twice

Then record should reverse . that would be bonus .

By default it Should Be As working now.

OK, so given that this isn’t a free-coding forum or a homework-for-you forum, what do you need? Hints on how to do this? Do you have code you’ve tried that isn’t working?

I’ll start you with hints and you can then try it yourself…

  1. The :order_by in your link_to will come through in the params hash in your controller as params[:order_by]

  2. You can use :order as an option to ActiveRecord find methods (along with “ASC” or “DESC” for ordering)

  3. You’ll need to remember the current order if you want to toggle it on clicks (or have it as another parameter in your link_to), you can use the session[:whatever_you_want] = … construct for that.

People are always happy to help, but you can’t just say “I need…”

Cheers,

Andy

Thanks Andy

I did this A Lot.

:order => “#{params[:order_by]} = 1”

there is a Write Query Running in my logger But Nothing happens .i thinks there must be a previous Code Which making my code Disable.Leave I have to consult my Senior . any Ways thanks Again For the advice Which are More important for me About Manners Thanks A lot.

I did this A Lot.

:order => “#{params[:order_by]} = 1”

This should probably be something more like:

session[:order_direction] ||= “ASC”

:order => “#{params[:order_by]} #{session[:order_direction]}”

there is a Write Query Running in my logger But Nothing happens .i thinks there must be a previous Code Which making my code Disable.

OK

Leave I have to consult my Senior .

any Ways thanks Again For the advice Which are More important for me About Manners Thanks A lot.

I assume this is sarcasm, but it’s really hard to tell.

Anyway, as I said I’m happy to help, but I need to know where you’re at/what the problem is (not just the requirement) and also need to feel valued that you’re not just using me/others here for free coding.

Cheers,

Andy