1

特定の送信ボタンがクリックされたときにネストされた属性のみを更新しようとしていますが、それらのパラメーターのみを更新アクションに渡す方法がわかりません。

def update
  if params[:commit] == "Update Dogs"
    owner.update_attributes(params[dogs_attributes: [:id, :name, :tag]])
    render :show
  ...
  end
end

def owner_attributes
  params.fetch(:owner).permit(:id, :name, :address, dogs_attributes: [:id, :name, :tag])
end

変更はコミットされません。私も試してみました

 owner.update_attributes(dogs_attributes)

しかし、「未定義のメソッドdogs_attributes」というエラーが表示されます。owner_attributes に似た独自のメソッドとして定義しようとしましたが、ActionController は param :dog を取得できませんでした。上記のバリエーションをたくさん試しましたが、役に立ちませんでした。おそらく単純なエラーですが、これを正しく記述する方法がわかりません。何か案は?

PS。所有者は、犬のネストされた属性を受け入れます。

ありがとう!

4

2 に答える 2

2

update メソッドだけに別の set 属性を指定しないのはなぜですか?

def update
    if params[:commit] == "Update Dogs"
        Owner.update_attributes(update_attr)
    else
        Owner.update_attributes(owner_attr)
    end
end

private
def update_attr
    params.require(:owner).permit(:dog_attributes[:id, :name, :tag])
end

def owner_attributes
  params.fetch(:owner).permit(:id, :name, :address, dogs_attributes: [:id, :name, :tag])
end
于 2013-09-09T20:52:52.363 に答える
1

追加:

accepts_nested_attributes_for :dogs

あなたのOwnerモデルに

于 2013-09-09T08:11:10.547 に答える