【发布时间】:2011-06-08 16:53:32
【问题描述】:
我正在编写一个帮助程序,将 HTML 属性添加到 rails 中的 link_to 标记。所以,我的想法是我的辅助方法应该接受传递给它的任何参数或块,使用相同的参数调用 link_to,将它的属性添加到返回的内容中,并将结果返回给调用者。
像这样:
def link_to(*args, &block)
... rails code in link_to ...
end
def myhelper(*args, &block) # Notice that at this point, 'args' has already
link_to() # become an array of arguments and 'block' has
... my code ... # already been turned into a Proc.
end
myhelper() # Any arguments or blocks I pass with this call should make
# it all the way through to link_to.
因此,如您所见,似乎没有办法(不涉及大量代码和条件分支)将 myhelper 收到的内容传递给 link_to 而不将所有参数恢复为之前的样子他们采用了我的方法。
对于这个问题是否有更“类似于 Ruby”的解决方案?
【问题讨论】:
-
请注意,在 Ruby 2.0 中,命名参数也可能必须被转发...
标签: ruby-on-rails ruby