2016-07-30 13 views
0

私は小さなレシピのウェブサイトを構築しようとしています。しかし、親(レシピ)アクションで子(recipe_ingredients)テーブルを保存するためにネストされた属性を使用しようとすると、それは私のためのIDを生成するだけで、その中にデータがありません。誰でも助けてくれますか?レシピのレールのネストされた属性

モデル:recipe_ingredientsため

class Recipe < ActiveRecord::Base 
    has_many :recipe_ingredients 
    accepts_nested_attributes_for :recipe_ingredients, allow_destroy: true 
end 

モデル:レシピの

class RecipeIngredient < ActiveRecord::Base 
    belongs_to :recipe, inverse_of: :recipe_ingredients 
end 

コントローラー:

def create 
    @recipe = Recipe.new(recipe_params) 
    @recipe.recipe_ingredients.build 
    ***binding.pry*** 

if @recipe.save 
    render json: @recipe, status: :created, location: @recipe 
else 
    render json: @recipe.errors, status: :unprocessable_entity 
end 
end 

def recipe_params 
    params.require(:recipes) 
    .permit(:name, :category, instruction: [], recipe_ingredients_attributes: [:amount, :ingredient, :measure]) 
end 

`` `

私はレールコンソールで確認した後、 、 じぶんのrecipe_paramsは以下のようになります

[7] pry(#<RecipesController>)> recipe_params Unpermitted parameter: recipe_ingredients => {"name"=>"an example recipe", "category"=>"fry", "instructions"=>["do it", "ignore it"]} "Unpermitted parameter"問題を解決する方法がわかりません。助けてください〜ありがとう〜

+0

更新:上記の問題が解決しました。私のカールの要求が原因です。私は自分のデータに "recipe_ingredients_attributes"を指定しなかった。しかし、レールは、recipe_ingredientsテーブルのデータと1行の空のデータの保存に役立ちます。 – tina

答えて

0

あなたのrecipe_paramsメソッドのparams.require(:recipes)をparams.require(:recipe)に変更してみてください お役に立ちますか?

+0

ありがとう〜私はカールの中に "recipe_ingredients_attributes"を指定するのを忘れています。問題解決〜もし可能であれば、ネストされた属性質問の別の質問を私にチェックアウトしてもよろしいですか?多くのありがとう〜http://stackoverflow.com/questions/38679639/update-nested-attributes-in-rails – tina

関連する問題