Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails &引用;ActiveModel::禁止属性错误 &引用;具有嵌套属性的Rails应用程序(5)_Ruby On Rails_Nested Attributes_Activemodel_Simple Form For - Fatal编程技术网

Ruby on rails &引用;ActiveModel::禁止属性错误 &引用;具有嵌套属性的Rails应用程序(5)

Ruby on rails &引用;ActiveModel::禁止属性错误 &引用;具有嵌套属性的Rails应用程序(5),ruby-on-rails,nested-attributes,activemodel,simple-form-for,Ruby On Rails,Nested Attributes,Activemodel,Simple Form For,我有一个创建发票的简单表单。通过这个表单,我希望用户能够创建一个与前面提到的发票关联的客户机。当前流程是首先创建客户机,然后在用户创建发票时从已创建的客户机集合中选择客户机,将其与发票关联 我的模型: class Client < ApplicationRecord has_many :invoices end class Invoice < ApplicationRecord belongs_to :client accepts_nested_attributes_f

我有一个创建发票的简单表单。通过这个表单,我希望用户能够创建一个与前面提到的发票关联的客户机。当前流程是首先创建客户机,然后在用户创建发票时从已创建的客户机集合中选择客户机,将其与发票关联

我的模型:

class Client < ApplicationRecord
  has_many :invoices
end 
class Invoice < ApplicationRecord
  belongs_to :client
  accepts_nested_attributes_for :client, reject_if: :all_blank, allow_destroy: true
end
我确保在Invoice Controller中使用以下命令更新我的强参数:

params.require(:invoice).permit(:param1, :param2,client_attributes:[:param1, :param2, :param3, etc..],..)
也就是说,在创建发票时,我遇到了一个“ActiveModel::ForbiddenAttributeError”,它被设置为在未正确定义强参数时出现。就我而言,情况似乎并非如此。
我发现在发票控制器的#Create中添加“params.permit!”可以避免该错误。但这是个骗局。这不应该是必要的,因为这是强参数的工作。请问,有没有人遇到过类似的情况?

好的,所以我想出来了。所有需要做的就是——显然——保存我的客户之前,我的发票。很简单,不是吗

这是我的最终发票#新建#创建

params.require(:invoice).permit(:param1, :param2,client_attributes:[:param1, :param2, :param3, etc..],..)
def new
    @invoice = Invoice.new
    @invoice.build_client
end 
def create
  @invoice = Invoice.new(invoice_params)
  @invoice.client.user = current_user
  @invoice.client.save

  if @invoice.save
    redirect_to @invoice
  else
    render :new
  end
end