我有以下的强PARAMS在我developers_controller命名location_params
def location_params
params.require(:location).permit(:country, {:ads_attributes => [:remote, :days]})
end
当我在代码中设置调试断点时,为了测试如何使用字段 :remote 和 :days,我必须使用以下代码在控制台中获取:remote和:days。
location_params[:ads_attributes]["2"][:remote]
我不明白也不知道如何删除这个["2"]
索引。我想params
用以下代码选择location_params[:ads_attributes][:remote]
这是控制台中的结果 location_params[:ads_attributes]
<ActionController::Parameters
{"0"=><ActionController::Parameters {} permitted: true>,
"1"=><ActionController::Parameters {"days"=>"12"} permitted: true>,
"2"=><ActionController::Parameters {"remote"=>"1"} permitted: true>}
permitted: true>
我确实在 stackoverflow 上进行了搜索,但无法找到解决此特定索引问题的方法。
我建议更好地修复表单视图,在此处添加这些数字:
<%= link_to_add_association "Add another", f, :ads %>
<%= f.fields_for :ads do |ff| %>
<%= ff.form_group ::remote do |ad| %>
<%= ad.label :remote %> *
<%= ad.text_field :remote %>
<% end %>
<%= ff.form_group :days do |ad| %>
<%= ad.label :days %> *
<%= ad.text_field :days %>
<% end %>
<%= link_to_remove_association "remove", ff %>
<% end %>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句