简体   繁体   English

Rails 中的 ArgumentError(arguments 的编号错误(给定 5,预期为 1))

[英]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 likeaccepts_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.

相关问题 Rails-ArgumentError(参数数量错误(给定1,预期为0)): - Rails - ArgumentError (wrong number of arguments (given 1, expected 0)): Rails-ArgumentError给定的参数个数错误3,预期为2 - Rails - ArgumentError wrong number of arguments given 3, expected 2 Rails 中止了! ArgumentError:参数数量错误(给定 0,预期为 1..2) - Rails aborted! ArgumentError: wrong number of arguments (given 0, expected 1..2) Rails Active Storage “附加” ArgumentError(arguments 的编号错误(给定 0,预期为 1)): - Rails Active Storage “attach” ArgumentError (wrong number of arguments (given 0, expected 1)): Rails ActiveRecord:ArgumentError:arguments 的编号错误(给定 1,预期为 0) - Rails ActiveRecord: ArgumentError: wrong number of arguments (given 1, expected 0) RSpec 3.6和Rails 5.0.1-ArgumentError:参数数量错误(给定0,应为1) - RSpec 3.6 and Rails 5.0.1 - ArgumentError: wrong number of arguments (given 0, expected 1) FactoryGirl ArgumentError:参数数量错误(给定1,预期为0) - FactoryGirl ArgumentError: wrong number of arguments (given 1, expected 0) ArgumentError:参数数量错误(给定0,应为1..2) - ArgumentError: wrong number of arguments (given 0, expected 1..2) ArgumentError(参数数量错误(给定 1,预期为 2)) - ArgumentError (wrong number of arguments (given 1, expected 2)) ArgumentError(arguments 的编号错误(给定 2,预期 0..1) - ArgumentError (wrong number of arguments (given 2, expected 0..1)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM