masuka
masuka

Reputation: 133

will_paginate not working in a certain view

I have the following code in my view file

<div id="articles">
<% @results.each do |r| %>
<h2>
<%= link_to r.univ_name, "#" %>
</h2>
<div class="info">
<b><%= r.location %>, <%= r.state %></b> <br/>
</div>
<br /><br />
<br /><br />
<div class="content"><p><%= r.description %><p></div>
<% end %>
</div>
<%= will_paginate @results %>

I included

@results = Result.search(params[:query], page: params[:page], per_page: 4)

in results_controller.rb file. I have around 8 records in my results table however the view lists out all the records instead of limiting them to 4 per page. Can somebody suggest where i am doing it wrong?

Thank you

Upvotes: 0

Views: 359

Answers (2)

Arup Rakshit
Arup Rakshit

Reputation: 118299

As per the Basic will_paginate use, your code is wrong. It should be as below :

Result.search(params[:query]).paginate(page: params[:page], per_page: 4)

Upvotes: 2

Kirti Thorat
Kirti Thorat

Reputation: 53048

You should be using paginate method of will_paginate gem to implement pagination.

@results = Result.search(params[:query]).paginate(page: params[:page], per_page: 4)

Make sure you are setting @results instance variable in the Controller's action which renders your view with

<%= will_paginate @results %>

Upvotes: 0

Related Questions