0
recipe_ingredientsからhas_many recipe_ingredientsとhas_many ingredientsを持つレシピがあります。レシピコントローラのrecipe_ingredientsテーブルのレコードを編集しようとしています。私が持っているフォームでは、編集のためのすべてのフィールドがリストされています(私はただ一つだけを必要とします)。もう一つは、レコードを更新していないということです。あなたがこの問題に出くわすことができる光のために、事前にありがとう。ここに編集フォームがあります。そのテーブルの結合テーブルのレコードを編集しています
<%= form_for @recipe, remote: true do |recipe_form| %>
<%= recipe_form.fields_for :recipe_ingredients do |f| %>
<%= f.label :amount %>
<%= f.number_field :amount, step: :any %>
<%= f.submit %>
<% end %>
<% end %>
およびコントローラ。
def edit
@recipe = Recipe.find(params[:id])
@recipe_ingredients = @recipe.recipe_ingredient
end
def update
@recipe = Recipe.find(params[:id])
@recipe.recipe_ingredients = RecipeIngredient.where(recipe_id: params[:id])
end
private
def recipe_params
params.require(:recipe).permit(:name, :recipe_ingredient_ids => [], :ingredient_ids => [])
end
レコードが更新されないという問題はおそらく、recipe_paramsメソッドの結果です。親レコードを使用して子レコードを更新するには、子レコードのどの属性を許可するかを指定する必要があります。あなたのスキーマを表示することができれば、私はそれを手伝ってくれるでしょう。 – moveson