undefined local variable or method `comment' for #<#<Class:0xb6d2fd0>:0xc077dd8>
这就是我的_comment.html.erb部分看起来像
<div class="media">
<%= link_to '#', class: 'pull-left' do %>
<%= image_tag(comment.user.avatar.small.url) if comment.user.avatar? %>
<% end %>
<div class="media-body">
<small>
<%= comment.user.name %> commented <%= time_ago_in_words(comment.created_at) %> ago
<% if policy(comment).destroy? %>
| <%= link_to "Delete", [@topic, @post, comment], method: :delete %>
<% end %>
</small>
</div>
我的comments_controller.rb的创建部分
def create
@post = Post.find(params[:post_id])
@comment = current_user.comments.build(comment_params)
@comment.post = @post
authorize @comment
if @comment.save
redirect_to @post, notice: "Comment was saved successfully."
else
flash[:error] = "Error creating comment. Please try again."
render :new
end
结束
以及我在帖子中的呈现方式#show view
<div>
<%= render :partial => "comments/comment", local: {comment: @comment} %>
</div>
答案 0 :(得分:3)
您没有将任何本地人传递给此部分:
<%= render :partial => "comments/comment" %>
你想要这样做:
<%= render :partial => "comments/comment", :locals => { :comment => @comment } %>
或使用快捷方式:
<%= render 'comments/comment', :comment => @comment %>
答案 1 :(得分:1)
您在comment
部分声明了名为_comment.html.erb
的本地变量。
您需要从控制器传递@comment
,如下所示:
<%= render :partial => "comments/comment", locals: { comment: @comment } %>
请查看Layouts and Rendering Rails指南以获取更多信息。