簡體   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