Ruby on rails Rails 4嵌套强参数引发未经允许的参数错误

Ruby on rails Rails 4嵌套强参数引发未经允许的参数错误,ruby-on-rails,strong-parameters,Ruby On Rails,Strong Parameters,我已经做了好几个小时了,但还是不明白: 尝试在新组织形式中创建新地址。可以为组织、公司或员工创建地址。我希望将与组织/公司/员工相关的地址id存储在他们的数据库记录中,但无法获得正常工作的强参数 模型/组织。rb 在我看来,地址没有被创建,因为它没有访问地址属性的权限,这些属性是被传递的,但是是“未被允许的参数”。即使我将其包括在组织许可声明中 我做错什么了吗 编辑1 根据hkumar的建议,我将视图/组织/_form.html.erb文件编辑为 但错误现在看起来像: Parameters:

我已经做了好几个小时了,但还是不明白:

尝试在新组织形式中创建新地址。可以为组织、公司或员工创建地址。我希望将与组织/公司/员工相关的地址id存储在他们的数据库记录中,但无法获得正常工作的强参数

模型/组织。rb

在我看来,地址没有被创建,因为它没有访问地址属性的权限,这些属性是被传递的,但是是“未被允许的参数”。即使我将其包括在组织许可声明中

我做错什么了吗

编辑1

根据hkumar的建议,我将
视图/组织/_form.html.erb
文件编辑为

但错误现在看起来像:

 Parameters: {"utf8"=>"✓", "authenticity_token"=>"T2344536dhfjkhkj3eb5g43glwy547fy5p2nbydddfr=", "organisation"=>{"name"=>"The company", "#<Address:0x00000003aa2690>_attributes"=>{"line1"=>"The Road", "line2"=>"", "line3"=>"", "line4"=>"", "postcode"=>"", "country"=>"Ireland"}, "phone"=>"01111111"}, "commit"=>"Create Organisation"}
Start of debug
Unpermitted parameters: #<Address:0x00000003aa2690>_attributes

End of debug
最终编辑

多亏了Paul Richter,以下代码现在可以工作了(适用于任何偶然发现它的人):

控制器/组织\u controller.rb

类组织控制器
该问题与以下表格中的这一行有关:

f.fields_for @address do |t|
控制器中的这一行(显示完整方法,因此我们都在同一页上):

问题是,您正在创建单独的
地址
组织
对象,就好像它们是独立的实体一样,但是由于
地址
属于
组织
(根据您的模型),
地址
实际上依赖于父
组织
,因此,应该正确地关联

相反,您希望执行以下操作:

def new
  @organisation = Organisation.new
  @organization.build_address
 ...
这将设置一个新的
地址
对象,该对象与其父
组织
关联

然后在表单中,将行的
字段更改为:

f.fields_for :address do |t|
这将调用
@organization
对象的
address
方法,而[insert rails magic]将使用
的命名约定接受
的嵌套属性

简而言之,您看到错误的原因是,虽然您确实正确地设置了强参数,但是一个单独的、断开连接的
地址
对象被传递给
f.fields\u for
这一事实阻止了表单字段的正确命名

编辑

现在表单已正确设置,请将
create
方法更改为如下所示:

def create
  @organisation = Organisation.new(organisation_params)
  @organisation.save
  respond_with(@organisation)
end
在下面的评论中,您提到了如何获取我正在创建的地址的id并将其存储在组织中。需要说明的是,此时没有地址的
id
,但是因为您已经在
组织
模型中为地址
行设置了
接受嵌套的属性,Rails将通过使用传入的
组织参数
散列中的
地址属性
参数,创建一个新的
地址
对象,该对象已经与
组织
关联


这一行(
organization.new(organization_params)
)为您创建了整个结构,只要参数符合预期的命名约定,它们现在应该这样做。

是因为在您的in#organization_params中允许address_属性:[之间没有空格:和][?我改变了一切,但它仍然不起作用。这是因为我的地址实际上不属于该组织。我是否设置了错误的地址模型?我这样做是因为我没有将组织id保留在地址模型中。相反,我将地址id保留在组织模型中。@AlD不,从它的声音来看,是模型协会选项设置正确。定义“不工作”;更改后表单现在做什么?@AlD Ah,这是一个完全不同的问题。您可以看到提交的参数现在命名正确。您遇到的问题是实际的错误消息(它说:“line1”列中的null值违反了非null约束).您可能在该列上有一个not null约束,并且您正在传递一个null值。我确实有。我很困惑如何获取我正在创建的地址的id并将其存储在组织中。虽然现在是凌晨2点,但我认为今晚已经足够了。明天是新的一天。感谢您的帮助!我告诉您了!非常感谢Paul。工作g现在很好
 Parameters: {"utf8"=>"✓", "authenticity_token"=>"T2344536dhfjkhkj3eb5g43glwy547fy5p2nbydddfr=", "organisation"=>{"name"=>"The company", "#<Address:0x00000003aa2690>_attributes"=>{"line1"=>"The Road", "line2"=>"", "line3"=>"", "line4"=>"", "postcode"=>"", "country"=>"Ireland"}, "phone"=>"01111111"}, "commit"=>"Create Organisation"}
Start of debug
Unpermitted parameters: #<Address:0x00000003aa2690>_attributes

End of debug
Started POST "/organisations" for 127.0.0.1 at 2014-12-12 01:35:13 +0000
Processing by OrganisationsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"T2344536dhfjkhkj3eb5g43glwy547fy5p2nbydddfr=", "organisation"=>{"name"=>"The company", "address_attributes"=>{"line1"=>"The road", "line2"=>"", "line3"=>"", "line4"=>"", "postcode"=>"", "country"=>"Ireland"}, "phone"=>"01111111"}, "commit"=>"Create Organisation"}
Start of debug
{"name"=>"The company", "phone"=>"01111111", "address_attributes"=>{"line1"=>"The road", "line2"=>"", "line3"=>"", "line4"=>"", "postcode"=>"", "country"=>"Ireland"}}
End of debug
   (0.6ms)  BEGIN
  SQL (0.9ms)  INSERT INTO "addresses" ("created_at", "updated_at") VALUES ($1, $2) RETURNING "id"  [["created_at", "2014-12-12 01:35:14.196724"], ["updated_at", "2014-12-12 01:35:14.196724"]]
PG::NotNullViolation: ERROR:  null value in column "line1" violates not-null constraint
DETAIL:  Failing row contains (46, null, null, null, null, null, null, 2014-12-12 01:35:14.196724, 2014-12-12 01:35:14.196724).
: INSERT INTO "addresses" ("created_at", "updated_at") VALUES ($1, $2) RETURNING "id"
   (0.3ms)  ROLLBACK
Completed 500 Internal Server Error in 57ms

ActiveRecord::StatementInvalid (PG::NotNullViolation: ERROR:  null value in column "line1" violates not-null constraint
DETAIL:  Failing row contains (46, null, null, null, null, null, null, 2014-12-12 01:35:14.196724, 2014-12-12 01:35:14.196724).
: INSERT INTO "addresses" ("created_at", "updated_at") VALUES ($1, $2) RETURNING "id"):
  app/controllers/organisations_controller.rb:29:in `create'
class OrganisationsController < ApplicationController
 espond_to :html, :json
 before_action :set_organisation, only: [:show, :edit, :update, :destroy]

 def new
    @organisation = Organisation.new
    @organisation.build_address
 end


 def create
  @organisation = Organisation.new(organisation_params)
  @organisation.save
  respond_with(@organisation)
 end

 private
 def set_organisation
  @organisation = Organisation.find(params[:id])
 end

 def organisation_params
   params.require(:organisation).permit(:name, :phone, :creator, :contact, :renewal, :approved, :balance, address_attributes:[:line1, :line2, :line3, :line4, :postcode, :country, :organisation_id])
 end
end
f.fields_for @address do |t|
def new
  @organisation = Organisation.new
  @address = Address.new  # This line, specifically
  respond_with(@organisation)
end
def new
  @organisation = Organisation.new
  @organization.build_address
 ...
f.fields_for :address do |t|
def create
  @organisation = Organisation.new(organisation_params)
  @organisation.save
  respond_with(@organisation)
end