0

I've tried the solutions on this other stack overflow question but they aren't working.

I'm getting this error when submitting my form: Unpermitted parameter: organization_required_fields

Any help would be appreciated.

I have the following models:

class Organization < ActiveRecord::Base
  belongs_to :user
  has_many :organization_required_fields
  has_many :fields, through: :organization_required_fields

  accepts_nested_attributes_for :organization_required_fields, allow_destroy: true
end

class OrganizationRequiredField < ActiveRecord::Base
  belongs_to :organization
  belongs_to :field
end

class Field < ActiveRecord::Base
  has_many :organization_required_fields
  has_many :organizations, through: :organization_required_fields
end

My controller:

def update
...
  @organization.update(organization_params)
...
end

private
def set_organization
  @organization = Organization.find_by_id(params[:id])
  ...
end

def organization_params
  params.require(:organization).permit(:name, :user_id, organization_required_fields_attributes: [:id, :organization_id, :field_id, :_destroy])
end

My form view

...
= f.select :organization_required_fields, options_for_select(@fields.collect {|rf| [ rf.name.titleize, rf.id ] }, @organization.fields.collect{ |orf| orf.id }),{ :prompt => "Please select"},{ :multiple => true, :size => 15 }
...
Chris
  • 4,643
  • 6
  • 31
  • 49
  • I don't really get why you would add a separate `OrganizationRequiredField` model and association unless the definition of the fields attached to an organization are not a developer concern (not defined by the db schema) and must be user editable. – max Nov 16 '18 at 23:48
  • That is why i added the model. They are user edited. – Chris Nov 18 '18 at 02:59

2 Answers2

0

I actually changed a lot by following this rails cast

I also had to change the organization_params to

params.require(:organization).permit(:name, :user_id, { field_ids: []})

Chris
  • 4,643
  • 6
  • 31
  • 49
0

You need to use fields_for in your form.

Dorian
  • 22,759
  • 8
  • 120
  • 116