0
votes

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.

1
The problem is in your form. That's how it sends data. If you want params to have another shape, change the form.Sergio Tulentsev
Thanks a lot for the helpuser7671402

1 Answers

1
votes

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