私はネストされたsimple_formを持っており、私はgemの繭を使用しています。私はチャレンジモデルを持っています。これはchallenge.rbにあります:accepts_nested_attributes_for :prizes
そして挑戦has_many
賞があります。フォームを送信すると、エラーが表示されます。繭のネストされたフォーム:許可の問題
Unpermitted params: prizes
強力なパラメータで許可しても、以前は繭を使用していましたが、問題を見つけることができず、私の髪を引っ張っています。私challenges_controller.rbで
:また
def create
@challenge = Challenge.create(challenge_params)
@challenge.user = current_user
authorize @challenge
if params["challenge"]["prizes_attributes"]
for k,v in params["challenge"]["prizes_attributes"]
@prize = Prize.create
@prize.challenge = @challenge
@prize.title = v.values[0].values[0]
@prize.save
end
end
if @challenge.save
redirect_to challenge_path(@challenge)
else
render :new
end
end
def challenge_params
params.require(:challenge).permit(:title,
:banner,
:user,
:start_date,
:end_date,
:tagline,
:slug,
:rules,
:category_id,
:organization,
prizes_attributes: [:id, :title])
end
、私の形での挑戦を作成するには:
<%= simple_form_for @challenge do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :title, :label => "Titre" %>
<%= f.input :banner, :label => "Bannière", as: :attachinary %>
<%= f.input :tagline, :label => "Tagline" %>
<%= f.input :organization, :collection => @organizations,:label => "Organization", :include_blank => true%>
<%= f.input :rules, :label => "Lien vers le règlement" %>
<%= f.input :start_date, as: :string, input_html: {type: :date}, :label => "Date de début" %>
<%= f.input :end_date, as: :string, input_html: {type: :date}, :label => "Date de fin" %>
<%= f.input :category_id, :collection => @ancestors,:label => "Catégorie", :include_blank => true%>
<%= f.simple_fields_for :prizes do |p| %>
<%= render 'prize_fields', f: p %>
<% end %>
<%= link_to_add_association 'Ajouter un prix', f, :prizes %>
</div>
<div class="form-actions text-center">
<%= f.button :submit, "Soumettre", :class => "btn btn-primary" %>
</div>
<% end %>
そして、私のprizes_fieldsで部分:
<li>
<div class='row'>
<%= f.fields_for :prizes do |p| %>
<div class='col-xs-12 col-md-6'>
<div class="materiel-wrapper">
<%= p.text_field :title, class:"form-control", placeholder:"Titre" %>
</div>
</div>
<% end %>
</div>
</li>
誰が見るんが私はどこでミスをしたのですか?
お時間をありがとうと
ありがとうございます!しかし、私がf.simple_fields_forを削除すると、prize do do | p |部分的に、私は "定義されていないメソッドtext_field for nil"を持っています –
@GrahamSlickそれはあなたが 'p'を呼び出しているからですが、部分的に' f'( '<%= render 'prize_fields'、f:p %> ')。あなたの部分を 'f.text_field'に変更してください。 – Matt
ありがとうございました! –