質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.49%
Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

2回答

2268閲覧

コメント一覧をページングしたい

satail

総合スコア31

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

1グッド

0クリップ

投稿2017/03/29 08:53

メッセージページのコメント一覧にpaginate(kaminari)を以下のように追加したのですが、
paginateの欄だけ表示されてページ移動してもコメント一覧に何も変化がなく機能しません。
どうしたらコメント3つ目以降はpaginateに反映されるでしょうか

###該当のソースコード

message_controller.rb def show @comments = @message.comments.page(params[:page]).per(3) end
messages/show.html.erb <%= render @message.comments %> <%= paginate @comments %>
mingos👍を押しています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

自己解決

<%= render @message.comments %>でコメント一覧を返すようにしていたのですが、

<% @comments.each do |comment| %> .... .... <% end %>

で返すようにすることで、paginateが反映するようになりました。
こちらの記事がとても参考になりました。

投稿2017/04/03 12:34

satail

総合スコア31

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

コードは問題なさそう。。
データがおかしいということはありませんか?
@message.commentsがあやしいですね。。

p @message.comments.page.to_sqlで発行SQLを見てみてください。
原因が分かるはずです。

投稿2017/03/30 04:44

編集2017/03/30 04:45
tqkqt0

総合スコア155

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

satail

2017/03/30 06:33 編集

Started GET "/messages/1" for 60.237.45.180 at 2017-03-30 06:29:26 +0000 Cannot render console from 60.237.45.180! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 Processing by MessagesController#show as HTML Parameters: {"id"=>"1"} Message Load (0.4ms) SELECT "messages".* FROM "messages" WHERE "messages"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]] User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 100], ["LIMIT", 1]] Comment Load (0.3ms) SELECT "comments".* FROM "comments" WHERE "comments"."message_id" = ? LIMIT ? OFFSET ? [["message_id", 1], ["LIMIT", 25], ["OFFSET", 0]] #<ActiveRecord::AssociationRelation [#<Comment id: 1, content: "Hi!!!!", message_id: 1, user_id: 1, created_at: "2017-03-25 06:29:32", updated_at: "2017-03-25 06:29:32">, #<Comment id: 2, content: "Hey !!!", message_id: 1, user_id: 100, created_at: "2017-03-26 01:19:13", updated_at: "2017-03-26 01:19:13">, #<Comment id: 3, content: "wadadoak", message_id: 1, user_id: 1, created_at: "2017-03-30 05:53:19", updated_at: "2017-03-30 05:53:19">, #<Comment id: 4, content: "csclspclspc", message_id: 1, user_id: 1, created_at: "2017-03-30 05:53:26", updated_at: "2017-03-30 05:53:26">, #<Comment id: 5, content: "lpcldpldpcldpl", message_id: 1, user_id: 1, created_at: "2017-03-30 05:53:32", updated_at: "2017-03-30 05:53:32">, #<Comment id: 6, content: "vfokvfokvf", message_id: 1, user_id: 100, created_at: "2017-03-30 06:07:42", updated_at: "2017-03-30 06:07:43">, #<Comment id: 7, content: "lpvlpclvp", message_id: 1, user_id: 100, created_at: "2017-03-30 06:07:49", updated_at: "2017-03-30 06:07:49">, #<Comment id: 8, content: "lplplvpflvpflvpf", message_id: 1, user_id: 100, created_at: "2017-03-30 06:07:55", updated_at: "2017-03-30 06:07:55">, #<Comment id: 9, content: "lplvbbvbvbll", message_id: 1, user_id: 100, created_at: "2017-03-30 06:08:03", updated_at: "2017-03-30 06:08:03">]> Rendering messages/show.html.erb within layouts/application ActsAsTaggableOn::Tagging Load (0.3ms) SELECT "taggings".* FROM "taggings" WHERE "taggings"."taggable_id" = ? AND "taggings"."taggable_type" = ? [["taggable_id", 1], ["taggable_type", "Message"]] ActsAsTaggableOn::Tag Load (0.3ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = ? AND "taggings"."taggable_type" = ? AND (taggings.context = 'games' AND taggings.tagger_id IS NULL) [["taggable_id", 1], ["taggable_type", "Message"]] User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]] User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]] Comment Load (0.3ms) SELECT "comments".* FROM "comments" WHERE "comments"."message_id" = ? [["message_id", 1]] User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]] User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]] User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 100], ["LIMIT", 1]] User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 100], ["LIMIT", 1]] User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]] User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]] User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]] User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]] User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]] User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]] User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 100], ["LIMIT", 1]] User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 100], ["LIMIT", 1]] User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 100], ["LIMIT", 1]] User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 100], ["LIMIT", 1]] User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 100], ["LIMIT", 1]] User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 100], ["LIMIT", 1]] User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 100], ["LIMIT", 1]] User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 100], ["LIMIT", 1]] Rendered collection of comments/_comment.html.erb [9 times] (17.9ms) Rendered comments/_form.html.erb (7.0ms) Rendered messages/show.html.erb within layouts/application (44.2ms) Completed 200 OK in 178ms (Views: 114.2ms | ActiveRecord: 6.9ms) このように出ました どのようにしたらよろしいでしょうか
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.49%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問