Bill Clinton
2006-May-14 03:57 UTC
[Rails] Beginner question on paginate with params and conditions
I am trying to do a simple search by product name. The first page returns fine, but when I try to go to any other pages of the search results, I lose the original search parameters from my search form and get NilClass errors. Here is the code in my controller: namesearch = "%" + params[:name].strip + "%" @product_pages, @products = paginate :product, :conditions => ["product_name like :name", {:name => namesearch}], :per_page => 10 This seems like it should be a very common task and I assumed RoR would handle it automatically by adding the original search parameters to the pagination links. Is there a correct way to accomplish a multi-page search that uses conditions? -- Posted via http://www.ruby-forum.com/.
Bill Clinton
2006-May-14 18:41 UTC
[Rails] Re: Beginner question on paginate with params and conditions
Bill Clinton wrote:> I am trying to do a simple search by product name. The first page > returns fine, but when I try to go to any other pages of the search > results, I lose the original search parameters from my search form and > get NilClass errors. > > Here is the code in my controller: > > namesearch = "%" + params[:name].strip + "%" > @product_pages, @products = paginate :product, > :conditions => ["product_name like :name", {:name => > namesearch}], > :per_page => 10 > > This seems like it should be a very common task and I assumed RoR would > handle it automatically by adding the original search parameters to the > pagination links. Is there a correct way to accomplish a multi-page > search that uses conditions?I''ve made a little progress on this by using merge_params in my view. For the paginate links I changed the line pagination_links( @@product_pages ) to: pagination_links( @product_pages, :params => @params.merge (:a =>"1")) (I found I needed to add the dummy variable "a" in order to make it work) Anyway, this gets me a step closer, but it still does not work properly. It adds the fields from my original search and I am able to click on another page after page 1 of the search result, but it end there. If I click on page two, "page" is added as a param, and all future pagination links have "page=2" added to them so every link points to page 2. Also, even if I could get this to work correctly, I am unsure of how to add the "@params.merge" to the previous and next links. I am hoping someone can point me in the right direction here. My experience with rails has been that if I need to work this hard on something so simple, I am probably missing an easier way to do it. It''s hard for me to imagine that the pagination stuff only works when listing the entire contents of a table, and can''t handle user defined searches from a form field. -- Posted via http://www.ruby-forum.com/.
Tom Davies
2006-May-15 15:10 UTC
[Rails] Re: Beginner question on paginate with params and conditions
Hi Bill, You are close. Your solution should look something like this: pagination_links( @product_pages, :params => {:name => params[:name]}) The reason you are running into problems in the 2nd attempt is your @params.merge is actually overriding the :page param to hardcode it to the currently set :page number. I think in your case, you only want the name to be passed around from page to page. The :page number will be automatically determined by your pagination_links. As an alternative, if you are passing around a lot of info across pages, you could store it in the session and then your page links don''t have to know what the data is... but I have found that in the majority of cases only one param is needed to be passed around. Tom On 5/14/06, Bill Clinton <bclinton@rangercomputer.com> wrote:> Bill Clinton wrote: > > I am trying to do a simple search by product name. The first page > > returns fine, but when I try to go to any other pages of the search > > results, I lose the original search parameters from my search form and > > get NilClass errors. > > > > Here is the code in my controller: > > > > namesearch = "%" + params[:name].strip + "%" > > @product_pages, @products = paginate :product, > > :conditions => ["product_name like :name", {:name => > > namesearch}], > > :per_page => 10 > > > > This seems like it should be a very common task and I assumed RoR would > > handle it automatically by adding the original search parameters to the > > pagination links. Is there a correct way to accomplish a multi-page > > search that uses conditions? > > I''ve made a little progress on this by using merge_params in my view. > For the paginate links I changed the line > pagination_links( @@product_pages ) > > to: > pagination_links( @product_pages, :params => @params.merge (:a =>"1")) > > (I found I needed to add the dummy variable "a" in order to make it > work) > > Anyway, this gets me a step closer, but it still does not work properly. > It adds the fields from my original search and I am able to click on > another page after page 1 of the search result, but it end there. If I > click on page two, "page" is added as a param, and all future pagination > links have "page=2" added to them so every link points to page 2. > > Also, even if I could get this to work correctly, I am unsure of how to > add the "@params.merge" to the previous and next links. > > I am hoping someone can point me in the right direction here. My > experience with rails has been that if I need to work this hard on > something so simple, I am probably missing an easier way to do it. It''s > hard for me to imagine that the pagination stuff only works when listing > the entire contents of a table, and can''t handle user defined searches > from a form field. > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Tom Davies http://blog.atomgiant.com http://gifthat.com
Charlie Bowman
2006-May-15 15:27 UTC
[Rails] Re: Beginner question on paginate with params and conditions
If you are going to implement any sort of caching I would suggest modifying the pagination to include the parameters as part of the url. If you don''t do this, page caching will not work for any pages that include pagination. Charlie Bowman http://www.recentrambles.com On Mon, 2006-05-15 at 11:09 -0400, Tom Davies wrote:> Hi Bill, > > You are close. Your solution should look something like this: > > pagination_links( @product_pages, :params => {:name => params[:name]}) > > The reason you are running into problems in the 2nd attempt is your > @params.merge is actually overriding the :page param to hardcode it to > the currently set :page number. > > I think in your case, you only want the name to be passed around from > page to page. The :page number will be automatically determined by > your pagination_links. > > As an alternative, if you are passing around a lot of info across > pages, you could store it in the session and then your page links > don''t have to know what the data is... but I have found that in the > majority of cases only one param is needed to be passed around. > > Tom > > On 5/14/06, Bill Clinton <bclinton@rangercomputer.com> wrote: > > Bill Clinton wrote: > > > I am trying to do a simple search by product name. The first page > > > returns fine, but when I try to go to any other pages of the search > > > results, I lose the original search parameters from my search form and > > > get NilClass errors. > > > > > > Here is the code in my controller: > > > > > > namesearch = "%" + params[:name].strip + "%" > > > @product_pages, @products = paginate :product, > > > :conditions => ["product_name like :name", {:name => > > > namesearch}], > > > :per_page => 10 > > > > > > This seems like it should be a very common task and I assumed RoR would > > > handle it automatically by adding the original search parameters to the > > > pagination links. Is there a correct way to accomplish a multi-page > > > search that uses conditions? > > > > I''ve made a little progress on this by using merge_params in my view. > > For the paginate links I changed the line > > pagination_links( @@product_pages ) > > > > to: > > pagination_links( @product_pages, :params => @params.merge (:a =>"1")) > > > > (I found I needed to add the dummy variable "a" in order to make it > > work) > > > > Anyway, this gets me a step closer, but it still does not work properly. > > It adds the fields from my original search and I am able to click on > > another page after page 1 of the search result, but it end there. If I > > click on page two, "page" is added as a param, and all future pagination > > links have "page=2" added to them so every link points to page 2. > > > > Also, even if I could get this to work correctly, I am unsure of how to > > add the "@params.merge" to the previous and next links. > > > > I am hoping someone can point me in the right direction here. My > > experience with rails has been that if I need to work this hard on > > something so simple, I am probably missing an easier way to do it. It''s > > hard for me to imagine that the pagination stuff only works when listing > > the entire contents of a table, and can''t handle user defined searches > > from a form field. > > > > > > -- > > 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/20060515/3f62c2c9/attachment-0001.html