我有一张背景图片,我无法只停留在一页上。我制作了一个带有一个主视图的欢迎 Controller 来显示它。我也在预编译我的 Assets 。背景显示得很好,但我的目标是只在我的 home.html.erb View 中显示背景图像。
欢迎/home.html.erb:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<%= I18n.locale || 'en' %>"
lang="<%= I18n.locale || 'en'%>">
<body class="container">
<h1>title</h1>
</body>
</html>
欢迎 Controller :
class WelcomeController < ApplicationController
def home
end
end
样式表/welcome.css.scss:
body
{
background: {
image: asset-url("image.jpg");
}
}
我的应用程序布局中有以下内容:
<head>
<%= stylesheet_link_tag "welcome" if controller_name == "welcome" %>
</head>
在 config/initializers/assets.rb 中:
Rails.application.config.assets.version = '1.0'
Rails.application.config.assets.precompile += %w( welcome.css )
最佳答案
添加特定的CSS,
body.welcome
{
background: {
image: asset-url("image.jpg");
}
}
在 html 中,
<body class="container welcome">
你一定想知道,即使你没有包含特定的文件,为什么它会应用到所有地方。这是因为您必须在 application.css
require_tree .
关于css - 如何让我的背景图片只出现在 rails 4 的一页上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25689898/