0

I'm trying to submit multiple records in a single form in a rail 5.2 app. I'm not using accepts nested attributes of another model, as it always seemed to bring in the entries that were previously created. The entries can't be edited once created.

<%= form_tag entry_details_path do %>
  <% @entries.each do |entry| %>
    <%= fields_for 'entries[]', entry do |e| %>
      <div class="field">
        <%= e.label :user_account_id %><br>
        <%= e.text_field :user_account_id %>
      </div>
      <div class="field">
        <%= e.label :amount %><br>
        <%= e.text_field :amount %>
      </div>
     <% end %>
   <% end %>
   <div class="actions">
     <%= submit_tag %>
   </div>
 <% end %>

The params looks like:

"entries"=>[<ActionController::Parameters {"user_account_id"=>"3", "amount"=>"55"} permitted: false>, <ActionController::Parameters {"user_account_id"=>"4", "amount"=>"65"} permitted: false>]

I want to pash the entire array of hashes to a create such as:

def create_multiple_entries
  @entries = Entries.create(multiple_entries_params)
  @entries.reject! { |e| e.errors.empty? }
  if @entries.empty?
    redirect_to entries_path
  else
   render 'new'
 end
end

The method above works if manually enter an array. My challenge is the strong parameters(I think). Right now I have:

def leave_accrual_details_params
  params.require(:entries).permit([:user_account_id, :amount])
end

Which gives me the following error:

!! #<NoMethodError: undefined method `permit' for #<Array:0x00007fee4014ec78>>

1 Answer 1

2

Try the following for Rails 5:

def leave_accrual_details_params
  params.require(:entries).map do |p|
    p.permit(:user_account_id, :amount)
  end
end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.