简体   繁体   English

参数数量错误(0代表2..3)

[英]Wrong number of arguments( 0 for 2..3)

I want to refactor a string, which I receive as a parameter, into an array of words (using the split method). 我想将作为参数接收的字符串重构为单词数组(使用split方法)。

My Test model has one attribute, called source . 我的Test模型具有一个名为source属性。

class Test < ActiveRecord::Base
  attr_accessible :source
  serialize :sources, Array
  after_create do
    test.source.split(' ')
  end
end

This returns an error: 这将返回错误:

wrong number of arguments (0 for 2..3) 参数数目错误(0代表2..3)

I can't figure out what arguments Rails wants. 我不知道Rails想要什么参数。

UPD UPD

And if I change my code like this: 如果我像这样更改代码:

class Test < ActiveRecord::Base
  attr_accessible :source
  serialize :sources, Array
  def split_this_text(test_id)
    @test = Test.where(:test_id=>test_id)
    @test.source.split(' ')
  end
end

and call method in tests_controller/create: 并在tests_controller / create中调用方法:

 @test.split_this_text(:id)

Than I get this error: 比我得到这个错误:

NoMethodError in TestsController#create

undefined method `split' for #<Arel::Nodes::JoinSource:0x4ddfc60>

UPD#2 UPD#2

Finally works works without any mistakes, but behaves like nothing works and source usuall string (for example @test.source[0] returns a letter) 最终工作正常,但没有任何错误,但行为却无济于事,并且源代码通常为字符串(例如@test.source[0]返回一个字母)

class Test < ActiveRecord::Base
  attr_accessible :source
  serialize :sources, Array
  before_save :split_this_text
  def split_this_text
    self.source.split(' ')
 end
end

There is a possibility that the word "test" is reserved by rails in some way. rails可能以某种方式保留了“ test”一词。

One of my model's attribute names was "test". 我模型的属性名称之一是“测试”。 When I tried to call instance.update_attributes(:test => SOMEVALUE) , it would return the same 'wrong number of arguments (0 for 2..3)' message you were getting. 当我尝试调用instance.update_attributes(:test => SOMEVALUE) ,它将返回与您得到的相同的“参数数目错误(2..3为0)”消息。

When I changed the name of the attribute from "test" to something else, the error disappeared. 当我将属性名称从“ test”更改为其他名称时,错误消失了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过 :test 导致 ArgumentError: 参数数量错误(给定 0,预期为 2..3) - Passing :test caused ArgumentError: wrong number of arguments (given 0, expected 2..3) 工厂女工说ArgumentError:参数数量错误(1代表2..3) - Factory Girl says ArgumentError: wrong number of arguments (1 for 2..3) mongoid 枚举 ArgumentError:参数数量错误(给定 1,预期为 2..3) - mongoid enum ArgumentError: wrong number of arguments (given 1, expected 2..3) Ruby - 找不到ArgumentError的原因:参数个数错误(给定0,预期2..3) - Ruby - can't find cause for ArgumentError: wrong number of arguments (given 0, expected 2..3) Admin :: ProductsController#中的ArgumentError创建错误的参数数量(给定1,预期2..3) - ArgumentError in Admin::ProductsController#create wrong number of arguments (given 1, expected 2..3) Rails-BookingsController#中的ArgumentError创建错误的参数数量(给定1,预期2..3) - Rails - ArgumentError in BookingsController#create wrong number of arguments (given 1, expected 2..3) ArgumentError:参数数量错误 - ArgumentError: wrong number of arguments ArgumentError:参数数量错误(1表示2) - ArgumentError: wrong number of arguments (1 for 2) rspec中的参数数目错误(1代表0) - wrong number of arguments (1 for 0) in rspec 参数数目错误(1代表0) - wrong number of arguments (1 for 0)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM