/show.html.erb我得到这个错误未定义的方法`FIRST_NAME”的零:NilClass无法显示用户名
comments_controller.rb
class CommentsController < ApplicationController
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.build(comment_params)
@comment.user_id = current_user.id
@comment.save
redirect_to post_path(@post)
end
def new
@comment = Comment.new
end
def show
@comment = Comment.find(params[:id])
end
def destroy
@post = Post.find(params[:post_id])
@comment = @post.comments.find(params[:id])
@comment.destroy
redirect_to post_path(@post)
end
private
def comment_params
params.require(:comment).permit(:body, :user_id, :post_id)
end
end
我的意见/ _show.html.erb
<% @post.comments.each do |comment| %>
<h3><%= comment.body %></h3>
<h6><%= comment.user.first_name %></h6>
<% end %>
个我帖子/ show.html.erb
<div class="show_post">
<h1><%= @post.title %></h1>
<p>Created <%= time_ago_in_words(@post.created_at) %> ago |
<%= link_to 'Edit', edit_post_path %>
|
<%= link_to 'Delete', '#' %>
</p>
<div class="body">
<p><%= @post.body %></p>
</div>
<hr>
<div id="comments">
<%= render 'comments/show' %>
<%= render 'comments/new' %>
</div>
是current_user nil? – eeeeeean
在控制台我有** user_id **,但为什么我不能在视图上显示? #]> –
请评论has_many用户吗? – Doon