###前提・実現したいこと
投稿に対するコメントを実装したいです。
Railsはチュートリアルと参考書で勉強中です(3ヶ月程)
ユーザー投稿 ーコメント ーコメント ユーザー投稿 ーコメント ユーザー投稿 ーコメント ーコメント ーコメント
###該当のソースコード
Ruby
1#関連付け 2class User < ApplicationRecord 3has_many :microposts, dependent: :destroy 4has_many :comments 5 6class Micropost < ApplicationRecord 7belongs_to :user 8has_many :comments, dependent: :destroy 9 10class Comment < ApplicationRecord 11belongs_to :user 12belongs_to :micropost
Ruby
1#コントローラー側 2#残骸も載せておきます。 3class MicropostsController < ApplicationController 4 5def index 6 @micropost = current_user.microposts.build if logged_in? 7 @microposts = Micropost.all 8 #@comment = Micropost.last.comments 9 #@post = Micropost.includes(:user) 10 #@comments = @post.comments.includes(:user).all 11 end
ruby
1#view側 2<% @microposts.each do |b| %> 3 <p><%= b.comments.content %></p> 4 <% end %>
###エラー内容
undefined method `content' for #<ActiveRecord::Associations::CollectionProxy []> Did you mean? connection
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。