Add custom methods to Rails 3.1 asset pipeline?

后端 未结 2 2015
长发绾君心
长发绾君心 2020-12-17 20:07

How can I add my custom methods to my assets such as css files like Rails did with \'asset_path\' helper?

With Rail\'s own helper, I can write this:

         


        
2条回答
  •  爱一瞬间的悲伤
    2020-12-17 20:26

    The best way I found was to create a custom helper module in app/helpers:

    module AssetsHelper
      def my_custom_helper_method
        # do something  
      end
    end
    

    And then to require it like this in application.rb, after your applications configuration (very bottom):

    module Sprockets::Helpers::RailsHelper
      require Rails.root.join('app', 'helpers', 'assets_helper.rb')
      include AssetsHelper
    end
    

    And you might follow this issue to find a better way: https://github.com/rails/rails/issues/3282

提交回复
热议问题