0
與simple_form嵌套的has_many關聯返回以下PARAMS散列:嵌套形式simple_form和強參數
params
# => {
"user"=>{
"first_name"=>"John",
"last_name"=>"Doe",
"bank_accounts_attributes"=>{
"-1"=>{
"_destroy"=>"0",
"iban"=>"fakeiban",
"bic"=>"fakebic"
},
"new-bank-account"=>{
"_destroy"=>"0",
"iban"=>"",
"bic"=>""
}
}
}
}
關鍵的「-1」是通過複製創建新的,目前還沒有持久的bank_accout「新 - 銀行賬戶「模板(由於iban爲空白,因此被忽略)。
我該如何准許這些強參數?
我已經嘗試了以下無濟於事:
permitted_params = {
:first_name,
:last_name,
{
:bank_accounts_attributes=>[:iban, :bic]
}
}
params.require(:user).permit(*permitted_params)
Unpermitted parameters: -1, new-bank-account
# => {
"user"=>{
"first_name"=>"John",
"last_name"=>"Doe",
"bank_accounts_attributes"=>{}
}
}
什麼我錯在這裏做什麼?
UPDATE:
下工作,但我不想列入負面鍵(其代表非持久的關係)到處明確:
params.require(:user).permit(:first_name, :last_name, bank_accounts_attributes: {"-1" => [:iban, :bic]})
更新2:
問題似乎是「新銀行帳號」鍵:
p = ActionController::Parameters.new user: { first_name: "Foo", bank_accounts_attributes: {"-1" => {iban: 'xxx'}, "-2" => {iban: 'yyy'}}}
p.require(:user).permit(:first_name, bank_accounts_attributes: :iban)
# => {"first_name"=>"Foo", "bank_accounts_attributes"=>{"-1"=>{"iban"=>"xxx"}, "-2"=>{"iban"=>"yyy"}}}
p = ActionController::Parameters.new user: { first_name: "Foo", bank_accounts_attributes: {"-1" => {iban: 'xxx'}, "new-bank-account" => {iban: 'yyy'}}}
p.require(:user).permit(:first_name, bank_accounts_attributes: :iban)
# => {"first_name"=>"Foo", "bank_accounts_attributes"=>{}}
看來我必須在提交表單之前從DOM中刪除模板「new-bank-account」。
您的用戶模型中是否有'accep_nested_attributes_for:bank_accounts'? – oxtub
是的,我正在加入Rails 4,並在Rails 3上工作。 – svoop