Skip to content


Subversion checkout URL

You can clone with
Download ZIP


css.erb custom helpers #3116

daniel2d2art opened this Issue · 8 comments

4 participants


can't we add some custom helper to use in css.erb files

we try to put some methods to Helpers/application_helper but they are not accesible from css.erb... in template output <%= myHelperMethod() %>

we add the "helper :all" in application_controller ... but no lucky!!

The helper and their methods are not accesible from css.erb



ActionView::Helper classes are generally only available to users of ActionView like app/views/

My thought is that you would be better off putting your methods in a lib/ module and including that module inside the css.erb file. I'm guessing you could also include ActionView::Helpers inside the css.erb file...


Try including your helper module in Sprockets::Context:

Rails.application.assets.context_class.instance_eval do
  include YourHelperModule

Hi Guille ...
As initializer or in application.rb??


@daniel2d2art try as initializer


@daniel2d2art this still being an issue for you?


No, I work arround templating directly to css, that is what I wanted to avoid. But in any case is another way to solve.


closing issue ...


Here a solution :
Create a config/initializers/sprockets_helpers.rb file
With this content :

# Support all ActionView goodness in asset files with *.erb extension
# More info at : and
Rails.application.assets.context_class.class_eval do
  include ERB::Util
  include ActionView::Helpers
  include Rails.application.routes.url_helpers

Then you can use ActionView helpers, Routes helpers and some ERB methods like h() in your [app|lib|vendor]/assets/[javascripts|stylesheets]/*.erb files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.