繁体   English   中英

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

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

我想将作为参数接收的字符串重构为单词数组(使用split方法)。

我的Test模型具有一个名为source属性。

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

这将返回错误:

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

我不知道Rails想要什么参数。

UPD

如果我像这样更改代码:

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

并在tests_controller / create中调用方法:

 @test.split_this_text(:id)

比我得到这个错误:

NoMethodError in TestsController#create

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

UPD#2

最终工作正常,但没有任何错误,但行为却无济于事,并且源代码通常为字符串(例如@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

rails可能以某种方式保留了“ test”一词。

我模型的属性名称之一是“测试”。 当我尝试调用instance.update_attributes(:test => SOMEVALUE) ,它将返回与您得到的相同的“参数数目错误(2..3为0)”消息。

当我将属性名称从“ test”更改为其他名称时,错误消失了。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM