我在 main_controller.rb 中有两个具有相同类的 scss 文件和两个方法,它们生成 page1 和 page2。
= stylesheet_link_tag '应用程序'
在 application.html.haml
如何渲染两个页面,它们将使用不同的 scss 文件?
我在 main_controller.rb 中有两个具有相同类的 scss 文件和两个方法,它们生成 page1 和 page2。
= stylesheet_link_tag '应用程序'
在 application.html.haml
如何渲染两个页面,它们将使用不同的 scss 文件?
不要那样做。它使简单的事情不必要地复杂化。并且为每个页面包含单独的 CSS 并不是一个好习惯。
做这个:
看法
# app/views/page.html.erb
<div class="#{action_name}">
<div class="page-content"></div>
</div>
SCSS
.page1 {
.page-content { background: red; }
}
.page2 {
.page-content { background: green; }
}
应用程序/控制器/pages_controller.rb
class PagesController < ApplicationController
layout 'for_another_page', only: :page2
def page1
end
def page2
end
end
应用程序/视图/布局/for_another_page.html.erb
. . .
= stylesheet_link_tag 'another_scss_stylesheet'
. . .
配置/环境/production.rb
config.assets.precompile += %w( another_scss_stylesheet.css )
我认为它应该可以帮助您了解有关 RailsGuides http://guides.rubyonrails.org/layouts_and_rendering.html的更多信息