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



我想知道,我们能在轨道视图中使用scs吗?例如

/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宝石来做这件事吗?

谢谢。

编辑我在谷歌上搜索了一下,没有任何结果。

通过调用sass编译器理论上是可能的。请注意,您希望使用css请求,而不是scs请求。客户端没有理由需要知道文件是如何生成的。

<%= 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那样只是声明性的。这可能会被用来在服务器上执行代码。

无论你想做什么,都必须有一个更智能/不那么复杂的解决方案。

最新更新