2016-12-18 17 views
0

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

+0

あなたは、エラーの前に 'P recipe_params' 1行を追加してもらえて、出力を見せてください。 –

+0

絶対に。完了! – jlcampbell1991

答えて

0

Ingredientモデルは、おそらくingredient属性を持つべきではありません。 nameはどうですか?

また、移行

t.string :ing

とモデルの間で一貫していないようです:

validates :ingredient, presence: true

+0

はい、それは物事を一緒に動かしています! – jlcampbell1991

+0

'ルートが一致しません{:action =>" show "、:controller =>" recipes "、:id => nil}必須キーがありません:[:id]' – jlcampbell1991

+0

つまり、コントローラは 'recipe'を保存しません。' recipe_id'は 'recipe'が保存されるまで存在しません。 – jlcampbell1991