@user.name
works on posts, user profile, , everywhere else on site. when add user comments, undefined method?
<p class="comment_body"> <h2><%= @user.name %></h2> <%= comment.body %> </p>
i tried <%= user.name %>
, still error.
i tried <%= current_user.name %>
, worked. however, don't want show current user's name, want see user's name of posted comment.
edit:
i forgot associate user comment, i'm not sure how.
comments controller
class commentscontroller < applicationcontroller def create @post = post.find(params[:post_id]) @comment = @post.comments.create(params[:comment].permit(:body)) redirect_to post_path(@post) end end
i went comments.rb , wrote belongs_to :user
, in user.rb added has_many :comments
.
so, should set user , save comment, shouldn't you? try like
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
Comments
Post a Comment