我正在尝试从Rails中的一个嵌套表单创建两个对象,recipe
has_many :ingredients
。
当我提交表单时,Rails会返回错误:
undefined method `ingredient' for #<Ingredient id: nil, ing: "a", amount: "a", recipe_id: nil> Did you mean? increment
专门指出我的创建操作中的错误:
@recipe = Recipe.new(recipe_params)
# @recipe.user_id = session[:user_id]
@recipe.save
redirect_to recipe_path(@recipe)
end
我怀疑我的问题出在模型关联中。
完整追踪:
Started POST "/recipes" for ::1 at 2016-12-19 13:53:06 -0600
Processing by RecipesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"wmdURAmdmdHrbrW3+0z5MvEIOs7hY5QBfzKG/L1PSgFKJPRHbFQlGMpBYeIwYdnRxPprsEKk1HT7qQNbSOo42Q==",
"recipe"=>{"name"=>"adsfadsfa",
"steps"=>"a",
"ingredients_attributes"=>{"0"=>{"ing"=>"a", "amount"=>"a"}}},
"commit"=>"Add"}
ActionController::Parameters {"name"=>"adsfadsfa",
"steps"=>"a",
"ingredients_attributes"=>
<ActionController::Parameters
{"0"=><ActionController::Parameters
{"ing"=>"a", "amount"=>"a"} permitted: true>} permitted: true>} permitted: true>
(0.1ms) begin transaction
(0.1ms) rollback transaction
Completed 500 Internal Server Error in 47ms (ActiveRecord: 0.3ms)
app/controllers/recipes_controller.rb:13:in `create'
Rendering /Users/JCamp1991/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout
Rendering /Users/JCamp1991/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
Rendered /Users/JCamp1991/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (11.0ms)
Rendering /Users/JCamp1991/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
Rendered /Users/JCamp1991/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (5.6ms)
Rendering /Users/JCamp1991/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
Rendered /Users/JCamp1991/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (3.6ms)
Rendered /Users/JCamp1991/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (153.7ms)
Rendering /Users/JCamp1991/.rvm/gems/ruby-2.3.1/gems/web-console-3.1.1/lib/web_console/templates/index.html.erb
Rendered /Users/JCamp1991/.rvm/gems/ruby-2.3.1/gems/web-console-3.1.1/lib/web_console/templates/_markup.html.erb (1.0ms)
Rendering /Users/JCamp1991/.rvm/gems/ruby-2.3.1/gems/web-console-3.1.1/lib/web_console/templates/console.js.erb within layouts/javascript
Rendering /Users/JCamp1991/.rvm/gems/ruby-2.3.1/gems/web-console-3.1.1/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string
Rendered /Users/JCamp1991/.rvm/gems/ruby-2.3.1/gems/web-console-3.1.1/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.9ms)
Rendering /Users/JCamp1991/.rvm/gems/ruby-2.3.1/gems/web-console-3.1.1/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string
Rendered /Users/JCamp1991/.rvm/gems/ruby-2.3.1/gems/web-console-3.1.1/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (1.0ms)
Rendering /Users/JCamp1991/.rvm/gems/ruby-2.3.1/gems/web-console-3.1.1/lib/web_console/templates/style.css.erb within layouts/inlined_string
Rendered /Users/JCamp1991/.rvm/gems/ruby-2.3.1/gems/web-console-3.1.1/lib/web_console/templates/style.css.erb within layouts/inlined_string (1.0ms)
Rendered /Users/JCamp1991/.rvm/gems/ruby-2.3.1/gems/web-console-3.1.1/lib/web_console/templates/console.js.erb within layouts/javascript (116.1ms)
Rendering /Users/JCamp1991/.rvm/gems/ruby-2.3.1/gems/web-console-3.1.1/lib/web_console/templates/main.js.erb within layouts/javascript
Rendered /Users/JCamp1991/.rvm/gems/ruby-2.3.1/gems/web-console-3.1.1/lib/web_console/templates/main.js.erb within layouts/javascript (0.9ms)
Rendered /Users/JCamp1991/.rvm/gems/ruby-2.3.1/gems/web-console-3.1.1/lib/web_console/templates/index.html.erb (229.6ms)
recipes_controller.rb:
...
def create
@recipe = Recipe.new(recipe_params)
@recipe.user_id = session[:user_id]
@recipe.save
redirect_to recipe_path(@recipe)
end
...
def recipe_params
params.require(:recipe).permit(:name, :steps, ingredients_attributes: [:ing, :amount])
end
型号:
class Ingredient < ApplicationRecord
belongs_to :recipe
...
end
class Recipe < ApplicationRecord
...
has_many :ingredients
accepts_nested_attributes_for :ingredients
validates_associated :ingredients
...
end
新食谱表格:
<%= form_for @recipe do |f| %>
<h1>
<%= f.label :name %><br>
<%= f.text_field :name %><br>
</h1>
<h1>
<%= f.label :steps %><br>
<%= f.text_area :steps %><br>
</h1>
<%= f.fields_for :ingredients do |p| %>
<h1>
<%= p.label :ing %><br>
<%= p.text_area :ing %><br>
</h1>
<h1>
<%= p.label :amount %><br>
<%= p.text_area :amount %><br>
</h1>
<% end %>
<%= f.submit "Add" %><br>
包含在routes.rb中:
resources :users do
resources :recipes
end
resources :recipes do
resources :ingredients
end
整个项目的Github Repo:
答案 0 :(得分:0)
Ingredient
模型可能不应具有ingredient
属性。那么name
呢?
迁移之间似乎也不一致
t.string :ing
和型号:
validates :ingredient, presence: true