Click Table Headings to Sort

Hi, I’m looking at using my table headings as links that reload the page
and sort it by the field which was clicked on. As is common, if the
list is already sorted by the field, it reverses the order. I’m now
looking at a good DRY way to do this.

I have 3 fields: name, date, race. I’m setting a parameter called so
(for sort order) to either “up” or “down” when the table header link is
clicked. Right now, my working code (in a partial) looks something like
this -

    <% if params[:v_order] == 'down' %>
    <%= link_to "e_date", :action =>"list_by_edate", :v_order =>

“up” %>
<% else %>
<%= link_to “e_date”, :action =>“list_by_edate”, :v_order =>
“down” %>
<% end %>

the controller has the correct code for sorting either by ‘edate’ or
‘edate DESC’

Right now, I need to check which field the page is sorted by and then
use the above code for each of the three fields. I can either create 3
partials (one for each page - or just leave the above code inside the
view for the respective actions) OR look for a way to combine the 3 into
a single partial.

Any suggestions which would be better?
Thanks
Mohit.

Mohit S. wrote:

Hi, I’m looking at using my table headings as links that reload the page
and sort it by the field which was clicked on. As is common, if the
list is already sorted by the field, it reverses the order. I’m now
looking at a good DRY way to do this.

Will you please help me in the same code. Can you suggest me where you
declared or initialized params[:v_order] because it’s not working fine.
It is always taking a single value so no change is there.

or if you have got another DRY way just help me

Thanks

    <% if params[:v_order] == 'down' %>
    <%= link_to "e_date", :action =>"list_by_edate", :v_order =>

“up” %>
<% else %>
<%= link_to “e_date”, :action =>“list_by_edate”, :v_order =>
“down” %>
<% end %>

i think you’ll definitely not want to have three partials to display one
and the same stuff only in different order. i would do it by handing in
another parameter with the column name clicked.

first make your “if then” a oneliner:

<%= link_to “e_date”, :action =>“list_by_edate”, :v_order =>
params[:v_order] == ‘down’ ? “up” : “down” %>

then add a fieldname:

<%= link_to “e_date”, :action =>“list_by_edate”, :v_order =>
params[:v_order] == ‘down’ ? “up” : “down”, :v_col => “edate” %>

Thorsten M. wrote:

<%= link_to “e_date”, :action =>“list_by_edate”, :v_order =>
params[:v_order] == ‘down’ ? “up” : “down” %>

then add a fieldname:

<%= link_to “e_date”, :action =>“list_by_edate”, :v_order =>
params[:v_order] == ‘down’ ? “up” : “down”, :v_col => “edate” %>

Thanks, I hope this will work. Will you please help me a little bit
more. Where params[:v_order] is defined or initialized? Means how this
will be global to all? Will you please give me some code example of
controller too and if you have defined or initialized params[:v_order]
somewhere else then please that code too.
I think according to my implemented code it is always taking same value
of params, do we need to initialize some value or what?

thanks

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs