/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