在Rails中为不同的操作提供单独样式表的最佳方法

Ale*_*ler 4 ruby-on-rails

我有一个应用程序布局,包括app宽css样式表和js文件,但某些控制器的操作需要额外的样式表,仅用于这些操作的视图?包含它们的最佳方法是什么?

Eri*_*ric 5

如果您的应用通常类似于:

<html>
  <head>
    <%= stylesheet_link_tag 'application' -%>
    <%= javascript_include_tag 'yui', 'application' -%>
  </head>
  <body>
    <%= yield -%>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

您可以随意添加其他yield块,命名为您想要的任何名称.通常我使用它来包含特定于页面的功能,无论我喜欢什么,甚至可能部分提供自己的功能.

# layouts/application.html.erb
<html>
  <head>
    <%= stylesheet_link_tag 'application' -%>
    <%= javascript_include_tag 'yui', 'application' -%>
    <%= yield :head -%>
  </head>
  <body>
    <%= yield -%>
  </body>
</html>

# views/profiles/show.html.erb
<%= title("#{@user.name}'s Profile") -%>

<% content_for :head do -%>
  <%= javascript_include_tag 'gallery' %>
<% end %>

<%= render @user.photos %>
Run Code Online (Sandbox Code Playgroud)

等等等等...