読者です 読者をやめる 読者になる 読者になる

Railsで「もっと見る」の実装

1. Gemfile

amatsuda/kaminari · GitHub

gem 'kaminari'
2. Model

・ app/models/item.rb

class Item < ActiveRecord::Base
  paginates_per 50
  default_scope :order => 'created_at DESC'
  
end  
3. View

・ app/views/items/_items.html.erb

<% @items.each do |item| %>
  <div><%= item.name %></div>
<% end %>

・ app/views/items/index.html.erb

<div id="items">
  <%= render 'items' %>
</div>

<%= link_to_next_page @items, 'more', remote: true, id: 'more_link' %>

・ app/views/items/index.js.erb

$('#items').append("<%= escape_javascript(render 'items', object: @items) %>")
$("#more_link").replaceWith("<%= escape_javascript(
  link_to_next_page(@items, 'more', remote: true, id: 'more_link')
) %>");
4. Controller

・ app/controller/items_controller.rb

class ItemsController < ApplicationController
  def index
    @items = Item.page params[:page]
  end
end

Source: Rails 3 - "More" ajax pagination with Kaminari - Stack Overflow