Ahoy, I checked the API and did some forum searches but didn''t find anything good on table sorting. Is there a "railish" way to do this? My current method really is lacking (i''m on like rails day 2) <th><%= link_to ''Name'', :action => ''list'', :order => ''name'' %></th> <th><%= link_to ''Note'', :action => ''list'', :order => ''note'' %></th> <th><%= link_to ''Created on'', :action => ''list'', :order => ''created_on'' %></th> <th><%= link_to ''Category'', :action => ''list'', :order => ''category_id'' %></th> This doesn''t allow switching between ASC and DESC either. plus I feel that it could be automagic to some degree? -- Posted via http://www.ruby-forum.com/.
Oh, forgot this part: def list @item_pages, @items = paginate(:items, :per_page => 10, :order_by => (params[:order])) end -- Posted via http://www.ruby-forum.com/.
Will Jessup wrote:> Oh, forgot this part: > def list > @item_pages, @items = paginate(:items, > :per_page => 10, > :order_by => (params[:order])) > endDoes it hold the order when you press ''next page''? Problem I''ve got is that I can order the table page that is displayed, but as soon as I press ''next page'' the order is not sorted. I am looking for the ''rails way'' to do this. -- Posted via http://www.ruby-forum.com/.
Jeff Gordon wrote:> Will Jessup wrote: >> Oh, forgot this part: >> def list >> @item_pages, @items = paginate(:items, >> :per_page => 10, >> :order_by => (params[:order])) >> end > > Does it hold the order when you press ''next page''? Problem I''ve got is > that I can order the table page that is displayed, but as soon as I > press ''next page'' the order is not sorted. I am looking for the ''rails > way'' to do this.I havn''t even gotten that far =] -- Posted via http://www.ruby-forum.com/.
Ok this is just my rails day 3 but in my PHP pages I stored the sort order in the session for every table... You would have something like session[:your_table_order] = params[:order] || your_default_order and in paginate, you''ll have to :order_by => session[:your_table_order] You get the order from a GET or POST parameter, the table will only be sorted if this parameter is present. If you look at the links for "next page" and "previous page" you will notice that they don''t send this order parameter --> The next page won''t be ordered. I''m quite sure there is a way to do this without sessions... Please correct me, if I''m wrong. -- Posted via http://www.ruby-forum.com/.
Is this secure, or should params[:order] be validated against acceptable values first? Will Jessup wrote:> Oh, forgot this part: > def list > @item_pages, @items = paginate(:items, > :per_page => 10, > :order_by => (params[:order])) > end-- Posted via http://www.ruby-forum.com/.
I dont see why it would be insecure. If someone happened to type a non existing column name in there they would (should) get an error back. I don''t think there is much you can do to screw up the process in the order clause. And this is also the way I do it as well. I also preform one more check on the parameters to see it is already in the session, if it is I switch it between ASC or DESC depending on which is already set. My code for this is a little messy right now, but I could most certainly go back and clean it up if I''m ever that area again. -Nick On 4/16/06, Steve Koppelman <hatlessnyc@yahoo.com> wrote:> > Is this secure, or should params[:order] be validated against acceptable > values first? > > Will Jessup wrote: > > Oh, forgot this part: > > def list > > @item_pages, @items = paginate(:items, > > :per_page => 10, > > :order_by => (params[:order])) > > end > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060416/fb9ce3b2/attachment.html
There is a SortHelper that you may use as a starting point http://wiki.rubyonrails.com/rails/pages/Sort+Helper On 4/16/06, Will Jessup <will@willjessup.com> wrote:> > Ahoy, > > > I checked the API and did some forum searches but didn''t find anything > good on table sorting. > > Is there a "railish" way to do this? My current method really is lacking > (i''m on like rails day 2) > > <th><%= link_to ''Name'', :action => ''list'', :order => ''name'' %></th> > <th><%= link_to ''Note'', :action => ''list'', :order => ''note'' %></th> > <th><%= link_to ''Created on'', :action => ''list'', :order => > ''created_on'' %></th> > <th><%= link_to ''Category'', :action => ''list'', :order => > ''category_id'' %></th> > > This doesn''t allow switching between ASC and DESC either. plus I feel > that it could be automagic to some degree? > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060416/fad96536/attachment.html