Dynamically render a sass file through sprockets

寵の児 提交于 2019-12-04 07:43:46
elstgav

After digging through a similar question, and a lot of trial and error, I found my solution: I have to supply a :sprockets hash when calling Sass::Engine.new.

css = Sass::Engine.new(
  rendered_sass,
  syntax:     :scss,
  cache:      false,
  load_paths: view_context.assets.paths,
  read_cache: false,
  style:      :compressed,

  # The key ingredient…
  sprockets:  {
    context:     view_context,
    environment: view_context.assets
  }
).render

It should be noted that view_context was passed from a view file, but it could have also been ActionView::Base.new

After struggling with the same issue I have found a live example of the same functionality on a Github page, surprisingly not gemified.

https://github.com/BLauris/custom-css-for-user

There is an article of the author explaining the method on this link: http://www.diatomenterprises.com/dynamically-compile-stylesheets-with-rails-and-sass/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!