带有嵌套属性的Rails App5上的“ ActiveModel :: ForbiddenAttributesError”

问题描述

我有一个simple_form_for创建发票。通过此表单,我希望用户能够创建将与之前提到的发票相关联的客户端。当前的过程是首先创建客户,然后在用户创建发票时通过从已创建的客户集合中选择它来将其与发票关联。

我的模特:

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

发票控制器:

def new
    @invoice = Invoice.new
    @invoice.build_client
end 

def create
    @invoice = Invoice.new(invoice_params)
    @client = @invoice.build_client(params[:invoice][:client_attributes])
    @client.user = current_user
    @client.save
end

并且我确保使用以下命令更新Invoice Controller中的强大参数:

params.require(:invoice).permit(:param1,:param2,client_attributes:[:param1,:param3,etc..],..)

也就是说,在创建发票时,我遇到了“ ActiveModel :: ForbiddenAttributesError”,该属性设置为在未正确定义强参数时显示。就我而言,似乎并非如此。 我发现添加“ params.permit!”在“发票控制器”中的#Create中,我可以避免该错误。但这是一个把戏。没必要,因为那是强大参数的工作。请问有人遇到过类似案件吗?

解决方法

好,所以我知道了。所有需要做的就是-显然-在我的发票之前保存了我的客户。相当简单,不是吗!

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

import more_itertools
print(list(more_itertools.powerset([1,2,3,4])))
# [(),(1,),(2,(3,(4,2),3),4),4)]

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...