添加注释到 posts = undefined local variable or method `post' for
Add comments to posts = undefined local variable or method `post' for
你好,我遇到了这样的错误 undefined local variable or method 'post' for #<#<Class:0x007ffc40469ad0>:0x007ffc40461088>
我不明白,因为我在 post 中创建了一个名为 _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| %>
模型 post 是 has_many :comments, dependent: :destroy
模型 post 是 has_many :comments, dependent: :destroy
模特评论是belongs_to :user
belongs_to :post
路线是
资源:posts 做
资源:类别
资源:评论
结束
评论控制器是
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
非常感谢您的帮助。
尝试更新以下内容:
<%= simple_form_for [post, post.comments.new] do |f| %>
有了这个:
<%= simple_form_for [@post, @post.comments.new] do |f| %>
你好,我遇到了这样的错误 undefined local variable or method 'post' for #<#<Class:0x007ffc40469ad0>:0x007ffc40461088>
我不明白,因为我在 post 中创建了一个名为 _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| %>
模型 post 是 has_many :comments, dependent: :destroy
模型 post 是 has_many :comments, dependent: :destroy
模特评论是belongs_to :user
belongs_to :post
路线是 资源:posts 做 资源:类别 资源:评论 结束
评论控制器是
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
非常感谢您的帮助。
尝试更新以下内容:
<%= simple_form_for [post, post.comments.new] do |f| %>
有了这个:
<%= simple_form_for [@post, @post.comments.new] do |f| %>