![](/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.