How to include ActionView helpers in the assets pipeline?

前端 未结 2 1657
抹茶落季
抹茶落季 2020-12-05 20:27

How to include Rails view helpers to be accesible by assets pipeline execution context?

An example use case would be to generate the markup for a form, using

相关标签:
2条回答
  • 2020-12-05 21:07

    The above answer is now out of date. As of sprockets-rails 3, the appropriate interface is a configure block, like so:

    Rails.application.config.assets.configure do |env|
      env.context_class.class_eval do
        # include SomeHelper
      end
    end
    

    This configure block should still be placed in an initializer.

    reference: https://github.com/rails/sprockets-rails/issues/307#issuecomment-170707886

    0 讨论(0)
  • 2020-12-05 21:20

    Create a inititializer and include the helpers in the context of the assets like this:

    Rails.application.assets.context_class.class_eval do
      include ActionView::Helpers
      include MyAppHelper
      include Rails.application.routes.url_helpers
    end
    

    Taken from this sprockets issue

    0 讨论(0)
提交回复
热议问题