简体   繁体   中英

Rails helper_method with argument

When allowing controller methods as helper in the views, how do you pass an argument?

In my example_controller.rb I have

class ExampleController < ApplicationController
  helper_method :group_exists?

  private
  def group_exists?(gid):
    .. code to check if group exists
  end
end

In the view (I use slim), I want to pass an argument

- if group_exists?(group.gid)
  | Exists
- else
  | Does not exist

However it throws an error saying

wrong number of arguments (2 for 0..1)

Buddy if you really want a helper method, there is already a place defined for it. Just write the same method in ExampleHelper class already created for you.

The reason for this is that if you want to debug the helper, you will know where to look very easily. Don't create helpers in controller until your use case specially call for it.

I use blocks while using before_filter. It might be useful in helper_method as well, but I am not sure. Try this and please let me know, it worked or not, if not we both will look for other options:

helper_method {|x|  x.group_exists?(gid) }

Hope it will help. Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM