별도의 컨트롤러 액션에서이 작업을 수행합니다 :
layout.html.erb
<%= stylesheet_link_tag stylesheet_path() %>
스타일 시트 쇼 컨트롤러 액션을
# Render the template to a string
css = Sass::Engine.new(
render_to_string("path/to/erb_template", :layout => false),
syntax: :scss,
cache: false,
read_cache: false,
style: :standard,
sprockets: {
context: self.view_context,
}
).render
# respond with the rendered string
respond_to do |format|
format.css { render plain: css, :content_type => "text/css" }
end
당신은 다음의 ERB 변수를 포함 할 수 있습니다 주형. 이것은 모든 단일로드에서 sass 엔진을 통해 처리되므로 프로덕션 환경에서이를 캐시해야합니다.
귀하의 모든 멋진를 별도의 파일로 유지하려는 경우 ERB로 렌더링하기 위해 하나의 파일에 변수를 넣고 나머지는 텍스트 파일로만 읽습니다.
rendered_sass = [render_to_string("stylesheets/colours", :layout => false),
File.read(Rails.root.join("app", "javascript", "stylesheets", "main.scss")),
File.read(Rails.root.join("app", "javascript", "stylesheets", "base.scss")),
File.read(Rails.root.join("app", "javascript", "stylesheets", "reset.scss")),
File.read(Rails.root.join("app", "javascript", "stylesheets", "type.scss")),
File.read(Rails.root.join("app", "javascript", "stylesheets", "layout.scss")),
File.read(Rails.root.join("app", "javascript", "stylesheets", "map.scss")),
File.read(Rails.root.join("app", "javascript", "stylesheets", "audio-player.scss")),
File.read(Rails.root.join("app", "javascript", "stylesheets", "slider.scss"))].join("\n\r")
Webpack은 ERB 인터프리터를 통해 자산을 실행하지 않습니다. 그러나 Sprockets는 그렇습니다. 또한 자산은 일반적으로 배포시 컴파일되므로 모든 변수가 요청 기반이 될 수 없음을 알아야합니다. 더 나은 해결책은 HTML의 클래스를 사용하여 사용할 스타일 시트에 다른 규칙을 설정하는 것입니다. – max