Forum: Ruby on Rails Search Forms with Pagination

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
68d3f48595af5d48db8de48cf9349e71?d=identicon&s=25 Jamie van Dyke (Guest)
on 2006-02-23 16:32
(Received via mailing list)
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
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-02-23 16:44
(Received via mailing list)
On Thu, 2006-02-23 at 15:31 +0000, Jamie van Dyke wrote:
> search form
>    	@machine_pages, @machines = paginate_collection(@filtered_machines)
> Now, the list page is a full listing, and it has a filter box at the
> @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
68d3f48595af5d48db8de48cf9349e71?d=identicon&s=25 Jamie van Dyke (Guest)
on 2006-02-23 17:15
(Received via mailing list)
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
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-02-23 17:51
(Received via mailing list)
On Thu, 2006-02-23 at 16:12 +0000, Jamie van Dyke wrote:
>
----
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
68d3f48595af5d48db8de48cf9349e71?d=identicon&s=25 Jamie van Dyke (Guest)
on 2006-02-23 18:12
(Received via mailing list)
That's what I was hoping to avoid.  Thanks for your help, mate.

Thanks,
Jamie van Dyke
Fear of Fish
E0c987f680cd640c14912ebfbf0f0f07?d=identicon&s=25 unknown (Guest)
on 2006-02-23 18:58
(Received via mailing list)
> 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.
This topic is locked and can not be replied to.