带参数的Rails helper_method
在视图中允许控制器方法作为帮助器时,如何传递参数?
在我的example_controller.rb中,我有
class ExampleController < ApplicationController helper_method :group_exists? private def group_exists?(gid): .. code to check if group exists end end
在视图中(我使用slim),我想传递一个参数
- if group_exists?(group.gid) | Exists - else | Does not exist
然而,它引发了一个错误说
wrong number of arguments (2 for 0..1)
伙计,如果你真的想要一个辅助方法,已经为它定义了一个地方。 只需在已经为您创建的ExampleHelper
类中编写相同的方法。
这样做的原因是,如果您想调试帮助程序,您将很容易知道在哪里查看。 在您的用例专门调用它之前,不要在控制器中创建帮助程序。
我在使用before_filter时使用了块。 它也可能在helper_method中很有用,但我不确定。 试试这个,请让我知道,它是否有效,如果不是,我们都会寻找其他选择:
helper_method {|x| x.group_exists?(gid) }
希望它会有所帮助。 谢谢