我们可以在 rails css.erb 视图文件中使用 scss (sass-rails) 还是这只是一个资产管道?

Can we use scss (sass-rails) in rails css.erb view files or is this just a asset pipeline thing?

我想知道,我们可以在 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-rails gem 以某种方式做到这一点吗?

谢谢。

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

理论上可以通过调用 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 那样只是声明性的。这可以被利用来在您的服务器上执行代码。

无论您想做什么,都必须有一个更智能/更简单的解决方案。