我的應用程序使用的是Rails 4.1.8,我使用簡單表單gem。 我在private
下強參數定義中得到了未定義的局部變量或方法錯誤,我不明白原因。Ruby on Rails - 未定義的局部變量或方法(強參數)
_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
我是新來的鐵軌,我相信這種事情可能會發生。我會盡可能地關閉/刪除這個問題。 – 2014-12-04 10:52:31