Adam12344
Adam12344

Reputation: 1053

Fields_for with permitted attributes

I have a form in Rails that looks like this psuedo code:

<%= form_for :project do |f| %>
  <%= f.fields_for :rewards do |r| %>
    <%= r.text_field :title %>

I have everything working except when I submit the form I get an error: Unpermitted parameter: rewards. What should my permissions look like in the Project controller?

Upvotes: 0

Views: 248

Answers (1)

max
max

Reputation: 102134

Pass the nested attributes as a hash option with an array of permitted attributes:

require(:project).permit(
  :foo, :bar, 
  rewards_attributes: [:title]
)

This is equivalent to:

require(:project).permit(
  :foo, :bar, 
  { rewards_attributes: [:title] }
)

Upvotes: 2

Related Questions