のhas_many :ingredients
という2つのオブジェクトをRailsの1つのネストされたフォームから作成しようとしています。ネストされたフォームの子オブジェクトのNoMethodError
私がフォームを送信すると、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のレポ:
https://github.com/jlcampbell1991/recipe-box
あなたは、エラーの前に 'P recipe_params' 1行を追加してもらえて、出力を見せてください。 –
絶対に。完了! – jlcampbell1991