我试图只让我的索引操作呈现样式表/背景图像,但由于某种原因,背景图像也出现在所有其他页面上。我的欢迎 Controller 正在处理所有这些。布局文件名为 background.css.scss。这是我的欢迎 Controller :
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;
}
}
索引 View :
background-image: url(<%= asset_path 'sampleimage.jpg' %>)
最佳答案
您应该在 application.html.erb 布局中使用 :yield
:
在 application.html.erb 中:
..
<head>
<%= yield :head %>
</head>
...
现在在 index
View 中添加 content_for
block :
<% content_for(:head) do %>
<%= stylesheet_link_tag 'background' %>
<% end %>
请记住,您的 application.css
中不应包含 *= require_tree .
,否则 background.css.scss
将包含在每个 View 中。
关于css - 为什么这段代码在我的所有页面上呈现背景图像,而不仅仅是我的索引操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25610574/