简体   繁体   中英

what's the differences between helper and include in a controller?

i'm working on refinerycms, trying to add preview feature to news like pages, i'm trying to include my own helper to a controller, 'include' does not works, but 'helper' works.

sample code:

module Refinery
  module News
    module Admin
      class PreviewController < ActionController::Base
        #include LayoutHelper # not ok
        helper LayoutHelper # Ok
      end
    end
  end
end

i've read the api, helper is working like 'require and include', but i don't know the real difference here.

thanks !

helper LayoutHelper does include LayoutHelper , but it is included directly in the template class .

while include LayoutHelper , the module is included in the controller class .

You could check the source of helper :

# File actionpack/lib/abstract_controller/helpers.rb, line 93
def helper(*args, &block)
  modules_for_helpers(args).each do |mod|
    add_template_helper(mod)
  end

  _helpers.module_eval(&block) if block_given?
end

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