我需要创建一个包含奖品的广告系列。我的模型已经相关并接受嵌套属性。
查看:
<%= 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 %>
生成:
<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">
在我的控制器中我有这个
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
无论我做什么,我总是会遇到这个错误:
Unpermitted parameters: name
我需要让每个广告系列都有不同的奖品,但我无法完成这项工作。我做错了什么?
感谢。
答案 0 :(得分:0)
campaign_params
方法应为:
def campaign_params
params.require(:campaign).permit(:name,
:date_start,
:date_end,
:status,
:rules,
prizes_attributes: [ :name ])
end
答案 1 :(得分:0)
您的campaign_params错误
prizes_attributes: [name: []]
必须是
prizes_attributes: [:name]