2016-03-28 64 views
0

您好我有這樣undefined local variable or method 'post' for #<#<Class:0x007ffc40469ad0>:0x007ffc40461088>添加評論帖=未定義的局部變量或方法'後」的

一個錯誤,我不明白,因爲我在後期製作的部分,其名稱_comments.html.erb

<p class="text-center">Poster un commentaire</p> 
     <%= simple_form_for [post, post.comments.new] do |f| %> 
     <%= f.error_notification %> 
     <%= f.input :content, label: "Commentaire"%> 
     <%= f.submit "Envoyer", class: "btn btn-primary" %> 
     <% end %> 

和它呈現像<% render 'comments' %>

的未定義的方法是在這條線<%= simple_form_for [post, post.comments.new] do |f| %>

模型後的has_many :comments, dependent: :destroy

模型後是has_many :comments, dependent: :destroy

模型的評論是belongs_to :user belongs_to :post

這條路線是 資源:帖子做 資源:類 資源:評論 結束

評論控制器是

class CommentsController < ApplicationController 

before_action :set_post 

def create 
    @comment = @post.comments.build(comment_params) 
    @comment.user_id = current_user.id 

    if @comment.save 
    flash[:success] = "You commented the hell out of that post!" 
    redirect_to :back 
    else 
    flash[:alert] = "There is a problem with your comment" 
    render root_path 
    end 
end 

def destroy 
    @comment = @post.comments.find(params[:id]) 

    @comment.destroy 
    flash[:success] = "Comment deleted :(" 
    redirect_to root_path 
end 

private 

def set_post 
    @post = Post.find(params[:post_id]) 
end 

def comment_params 
    params.require(:comment).permit(:content, :post_id, :user_id) 
end 
end 

非常感謝您的幫助。

回答

1

嘗試更新如下:

<%= simple_form_for [post, post.comments.new] do |f| %>

與此:

<%= simple_form_for [@post, @post.comments.new] do |f| %>

+0

它的工作原理,但渲染'<%渲染 '意見' %>'顯示什麼..你知道爲什麼? –

+0

您應該使用'<%= render'註釋'%>',而不是'<%render'註釋'%>'。注意缺少'='符號? – Dharam

+0

非常感謝你我錯過了'=' –

相關問題