![](/img/trans.png)
[英]Retrieving form params in Rails 4 controller using fields_for and accepts_nested_attributes_for
[英]Rails params attributes - using the params fields in the controller
我的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 %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.