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