简体   繁体   中英

Limiting Number of Posts Per Page - Ruby on Rails Blog

I am currently developing a Ruby on Rails blog. I have my blog posts show up on the main page, however, I would like to list the posts 5 at a time, so that my frontpage doesn't go on forever and my blog will look much cleaner.

Let me know if you can help. Much appreciated.

Looks like you need a pagination solution - consider using kaminari or will_paginate ( https://github.com/amatsuda/kaminari , https://github.com/mislav/will_paginate/wiki )

And if you need an endless page, there is a nice screencast about that: http://railscasts.com/episodes/114-endless-page

For example, if using will_paginate for pagination, you just call paginate method at end of line your query inside controller, for example inside your controller

def index
  @blogs = Blog.all.paginate(:page => params[:page], :per_page => 5)
end

from your view, just simply put:

will_paginate @blogs

at specify location, to show pagination.

If I understand right, you want to limit the number of post on the home page . Then you should do like

Model.find(:all, :limit => 5, :order=> 'created_at desc')

you can remove the order if you don't need it. If you need to make pagination take a look at will_paginate

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM