Anyone know how to turn off will_paginate in Spree? - ruby-on-rails

Deleting the lines of
<% if paginated_products.is_a?(WillPaginate::Collection)
params.delete(:search)
params.delete(:taxon)
%><div class="clear"></div><%= will_paginate(paginated_products,
:previous_label => "« #{t('previous')}",
:next_label => "#{t('next')} »") %>
<% end -%>
Only makes it so that the pagination nav disappears but not the pagination as a whole.

It's a bit hackey but you could just set the per_page setting to something like 1,000 so it wouldn't ever have to paginate the results.
Throwing something like this in config/initializers/will_paginate.rb should work.
ActiveRecord::Base.instance_eval do
def per_page
1000
end
end

Related

How to display two differents models in my view with will_paginate in rails?

Hi everyone I have a question , I looking for on google but I don't find an answer.
I'm actually want to display Post model and User model by search in same page and for this I want to use Will_paginate to display all of them .
I try this solution =>
**require 'will_paginate/array'
#posts =Post.search(params[:q]).order("created_at DESC")
#users=User.search(params[:q]).order("created_at DESC")
#all_records = (#posts + #users).paginate(:page =>params[:page], :per_page => 10)**
And In my view I want to display all query found ,so I try something like this =>
**<%unless #posts.nil?%>
<% #posts.each do |post| %>
....
**
**<% unless #users.nil?%>
<% #users.each do |user| %>
...**
<%= will_paginate #all_records %>
But it doesn't work nothing happend, so I want to know how to get #posts and #users records in #all_records to do something like this =>
**
<% #all_records[#posts].each do.. %>
<% #all_records[#usets].each do ..%>
**
I saw some solutions that suggest to use two differentes paginate in my view like =>
**<%= will_paginate #posts %>
<%= will_paginate #users %>**
**But It's not want I want , I would'like only one paginate for all, Please Help me cause I'm going crazy, Thank you **

Rails 4 will_paginate articles/post by tags

trying to paginate a list of articles (ideas in my case) which are on my tag show page. So I am listing all ideas that are tagged with "loremipsum". The problem is :per_page => 3 doesn't seem to take effect. All ideas show up (I have 4 for tag "loremipsum") withour error. Pagination links also show at page bottom (but page2 doesn't work).
tags_controller:
def show
#tag = Tag.find_by_name(params[:id])
#ideas = #tag.ideas.all.paginate(:per_page => 3, :page => params[:page])
end
In show.html.erb
<% #tag.ideas.each do |idea| %>....<% end %>
<%= will_paginate #ideas %>
On my idea list page pagination works just fine so no clue.
Any help much appreciated. Thanks in advance!
I would suggest using #ideas.each in your view rather than #tag.ideas.each.
<% #ideas.each do |idea| %> ... <% end %>
<%= will_paginate #ideas %>
That is, I believe the problem you encountered was because you were not iterating over the same collection that you applied the pagination to. Though #ideas will paginate, #tag.ideas will not.
Replace this line
#ideas = #tag.ideas.all.paginate(:per_page => 3, :page => params[:page])
with
#ideas = #tag.ideas.paginate(:per_page => 3, :page => params[:page])
I believe you don't need that all.

Kaminari and extra params

I've looked around and I can't find anything close to answer for this. I think it's a pretty unique edge case though so I'm not surprised.
I have an index page where I'm showing all the leads, but I have 3 separate ways of showing it: unreviewed, reviewed, all. To accomplish this I use the current_page? helper, add: ?unreviewed=true to the url.
This worked no problem by itself, when I added in kaminari for pagination the page params seems to mess this up and make it so the current_page? cant read the extra params.
How can I fix this, or is there a better way to accomplish this goal?
This is using Rails 4 and Kaminari 0.15.1
Controller:
def index
#leads = Lead.order(:id).page params[:page]
#reviewed_leads = Lead.where(reviewed: true).order(:id).page params[:page]
#unreviewed_leads = Lead.where(reviewed: nil).order(:id).page params[:page]
current_page_no = Lead.page
end
index.html.erb
<tr>
<td colspan="7" style="text-align: center;">
<div class="pagination pagination-table">
<% if current_page?(controller: 'leads', action: 'index', unreviewed: 'true') %>
<%= paginate #unreviewed_leads %>
<% elsif current_page?(controller: 'leads', action: 'index', reviewed: 'true') %>
<%= paginate #reviewed_leads %>
<% else %>
<%= paginate #leads %>
<% end %>
</div>
</td>
</tr>
It worked when the URL looked like:
/closingloop/leads?unreviewed=true
with it like this, the current_page isn't reading it right and it's always showing the #leads instance variable
/closingloop/leads?page=2&unreviewed=true
any help would be greatly appreciated. I'm going to continue trying things. Will update with what I find.
Change your controller action to
def index
#leads = Lead.where(reviewed: params[:reviewed]).order(:id).page(params[:page])
end
Then in your view, you don't have to check for the current page, just make sure that you're passing the value of params[:reviewed]
<%= paginate #leads, params: { reviewed: params[:reviewed] } %>
UPDATE
As pointed out in the comments, this doesn't work for showing mixed leads. This can be achieved by creating a scope
# lead model
def self.filter_by_reviewed_status(status)
case status
when nil then scoped
when 'true' then where(reviewed: true)
when 'false' then where(reviewed: false)
end
end
Then in your controller, use this in getting the leads
#leads = Lead
.filter_by_reviewed_status(params[:reviewed])
.order(:id)
.page(params[:page])
Be sure to set params[:reviewed] to either true, false or nil (not pass at all) in links to get the leads you need

Paginate Comments With will_paginate

I'm using the will_paginate gem and currently have a list that is already paginated.
#businesses = Business.paginate(:page => params[:page], :per_page => 7)
<%= will_paginate #businesses %>
<% #businesses.each do |business| %>
To display the comments for a particular entry I use:
<% #business.comments.each do |comment| %>
I thought something like the following might work but it didn't:
#business.comments = Business.comments.paginate(:page => params[:page], :per_page => 7)
<%= will_paginate #business.comments %>
I can't seem to find any answer to paginating the comments (99.9% sure its to do with it being #something.something instead of just #something)
Setting #business.comments not only won't work but is actually dangerous and destructive. You are setting the comments associated to the business to be only the ones returned by that paginate call. (Check what the log shows when you set #business.comments to the paginated call.
You have, however, got the call right. You just need to set it to a different variable rather than reusing #business.comments. If you use #comments instead it will work fine:
#comments = #business.comments.paginate(:page => params[:page], :per_page => 7)
and
#comments.each do |comment|
...
end
will_paginate #comments

adding search functionality to a paginated page in RoR

Here is the scenario:
I have a table which renders a list of groups (id,title,description,etc) like below which is paginated, it also has a search form:
<div id="navigation" style="background:white;border:1px outset cadetblue;">
<p>
<% form_tag groups_path, :method => 'get' do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
</p>
</div>
<table>
.....
</table>
<%= paginate #groups %>
In my groups_controller.rb I have:
#groups = Group.search(params[:search])
#groups = Group.order("id").page(params[:page]).per(15)
which doesn't seem to be working. The pagination works but when I click
the search button it doesn't do anything.
The search function is implemented in the group.rb model:
def self.search(search)
if search
find(:all, :conditions => ['description LIKE ?', "%#{search}%"])
else
find(:all)
end
end
How do I solve this, how do I make my search form work and return results in a paginated way!
Here is the ultimate answer to this post. All the original poster needed to do was this:
#groups = Group.where("description LIKE '%#{params[:search]}%'").order('id').page(params[:page]).per(15)
In an offline discussion, it wasn't working. turns out he had BOTH will_paginate Gem and kaminari Gem for his pagination. He was crapping out when it came to .per(15). Deleting the will_paginate gem fixed it!
will_paginate isn't being actively upgraded, so if you have will_paginate and you are on Rails 3.X, get rid of will_paginate and use kaminari instead, it's a fairly painless swap out, just a bit of css work to format the pagination.
You need to combine those two calls into one, right now you are overriding the first setting of #groups with the second.
I'd add a scope to your group model
class Group
scope :search, lambda { |val| , find(:all, :conditions=>['description LIKE ?', "%#{val}%"]) }
end
Then you can do
#groups = Group.search(params[:search]).order("id").page(params[:page]).per(15)

Resources