[英]ArgumentError (wrong number of arguments (given 5, expected 1)) in Rails
I have a Scaffold called contracts and a Model called Addresses.我有一个叫做合同的脚手架和一个叫做地址的 Model。 I want to add call my model in the scaffold.
我想在脚手架中添加调用我的 model 。 in the Contrato.rb (Model) I have:
在 Contrato.rb(模型)中,我有:
class Contrato < ApplicationRecord
has_many :addresses
accepts_nested_attributes_for :addresses, reject_if: lambda { |attrs| attrs['estado', 'cidade', 'bairro', 'endereco', 'cep'].blank?}
before_save do
self.paf.gsub!(/[\[\]\"]/, "") if attribute_present?("paf")
end
end
When I try to create a Contract using the console using: Contrato.create:(razao, "Carrefour Comercial": cpnj, "12141618": insc_estadual, "12345": insc_municipal, "56789": paf, "": empresa, "Carrefour": addresses_attributes:[{estado, "Sc": cidade, "santos": bairro, "myborough": endereco, "avenue": cep: "112233"})
The rails controller returns:当我尝试使用控制台创建合同时:
Contrato.create:(razao, "Carrefour Comercial": cpnj, "12141618": insc_estadual, "12345": insc_municipal, "56789": paf, "": empresa, "Carrefour": addresses_attributes:[{estado, "Sc": cidade, "santos": bairro, "myborough": endereco, "avenue": cep: "112233"})
轨道 controller 返回:
Traceback (most recent call last):
2: from (irb):8
1: from app/models/contrato.rb:3:in `block in <class:Contrato>'
ArgumentError (wrong number of arguments (given 5, expected 1))
I'm clueless about this error.我对这个错误一无所知。
I solved adding我解决了添加
def contrato_params
params.require(:contrato).permit(:razao, :cpnj, :insc_estadual,:insc_municipal, :empresa, paf:[], addresses_attributes:[:estado, :cidade, :bairro, :endereco, :cep])
end
The addresses_attributes to the contracts controller.合约 controller 的地址属性。
In above code在上面的代码中
class Contrato < ApplicationRecord
has_many :addresses
accepts_nested_attributes_for :addresses, reject_if: lambda { |attrs| attrs['estado', 'cidade', 'bairro', 'endereco', 'cep'].blank?}
before_save do
self.paf.gsub!(/[\[\]\"]/, "") if attribute_present?("paf")
end
end
At line of accepts_nested_attributes_for
it should be like在
accepts_nested_attributes_for
行它应该像
accepts_nested_attributes_for :addresses, reject_if: lambda { |attrs| attrs.any? { |k, v| ['estado', 'cidade', 'bairro', 'endereco', 'cep'].include?(k) && v.blank? } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.