1

嗨,我有一个带有 gem Liquid 模板的 Rails 3 应用程序。问题是我的自定义标签出现此错误。我将使用 wiki 的示例。

在我的 /lib/ 中,我为我的自定义标签创建了一个 random.rb

 class Random < Liquid::Tag

   def initialize(tag_name, max, tokens)
     super
     @max = max.to_i
   end

   def render(context)
     rand(@max).to_s
   end
end

在我的初始化程序文件夹中,一个liquid.rb 用于注册该标签。

Liquid::Template.register_tag('random', Random)

在控制台中

@template = Liquid::Template.parse("{% random 5 %}")

我得到这个错误:

ArgumentError: wrong number of arguments (3 for 0..1)
from /Users/Artau/.rvm/gems/ruby-1.9.3-p385/gems/liquid-2.5.0/lib/liquid/block.rb:28:in `initialize'
from /Users/Artau/.rvm/gems/ruby-1.9.3-p385/gems/liquid-2.5.0/lib/liquid/block.rb:28:in `new'
from /Users/Artau/.rvm/gems/ruby-1.9.3-p385/gems/liquid-2.5.0/lib/liquid/block.rb:28:in `parse'
from /Users/Artau/.rvm/gems/ruby-1.9.3-p385/gems/liquid-2.5.0/lib/liquid/document.rb:5:in `initialize'
from /Users/Artau/.rvm/gems/ruby-1.9.3-p385/gems/liquid-2.5.0/lib/liquid/template.rb:58:in `new'
from /Users/Artau/.rvm/gems/ruby-1.9.3-p385/gems/liquid-2.5.0/lib/liquid/template.rb:58:in `parse'
from /Users/Artau/.rvm/gems/ruby-1.9.3-p385/gems/liquid-2.5.0/lib/liquid/template.rb:46:in `parse'
from (irb):14
from /Users/Artau/.rvm/gems/ruby-1.9.3-p385/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in `start'
from /Users/Artau/.rvm/gems/ruby-1.9.3-p385/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in `start'
from /Users/Artau/.rvm/gems/ruby-1.9.3-p385/gems/railties-3.2.13/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'

但如果我输入:

 1.9.3-p385 :004 > @template = Liquid::Template.parse("{{random 5}}")
 => #<Liquid::Template:0x007fa36b554108 @root=#<Liquid::Document:0x007fa36b553de8 @nodelist=[#<Liquid::Variable:0x007fa36b5536e0 @markup="random 5", @name="random", @filters=[]>]>> 

当我渲染时:

 1.9.3-p385 :005 > @template.render
 => ""

有任何想法吗?

谢谢

我在 Rails 3.2.13

4

1 回答 1

1

尝试重命名您的类,因为在标准 ruby​​ 库中已经有一个名为 Random 的类。

于 2013-04-18T20:26:19.243 回答