开发者

Multiple Rails Helpers w/ Same Method Name

开发者 https://www.devze.com 2023-03-11 05:57 出处:网络
I have two different helper files (photos_helper & comments_helper) w/ that have a method named actions_for. How can I explicitly call which helper method that开发者_StackOverflow中文版 I need? I

I have two different helper files (photos_helper & comments_helper) w/ that have a method named actions_for. How can I explicitly call which helper method that开发者_StackOverflow中文版 I need? I know that I could just rename one of them but I would prefer to keep them the same. I tried PhotosHelper::actions_for but that doesn't seem to work.


In Rails 3 all helpers are always (in Rails 3.1 a patch exists to selectively allow helpers again) included. What's happening behind the scenes:

class YourView
  include ApplicationHelper
  include UserHelper
  include ProjectHelper

  ...
end

So depending on the order Rails includes them, any of your actions_for methods will be used. There is no way you can explicitly chose one of them.

If you would have to explicitly call ProjectHelper.action_for, you could also name your methods project_action_for - simplest solution.


Make both of them a Class Method

module LoginsHelper
  def self.your_method_name
    "LoginsHelper"
  end
end

AND

module UsersHelper
  def self.your_method_name
    "UsersHelper"
  end
end

Then in View

   LoginsHelper.your_method_name #Gives 'LoginsHelper'

AND

   UsersHelper.your_method_name #Gives 'UsersHelper'
0

精彩评论

暂无评论...
验证码 换一张
取 消