简体   繁体   中英

Rails params attributes - using the params fields in the controller

I have the following strong params in my developers_controller named location_params

  def location_params
    params.require(:location).permit(:country, {:ads_attributes => [:remote, :days]})
  end

When I set a debug breakpoint in the code, to test on how to use the fields :remote and :days, I have to use the following code to get :remote and :days in the console .

location_params[:ads_attributes]["2"][:remote]

I do not understand and know how to remove this ["2"] index. I would like to select the params with the following code location_params[:ads_attributes][:remote]

This is the result in the console from 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>

I did make a search on stackoverflow and I could not find a solution to this specific issue with the index.

I would recomment better to fix form view, which add these numbers in here:

<%= 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 %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM