开发者

Rails 3 & Kaminari pagination problem

开发者 https://www.devze.com 2023-03-11 20:12 出处:网络
Ok so I have decided to use Kaminari for pagination in a rails 3 project.I have followed the video from RailsCasts http://railscasts.com/episodes/254-pagination-with-kaminari

Ok so I have decided to use Kaminari for pagination in a rails 3 project. I have followed the video from RailsCasts http://railscasts.com/episodes/254-pagination-with-kaminari

All goes well up until the point or running the server.

controllers/stories_controller.rb

def index
   @stories = Story.all
   @pages = Story.page(params[:page]).per(3)
   @stories = Story.search(params[:search]) 
end

views/stories/index.html.erb

<%= paginate @pages %&g开发者_Python百科t;

When i start the server the index page in question displays all the stories from the DB and renders the pagination view showing (1 2 Next > Last »). What am I missing to get the pagination working?


I still can not understand your code. Why do you assign Story.all to @stories in the 1st line and overwrite the variable in the 3rd line?

Anyways, @stories will display "all the stories from the DB" because you're not calling the pagination method (.per) on @stories. The pagination links will show you the paginated counts because you're calling per method on @page variable and passing it to the helper.

I mean, you need to call .per on the relation before passing it to <%= paginate %> helper. It's quite simple.


I guess you want to get results from your search, right? Try

@stories = Story.search(params[:search]).page(params[:page]).per(3)

and something like:

<% @stories.each do |story| %>
<%= render story %>
<% end %>
<%= paginate @stories %>

in your view

0

精彩评论

暂无评论...
验证码 换一张
取 消