###実現したいこと
gem kaminariを活用して、表示するpostの数を限定したい。
また、一番下に出てくる「もっと見るボタン」を押すと要素が増えていく設定行いたいです。
こちらのサイトを参考にしました。
https://qiita.com/rainbow___0/items/06b231273b3ae56761c8
※諸々進みましたので、一番最初の質問から修正した最新状況に書き加えました。
###エラー
なし
Postが3つ表示されますが、Moreボタンを押しても、表示数は増えません。
###コード
gemfile
1gem 'kaminari'
app/views/items/index.html.erb
view
1<div id="items"> 2 <%= render 'items' %> 3</div> 4<%= link_to_next_page @items, 'more', remote: true, id: 'more_link' %>
app/views/items/_items.html.erb
view
1<% @items.each do |item| %> 2 <div><%= item.name %></div> 3<% end %>
app/views/items/index.js.erb
js
1$('#items').append("<%= escape_javascript(render 'items', object: @items) %>") 2$("#more_link").replaceWith("<%= escape_javascript( 3 link_to_next_page(@items, 'more', remote: true, id: 'more_link') 4) %>");
app/controller/items_controller.rb
Controller
1def index 2 @items = Item.all.page(params[:page]).order(created_at: :desc) 3end
Model
1paginates_per 3
Moreボタンを押した直後のログ
log
1Started GET "/posts/index?page=2" for 127.0.0.1 at 2019-12-03 23:28:01 +0900 2Processing by PostsController#index as JS 3 Parameters: {"page"=>"2"} 4 User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 2], ["LIMIT", 1]] 5 ↳ app/controllers/application_controller.rb:6 6 Rendering posts/index.html.erb within layouts/application 7 Post Load (0.4ms) SELECT "posts".* FROM "posts" ORDER BY "posts"."created_at" DESC LIMIT ? OFFSET ? [["LIMIT", 3], ["OFFSET", 3]] 8 ↳ app/views/posts/_items.html.erb:1 9 User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]] 10 ↳ app/models/post.rb:16 11 CACHE User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]] 12 ↳ app/models/post.rb:16 13 CACHE User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]] 14 ↳ app/models/post.rb:16 15 CACHE User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]] 16 ↳ app/models/post.rb:16 17 CACHE User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]] 18 ↳ app/models/post.rb:16 19 CACHE User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]] 20 ↳ app/models/post.rb:16 21 CACHE User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]] 22 ↳ app/models/post.rb:16 23 Rendered posts/_items.html.erb (14.6ms) 24 Rendered posts/index.html.erb within layouts/application (37.3ms) 25Completed 200 OK in 129ms (Views: 100.7ms | ActiveRecord: 1.3ms)
お分かりの方宜しくお願い致します。
回答1件
あなたの回答
tips
プレビュー