i have no idea i'm doing wrong. have associated comments posts , users.
user.rb
has_many :comments, dependent: :destroy
post.rb
has_many :comments, dependent: :destroy
comment.rb
belongs_to :user belongs_to :post
comments controller
def create @post = post.find(params[:post_id]) @comment = @post.comments.create(params[:comment].permit(:body)) @comment.user = current_user if @comment.save redirect_to post_path(@post) else # else end end
posts view
<h2><%= @comment.user.name %></h2> <p><%= comment.body %></p>
what doing wrong? in controller, have tried @comment.user = current_user.name
in posts view, have tried <%= @comment.user.name %>
, <%= @comment.user %>
error: can't remember if same error got before, error i'm seeing when try create comment.
activemodel::missingattributeerror in commentscontroller#create can't write unknown attribute `user_id` @comment.user = current_user
so error message looks might not have foreign key user_id on comments table.
you need run
rails g migration addusertocomments user:references
Comments
Post a Comment