will_paginate - don't show page entries info if collection is empty or less than per-page param
Let's say I have a controller action that creates the WillPaginate collection:
@comments = WillPaginate::C开发者_开发知识库ollection.new(@page_num, 15, @comments.length).concat(comments_to_paginate)
Then in my view:
<div class="pag">
<div clas="page_info">
<%= page_entries_info @comments %>
</div>
<%= will_paginate @comments, :container => false %>
</div>
Now what I want to do is to NOT show the page_entries_info output if (1) there are no comments and (2) if the number of entries (eg, 7) is less than the per page limit (15).
How would you go about handling this?
You just need to guard your page_entries_info
with the conditions you wish
For example
<div class="pag">
<% if @comments.length > 0 && @comments.total_pages > 1 %>
<div class="page_info">
<%= page_entries_info @comments %>
</div>
<% end %>
<%= will_paginate @comments, :container => false %>
</div>
Or you can put this in your controller and keep your view a little cleaner, also letting you have one variable to reuse if you need the same guard around other parts of view code.
@comments = WillPaginate::Collection.new(@page_num, 15, @comments.length).concat(comments_to_paginate)
@show_pagination = @comments.length > 0 && @comments.total_pages > 1
Then in the view:
<div class="pag">
<% if @show_pagination %>
<div class="page_info">
<%= page_entries_info @comments %>
</div>
<% end %>
<%= will_paginate @comments, :container => false %>
</div>
If you can deal with the extra div, then this should also work
<div class="pag">
<div class="page_info">
<%= page_entries_info(@comments) if @show_pagination %>
</div>
<%= will_paginate @comments, :container => false %>
</div>
精彩评论