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.
... what about <% %w(name date race).each do |field| %> <% value = params[:v_order] == ''down''? ''up'' : ''down'' %> <%= link_to "e_#{field}", :action => "list_by_e#{field}", :v_order => value %> <% end %> 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. __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com
Hi! Thanks for your reply. Other comment inline... B Gates wrote:> ... > what about <% %w(name date race).each do |field| %> > <% value = params[:v_order] == ''down''? ''up'' > : ''down'' %> > <%= link_to "e_#{field}", :action => > "list_by_e#{field}", :v_order => value %> > <% end %> > >The only problem with this approach is that it creates all the links as either ''up'' or ''down'' since it does not consider which page it is on. But, you got me started in the right direction, so I included a couple of checks for it and came up with this: <tr> <% %w(name date race).each do |field| %> <th> <% value = params[:v_order] == ''up''? ''down'':''up'' %> <% if params[:by] == field %> <%= link_to "#{field.titleize}", :action => "list_by_#{field}", :v_order => value, :by => field %> <% else %> <%= link_to "#{field.titleize}", :action => "list_by_#{field}", :v_order => "up", :by => field %> <% end %> </th> <% end %> </tr> Thanks for your help! Cheers Mohit.> 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. > > > > > __________________________________________________ > Do You Yahoo!? > Tired of spam? Yahoo! Mail has the best spam protection around > http://mail.yahoo.com > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
Mohit Sindhwani 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> <% 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" %> -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thorsten Mueller 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---