Rails 4 允许使用动态键的嵌套散列
Rails 4 permit nested hash with dynamic keys
在允许参数中包含动态键的嵌套哈希时遇到问题。我已经提到了关于堆栈溢出的其他类似问题,但到目前为止还没有运气。任何帮助是极大的赞赏。以下是引发的错误。
ActionController::UnpermittedParameters 发现不允许的参数:volunteers_slots_attributes、sign_up_slots_attributes
{"event"=>{
"name"=>"ss",
"volunteers_slots_attributes"=>{
"0"=>{"_destroy"=>"false", "needed_count"=>""},
"1"=>{"_destroy"=>"false", "needed_count"=>""},
...
},
"sign_up_slots_attributes"=>{
"0"=>{"_destroy"=>"false", "title"=>"", "quantity"=>""},
"1"=>{"_destroy"=>"false", "title"=>"", "quantity"=>""},
"2"=>{"_destroy"=>"false", "title"=>"", "quantity"=>""},
....
},
"supplies_note"=>""}}
控制器:
def event_params
params[:event].permit(:name,:supplies_note,
:volunteers_slots_attributes,
:sign_up_slots_attributes)
end
事件模型:
accepts_nested_attributes_for :sign_up_slots, allow_destroy: true, reject_if: :all_blank
accepts_nested_attributes_for :volunteers_slots, allow_destroy: true, reject_if: :all_blank
您需要确保 event.rb
模型文件接受嵌套属性。
在您的情况下,它应该包含如下几行:
accepts_nested_attributes_for :volunteers_slots, reject_if: :all_blank, allow_destroy: true
accepts_nested_attributes_for :sign_up_slots, reject_if: :all_blank, allow_destroy: true
# ...
您总是可以使用 params[:event].permit(:name: volunteers_slots_attributes: ['0', '1'])
之类的东西来允许嵌套属性。
你必须像
一样改变你的强参数
params.require(:event).permit(
:name,
:supplies_note,
volunteers_slots_attributes: [
:_destroy,
:needed_count
],
sign_up_slots_attributes: [
:_destroy,
:title,
:quantity
]
)
在允许参数中包含动态键的嵌套哈希时遇到问题。我已经提到了关于堆栈溢出的其他类似问题,但到目前为止还没有运气。任何帮助是极大的赞赏。以下是引发的错误。
ActionController::UnpermittedParameters 发现不允许的参数:volunteers_slots_attributes、sign_up_slots_attributes
{"event"=>{
"name"=>"ss",
"volunteers_slots_attributes"=>{
"0"=>{"_destroy"=>"false", "needed_count"=>""},
"1"=>{"_destroy"=>"false", "needed_count"=>""},
...
},
"sign_up_slots_attributes"=>{
"0"=>{"_destroy"=>"false", "title"=>"", "quantity"=>""},
"1"=>{"_destroy"=>"false", "title"=>"", "quantity"=>""},
"2"=>{"_destroy"=>"false", "title"=>"", "quantity"=>""},
....
},
"supplies_note"=>""}}
控制器:
def event_params
params[:event].permit(:name,:supplies_note,
:volunteers_slots_attributes,
:sign_up_slots_attributes)
end
事件模型:
accepts_nested_attributes_for :sign_up_slots, allow_destroy: true, reject_if: :all_blank
accepts_nested_attributes_for :volunteers_slots, allow_destroy: true, reject_if: :all_blank
您需要确保 event.rb
模型文件接受嵌套属性。
在您的情况下,它应该包含如下几行:
accepts_nested_attributes_for :volunteers_slots, reject_if: :all_blank, allow_destroy: true
accepts_nested_attributes_for :sign_up_slots, reject_if: :all_blank, allow_destroy: true
# ...
您总是可以使用 params[:event].permit(:name: volunteers_slots_attributes: ['0', '1'])
之类的东西来允许嵌套属性。
你必须像
一样改变你的强参数
params.require(:event).permit(
:name,
:supplies_note,
volunteers_slots_attributes: [
:_destroy,
:needed_count
],
sign_up_slots_attributes: [
:_destroy,
:title,
:quantity
]
)