开发者

HOW TO invoke application_helper's method in a specific CategoryHelper

开发者 https://www.devze.com 2023-01-23 18:38 出处:网络
for the purpose of reuse i put some method on applciation_helper,now i want to invoke this method on a specific helper like CategoryHelper,

for the purpose of reuse i put some method on applciation_helper,now i want to invoke this method on a specific helper like CategoryHelper,

is i need do something else?

Application_helper.rb
def ad_materials(a)
   d开发者_如何学运维o sth
end


CategoryHelper.rb
ad_materials("dd")#this method define on application_helper,but it didnt' work

is this a commany way to use common method put them in application_helper or any other recommend way,hope someone could give me a hand,thanks

i have re-edited the question,to make it clear,hope someone help again


If you call helper :all in your application_controller, then any helper will be available in any view.

Example:

app/helpers/application_helper.rb

module ApplicationHelper
  def helper(object)
    object.to_s
  end
end

app/controllers/application_controller.rb

class ApplicationController < ActionController::Base
  helper :all
end

app/views/other_objects/show.html.erb

<%= helper(other_object) %>

Is this what you are asking?


It doesn't really matter where you put the helpers in terms of how your application finds them. But you do need to define and end your helpers such as

def ad_materials("dd")
    do stuff here
end 


If you're saying "how do I invoke a ApplicationHelper methods from CategoryHelper" I've ran into this problem. My less than elegant workaround was this:

module ReusableHelpers
  def ad_materials(a)
    do sth
  end
end

module ApplicationHelper
  ...
  include ReusableHelpers

  ...
end

module CategoryHelper
  include ReusableHelpers

  def show_pastries_only    
    ad_materials("cherry")
  end
end
0

精彩评论

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