我想创建一个简单的表单来对食谱中已发布的食谱进行新的评论。我在食谱显示页面上呈现评论表单,但它一直给我同样的错误:
nil:NilClass 的未定义方法“model_name”
当我不在app/views/recipes/show.html.erb呈现部分新评论表单,而是创建文件app/views/reviews/new.html.erb时,只有这样表单才起作用。我不明白为什么当我尝试在显示食谱页面呈现该表单时,该表单无法正常工作。
这是我的代码:
简单的表格:
<%= simple_form_for(@review, url: recipe_reviews_path(@recipe)) do |f| %>
<%= f.error_notification %>
<%= f.input :content %>
<%= f.input :rating %>
<%= f.button :submit, class: "btn btn-success" %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
评论控制器:
class ReviewsController < ApplicationController
def new
@recipe = recipe.find(params[:recipe_id])
@review = Review.new(review_params)
end
def create
@recipe = recipe.find(params[:recipe_id])
@review = Review.new(review_params)
@review.recipe = @recipe
if @review.save
redirect_to recipe_path(@recipe)
else
render 'recipe/show'
end
end
private
def review_params
params.require(:review).permit(:content, :rating)
end
end
Run Code Online (Sandbox Code Playgroud)
食谱控制器: …