wweare
wweare

Reputation: 241

Unpermitted parameters error

I am trying to add a nested form in my app, but shell shows me an error:

Unpermitted parameter: description

there is my model

class Chart < ActiveRecord::Base
  has_many :descriptions
  accepts_nested_attributes_for :descriptions
end

There is strong params in my controller:

  def chart_params
    params.require(:chart).permit(:title, descriptions_attributes: [:name])
  end

This is my form:

= form_for @chart do |f|
  = f.text_field :title
  = f.fields_for :descriptions do |d|
    = d.text_field :name
  = f.submit

What am I doing wrong?

upd

I was change form and action new in controller

form

 = form_for @chart do |f|
    = f.text_field :title
    = f.fields_for @descriptions do |d|
      = d.text_field :name
    = f.submit

action new

  def new
    @chart = Chart.new
    @descriptions = @chart.descriptions.build
  end

Upvotes: 4

Views: 649

Answers (1)

sufinsha
sufinsha

Reputation: 765

The name attribute for the text_field 'name' should be 'chart[descriptions_attributes][name]'.

Please check the code snippet below:

= form_for @chart do |f|
  = f.text_field :title
  = f.fields_for :descriptions_attributes, @descriptions do |d|
    = d.text_field :name
  = f.submit

No changes required for the new action of controller.

Upvotes: 1

Related Questions