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

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

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

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

Q&A

解決済

1回答

595閲覧

Railsで投稿にコメントできるようにしていて、そのコメントをトップページに表示したいのですが・・・

gratelyas

総合スコア6

Ruby on Rails

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

0グッド

0クリップ

投稿2020/01/27 15:39

前提・実現したいこと

Railsで投稿アプリを作っていて、
その投稿に付いているコメントを
トップページに表示させようと思っています。

発生している問題・エラーメッセージ

undefined method `comments' for nil:NilClass

該当のソースコード

commentrb

1 2class Comment < ApplicationRecord 3 mount_uploader :image, ImageUploader 4 belongs_to :post 5 belongs_to :user 6end 7

postrb

1 2class Post < ApplicationRecord 3 mount_uploader :image, ImageUploader 4 has_many :comments 5end 6

userrb

1 2class User < ApplicationRecord 3 # Include default devise modules. Others available are: 4 # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable 5 devise :database_authenticatable, :registerable, 6 :recoverable, :rememberable, :validatable 7 has_many :comments 8end 9

commentcontroller

1class CommentsController < ApplicationController 2 def create 3 Comment.create(comment_params) 4 redirect_to root_path #ここの記述は勉強し直し! 5 end 6 7 private 8 def comment_params 9 params.require(:comment).permit(:text, :image).merge(user_id: current_user.id, post_id: params[:post_id]) 10 end 11end

postcontroller

1class PostsController < ApplicationController 2 3 def index 4 @posts = Post.all 5 end 6 7 def new 8 @post = Post.new 9 end 10 11 12 def create 13 Post.create(post_params) 14 redirect_to root_path 15 end 16 17 18 def show 19 @post = Post.find(params[:id]) 20 @comment = Comment.new 21 @comments = @post.comments.includes(:user) 22 end 23 24 25 def edit 26 @post = Post.find(params[:id]) 27 end 28 29 def update 30 @post = Post.find(params[:id]) 31 @post.update(post_params) 32 redirect_to root_path 33 end 34 35 def destroy 36 @post = Post.find(params[:id]) 37 @post.destroy 38 redirect_to root_path 39 end 40 41 42 private 43 def post_params 44 params.require(:post).permit(:title, :image, :subtitle, :content) 45 end 46 47 48end 49

次の記述でトップページにpostについたコメントを表示したい
と思ったのですが、エラーです。

index

1.comment-square 2  - @post.comments.each do |comment| 3    .comment-content 4      = comment.text

試したこと

postコントローラーのindexに記述を加えました。

def index @posts = Post.all   @post = Post.find(params[:id]) end
 def index @posts = Post.all    @comments = @post.comments.includes(:user) end

など試しているのですが、上手くできません。

何かヒントをいただけたら幸いです。

postテーブルにcomment_idを取得する必要などがあるのでしょうか?

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

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

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

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

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

guest

回答1

0

ベストアンサー

postのコントローラの部分で

def index @comments = Post.comments.order('created_at DESC') end

にして、indexのビューに

@comments.each do |comment|

としないとエラーがでてしまいます。これは、コントローラでcommentとpostとの間に結びつきがされていない状態で@post.commentsとしているためです。

投稿2020/01/27 15:53

bamboo-nova

総合スコア1408

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

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

gratelyas

2020/01/27 23:01

bamboo-nova様 回答いただきありがとうございます。 おっしゃられたように記述してみたところ undefined method `comments' for #<Class:0x00007fc2d7c4a558> commentsがNillClassから#<Class:0x00007fc2d7c4a558>このように変わりました。 上手く定義できていないという事ですかね?
bamboo-nova

2020/01/28 03:24

追記ミスですが、 if @comments.present? のようにして、もしも@commentsに中身が存在すればの構文をeachの前に置かないと、そもそもコメント履歴がない場合に関しては正常に動作しないでエラーになりますね。失念しておりました...汗 参考資料) https://teratail.com/questions/17353
gratelyas

2020/01/28 06:33

ご丁寧に回答いただきありがとうございました! 助かりました!!!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問