提交表单时出错:嵌套属性不允许的参数

时间:2018-11-16 19:44:13

标签: ruby-on-rails activerecord

我已经尝试过this other stack overflow question上的解决方案,但是它们不起作用。

提交表单Unpermitted parameter: organization_required_fields

时出现此错误

任何帮助将不胜感激。

我有以下型号:

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

我的控制器:

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

我的表单视图

...
= 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 }
...

2 个答案:

答案 0 :(得分:0)

通过遵循此rails cast

,我实际上做了很多改变

我还必须将organization_params更改为

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

答案 1 :(得分:0)

您需要在表单中使用fields_for