我有一个应用程序布局,其中包括应用程序范围的 css 样式表和 js 文件,但是某些 Controller 的操作需要仅用于这些操作的其他样式表
观点?包含它们的最佳方法是什么?
最佳答案
如果您的应用程序通常类似于:
<html>
<head>
<%= stylesheet_link_tag 'application' -%>
<%= javascript_include_tag 'yui', 'application' -%>
</head>
<body>
<%= yield -%>
</body>
</html>
你可以在任何你喜欢的地方添加其他 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 %>
等等等等...
关于ruby-on-rails - 为 Rails 中的不同操作设置单独样式表的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5097805/