I need to create a campaign with given prizes. My models already are related and accepting nested attributes.
View:
<%= form_for @campaign, remote: true do |f| %>
<% 5.times do |i| %>
<%= f.fields_for :prizes do |prize_form| %>
<div class="form-group">
<%= prize_form.label "prize #{i + 1}" %>
<%= prize_form.text_field :name %>
</div>
<% end %>
<% end %>
<% end %>
Which generates:
<input id="campaign_prizes_attributes_0_name" name="campaign[prizes_attributes][0][name]" type="text">
<input id="campaign_prizes_attributes_1_name" name="campaign[prizes_attributes][1][name]" type="text">
<input id="campaign_prizes_attributes_2_name" name="campaign[prizes_attributes][2][name]" type="text">
<input id="campaign_prizes_attributes_3_name" name="campaign[prizes_attributes][3][name]" type="text">
<input id="campaign_prizes_attributes_4_name" name="campaign[prizes_attributes][4][name]" type="text">
In my controller I have this
class CampaignsController < ApplicationController
respond_to :html, :js
def index
@campaigns = Campaign.all
end
def new
@campaign = Campaign.new
@campaign.prizes.build
end
def create
@campaign = Campaign.new(campaign_params)
@campaign.prizes.build
end
def campaign_params
params.require(:campaign).permit(:name, :date_start, :date_end, :status, :rules, prizes_attributes: [name: []])
end
end
No matter what I do, I always get this error:
Unpermitted parameters: name
I need to make each campaign have a varying ammount of prizes, but I'm not able to make this work. What am I doing wrong?
Thanks.