0

我想知道,我们可以在 Rails 视图中使用 scss,例如

/views/home/home_stylesheet.css.scss.erb

$gray-medium-light : #eaeaea;

background: $gray-medium-light;

我尝试更改路径格式

<%= stylesheet_link_tag(about_me_user_path(current_user, format: :scss), media: 'all', class: "home_about_me_css") %>

也在路线中

get    'user_profile_stylesheet/:id'  => 'users#user_profile_stylesheet',  as: :user_profile_stylesheet, :defaults => { :format => 'scss' }

但是 rails 似乎只是将格式转换回css.

我们可以使用sass-railsgem 以某种方式做到这一点吗?

谢谢。

编辑我用谷歌搜索了这个,没有任何结果。

4

1 回答 1

1

理论上可以通过调用 sass 编译器来实现。请注意,您希望使用对 css 而不是 scss 的请求。客户没有理由需要知道文件是如何产生的。

<%= stylesheet_link_tag(about_me_user_path(current_user, format: :css), media: 'all', class: "home_about_me_css") %>

class UsersController
  def user_profile_stylesheet
    respond_to do |f| 
      f.css do
        fn = Rails.root.join('app', 'views', 'home', 'home_stylesheet.css.scss.erb')
         # expand ERB template
        sass = render_to_string(file: fn)
        # run rendered template through the sass compiler
        css = SassC::Engine.new(sass, style: :compressed).render 
        render text: css
      end
    end  
  end
end   

我不太确定它是您真正想要在生产中做的事情,因为它要求您在响应请求时在运行时编译 sass。而且您将无法在资产管道中引用诸如 SASS 函数之类的任何内容,因为这是在管道之外编译的。

这也是一场安全噩梦,因为 SASS 不仅仅是像 CSS 那样的声明性。这可以被利用在您的服务器上执行代码。

无论您尝试做什么,都必须有一个更智能/不太复杂的解决方案。

于 2020-03-09T18:22:20.550 回答