I''m playing with a simple rails app to store video information. I wanted to implement a listing with column headers, where you can click on the column headers to change the sort order (this version actually remembers the click sequence, and sorts the columns in reverse order of column clicks; it also reverses the sorting of a column if it is clicked on twice). I would like to generalize this so it''s easy to re-use for other tables. The view and application_helper parts are reasonably OK. The controller is rather yuchy. Can anyone suggest ways to generalize the controller code? The relevant code is attached. Thanks, Bob _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Minor bug: @default_sort = "title:genre:rating:format:owner:-year" should have been: @default_sort = "title:genre:rating:format:owner:year" Bob On Wed, 19 Jan 2005 01:13:52 -0800, Bob Sidebotham <bob.sidebotham-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m playing with a simple rails app to store video information. I > wanted to implement a listing with column headers, where you can click > on the column headers to change the sort order (this version actually > remembers the click sequence, and sorts the columns in reverse order > of column clicks; it also reverses the sorting of a column if it is > clicked on twice). > > I would like to generalize this so it''s easy to re-use for other > tables. The view and application_helper parts are reasonably OK. The > controller is rather yuchy. Can anyone suggest ways to generalize the > controller code? > > The relevant code is attached. > > Thanks, > Bob > > >
Am Mittwoch, 19. Januar 2005 10.13 schrieb Bob Sidebotham:> > I would like to generalize this so it''s easy to re-use for other > tables. The view and application_helper parts are reasonably OK. The > controller is rather yuchy. Can anyone suggest ways to generalize the > controller code?My solution isn''t perfect either. Controller: sort, order = @session["order_by"] if @params[''sort''] then # just changed sort = @params[''sort''] order = @params[''order''] end unless sort.nil? then @session["order_by"] = [sort, order] end @order_by = [sort, order] orderings = sort + '' '' + order unless sort.nil? Project.find_all(nil, orderings) Helper: def order_by(displayname, key, orderby) sort, order = orderby orderswitch = ''asc'' if key == sort and order.downcase == ''asc'' then orderswitch = ''desc'' end url = ''<a href="?sort='' url << key url << ''&order='' << orderswitch url << ''">'' << displayname << ''</a>'' if key == sort then url << '' <img src="/images/'' << order << ''_order.png"'' url << '' border="0" width="7" height="7" alt="'' << order << ''" />'' end url end View: <th><%= order_by "Herausgeber", "publisher", @order_by %></th> Pirmin -- Pirmin Kalberer Sourcepole - Linux & Open Source Solutions http://www.sourcepole.com