为什么此代码在我的所有页面上呈现背景图像,而不仅仅是我的索引操作?
我试图只使我的索引操作呈现样式表/背景图像,但由于某种原因,背景图像也出现在所有其他页面上。 我的欢迎控制器正在处理所有这些。 布局文件名为background.css.scss。 这是我的欢迎控制器:
class WelcomeController < ApplicationController layout :resolve_layout def index end def show end def about end def testimonials end private def resolve_layout case action_name when "index" "background.css.scss" else "application" end end end
这是background.css.scss样式表:
body { width: 100%; background: { image: asset-url("sampleimage.png"); repeat: no-repeat; size: 100% auto; } }
索引视图:
background-image: url()
您应该在application.html.erb布局中使用:yield
:
在application.html.erb中:
.. <%= yield :head %> ...
现在在index
视图中添加content_for
块:
<% content_for(:head) do %> <%= stylesheet_link_tag 'background' %> <% end %>
请记住,您不应该有*= require_tree .
在您的application.css
否则background.css.scss
将包含在每个视图中。