Reputation: 2666
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>>
Upvotes: 0
Views: 433
Reputation: 511
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
Upvotes: 2