Ruby on Rails5で記事に対してコメントする機能を追加しました。
コメントデータが全くない状態で、showページを確認すると@entry.comments.sizeが
初期状態で1になっており、partialのcomment.user.nameの部分でエラーが出てしまいます。
rails consoleでも確認しましたが、コメント数は0となっておりました。
ご教授の程宜しくお願いいたします。
show
1<%= @entry.comments.size %> #ここで1と表示される。 2<%= render :partial => "comments/comment", :collection => @entry.comments %>
railsc
12.3.0 :006 > Comment.all 2 Comment Load (0.2ms) SELECT "comments".* FROM "comments" LIMIT ? [["LIMIT", 11]] 3 => #<ActiveRecord::Relation []> 4 52.3.0 :010 > comments.size 6 (0.3ms) SELECT COUNT(*) FROM "comments" WHERE "comments"."entry_id" = ? [["entry_id", 1]] 7 => 0
comment
1 <%= comment.user.name %> 2 <%= comment.body %>
model
1class Comment < ApplicationRecord 2 belongs_to :user 3 belongs_to :entry 4 5 validates :body, presence: true 6end 7 8 9class Entry < ApplicationRecord 10 belongs_to :podcasts 11 has_many :comments 12 13end
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/08/26 04:47