首页 > 解决方案 > 如何为rails中的布局设置特定样式

问题描述

我需要特定布局的特定样式表。

例如index.html.erb需要特定的样式表。

为什么需要这个,因为任何布局都有不同的主体样式。

当加载index.html.erb加载时custom.css.scss

标签: ruby-on-railsruby

解决方案


在您的布局文件中,假设application.html.erb文件:

使用条件样式表:如果是PostController和'index,则加载custom.css.scss ,否则加载custom2.css.scss

编辑:加载除custom.css.scss. 然后使用条件语句custom.css仅在PostController'index方法中加载

<%= stylesheet_link_tag 'style1' %>
<%= stylesheet_link_tag 'style2' %>
<%= stylesheet_link_tag 'style3' %>
<% if params[:controller] == 'post' && params[:action] == 'index' %>
    <%= stylesheet_link_tag 'custom' %>
<% end %>

推荐阅读