Hi there everyone, I''ve recently moved to RoR and am still struggling with the basics of what used to be second nature in php, so on with my question: I have a database with products, and I need to create a search form that paginates the results. What I have so far in my controller is: def list # If the values are nil, then set them, otherwise wrap them in % # Also, set up our view instance variable, to re-populate the search form @view = Machine.new @view.make = getParam("make") @view.model = getParam("model") # Search the database and populate our paging object @filtered_machines = Machine.find(:all, :conditions => ["make LIKE ? AND model LIKE ? AND for_sale = 1 AND web_width != 0", sqlWildcard(@view.make), sqlWildcard(@view.model)]) @machine_pages, @machines = paginate_collection(@filtered_machines) end def getParam(name, default="") params.fetch("view", {}).fetch(name, default) end def sqlWildcard(value) value.blank? ? ''%'' : "%#{value}%" end Now, the list page is a full listing, and it has a filter box at the top that people can type in and hit Filter and it searches ad returns a cut down version. But what I have in my view paginates the results, but the links to other pages drop the search. I''ve probably not done this the right way, I''m open to suggestions on best practises etc. Here''s my view: <%= render :partial => ''search'' %><br /> <%= link_to ''Previous page'', { :page => @machine_pages.current.previous } if @machine_pages.current.previous %> <%= link_to ''Next page'', { :page => @machine_pages.current.next, :make => @view.make, :model => @view.model } if @machine_pages.current.next %><br /> <%= render :partial => "machine", :collection => @machines %> Everything works apart from the pagination. But again, this feels a little messy, so please feel free to rip apart the way I''ve done it. Thanks, Jamie van Dyke Fear of Fish
On Thu, 2006-02-23 at 15:31 +0000, Jamie van Dyke wrote:> Hi there everyone, I''ve recently moved to RoR and am still struggling > with the basics of what used to be second nature in php, so on with > my question: > > I have a database with products, and I need to create a search form > that paginates the results. What I have so far in my controller is: > > def list > # If the values are nil, then set them, otherwise wrap them in % > # Also, set up our view instance variable, to re-populate the > search form > @view = Machine.new > > @view.make = getParam("make") > @view.model = getParam("model") > > # Search the database and populate our paging object > @filtered_machines = Machine.find(:all, :conditions => ["make > LIKE ? AND model LIKE ? AND for_sale = 1 AND web_width != 0", > sqlWildcard(@view.make), > sqlWildcard(@view.model)]) > @machine_pages, @machines = paginate_collection(@filtered_machines) > end > > def getParam(name, default="") > params.fetch("view", {}).fetch(name, default) > end > > def sqlWildcard(value) > value.blank? ? ''%'' : "%#{value}%" > end > > Now, the list page is a full listing, and it has a filter box at the > top that people can type in and hit Filter and it searches ad returns > a cut down version. But what I have in my view paginates the > results, but the links to other pages drop the search. I''ve probably > not done this the right way, I''m open to suggestions on best > practises etc. Here''s my view: > > <%= render :partial => ''search'' %><br /> > <%= link_to ''Previous page'', { :page => > @machine_pages.current.previous } if @machine_pages.current.previous %> > <%= link_to ''Next page'', { :page => > @machine_pages.current.next, :make => @view.make, :model => > @view.model } if @machine_pages.current.next %><br /> > <%= render :partial => "machine", :collection => @machines %> > > Everything works apart from the pagination. But again, this feels a > little messy, so please feel free to rip apart the way I''ve done it.---- use the methodology just like a scaffold would create... @filtered_machines_pages = paginate(: :filtered_machines, :conditions => ["make LIKE ? AND model LIKE ? AND for_sale = 1 AND web_width != 0", sqlWildcard(@view.make), sqlWildcard(@view.model)]) Something like that should work Craig
My problem is not that paging isn''t working, sorry, but that the links aren''t working. If I click "Next Page" it reverts back to the entire database rather than the filtered results. My links are done like so: <%= link_to ''Previous page'', { :page => @machine_pages.current.previous } if @machine_pages.current.previous %> <%= link_to ''Next page'', { :page => @machine_pages.current.next, :make => @view.make, :model => @view.model } if @machine_pages.current.next %> Thanks, Jamie van Dyke Fear of Fish On 23 Feb 2006, at 15:41, Craig White wrote:> @filtered_machines_pages = paginate(: > :filtered_machines, > :conditions => ["make LIKE ? AND model LIKE ? AND for_sale = 1 AND > web_width != 0", sqlWildcard(@view.make), sqlWildcard(@view.model)])
On Thu, 2006-02-23 at 16:12 +0000, Jamie van Dyke wrote:> My problem is not that paging isn''t working, sorry, but that the > links aren''t working. If I click "Next Page" it reverts back to the > entire database rather than the filtered results. My links are done > like so: > > <%= link_to ''Previous page'', { :page => > @machine_pages.current.previous } if @machine_pages.current.previous %> > <%= link_to ''Next page'', { :page => > @machine_pages.current.next, :make => @view.make, :model => > @view.model } if @machine_pages.current.next %> >---- then you have to either store the ''filters'' that you used in a session variable and use that to do the paging or pass the params[:variable] with each link_to Craig
That''s what I was hoping to avoid. Thanks for your help, mate. Thanks, Jamie van Dyke Fear of Fish On 23 Feb 2006, at 16:48, Craig White wrote:> On Thu, 2006-02-23 at 16:12 +0000, Jamie van Dyke wrote: >> My problem is not that paging isn''t working, sorry, but that the >> links aren''t working. If I click "Next Page" it reverts back to the >> entire database rather than the filtered results. My links are done >> like so: >> >> <%= link_to ''Previous page'', { :page => >> @machine_pages.current.previous } if >> @machine_pages.current.previous %> >> <%= link_to ''Next page'', { :page => >> @machine_pages.current.next, :make => @view.make, :model => >> @view.model } if @machine_pages.current.next %> >> > ---- > then you have to either store the ''filters'' that you used in a session > variable and use that to do the paging or pass the params[:variable] > with each link_to > > Craig > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
> My problem is not that paging isn''t working, sorry, but that the > links aren''t working. If I click "Next Page" it reverts back to the > entire database rather than the filtered results. My links are done > like so: > > <%= link_to ''Previous page'', { :page => > @machine_pages.current.previous } if @machine_pages.current.previous %> > <%= link_to ''Next page'', { :page => > @machine_pages.current.next, :make => @view.make, :model => > @view.model } if @machine_pages.current.next %>I have used: pagination_links @FOO_pages, { :params => {''filter'' => @params[:filter]} } to append my current filter string as a GET parameter on the page links.
See: http://wiki.rubyonrails.com/rails/pages/HowtoPagination Specifically: <%= link_to(''previous page'', {:params => params.merge(''page'' => @item_pages.current.previous)}) if @item_pages.current.previous %> <%= link_to(''next page'', {:params => params.merge(''page'' => @item_pages.current.next)}) if @item_pages.current.next %> HTH, Kevin> > My problem is not that paging isn''t working, sorry, but that the > links aren''t working. If I click "Next Page" it reverts back to > the entire database rather than the filtered results. My links are > done like so: > > <%= link_to ''Previous page'', { :page => > @machine_pages.current.previous } if > @machine_pages.current.previous %> > <%= link_to ''Next page'', { :page => > @machine_pages.current.next, :make => @view.make, :model => > @view.model } if @machine_pages.current.next %> > > Thanks, > Jamie van Dyke > Fear of Fish