我的应用程序正在使用Rails 4.1.8并且我使用Simple Form gem。
我在private
下的强参数定义中得到了未定义的局部变量或方法错误,我不明白其原因。
_form.haml.html
= simple_form_for @recipe, html: { multipart: true } do |f|
- if @recipe.errors.any?
#errors
%h2
= pluralize(@recipe.errors.count, "error")
prohibited this recipe from being saved
%ul
- @recipe.errors.full_messages.each do |msg|
%li= msg
= f.input :title
= f.input :serving
= f.input :prep
= f.input :cook
= f.button :submit
控制器
class RecipesController < ApplicationController
def create
@recipe = Recipe.new(recipe_params)
if @recipe.save
redirect_to @recipe, notice: "Successfully created"
else
render 'new'
end
private
def recipe_params
params.require(:recipe).permit(:title, :serving, :prep, :cook)
end
end
end
提交按钮带我去路由http://localhost:3000/recipes
而不是
显示http://localhost:3000/recipes/1
答案 0 :(得分:2)
您没有正确关闭create
方法。它应该是:
class RecipesController < ApplicationController
def create
@recipe = Recipe.new(recipe_params)
if @recipe.save
redirect_to @recipe, notice: "Successfully created"
else
render 'new'
end
end
private
def recipe_params
params.require(:recipe).permit(:title, :serving, :prep, :cook)
end
end