css - 如何让我的背景图片只出现在 rails 4 的一页上?

标签 css ruby-on-rails ruby ruby-on-rails-4 sass

我有一张背景图片,我无法只停留在一页上。我制作了一个带有一个主视图的欢迎 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/

相关文章:

ruby - 在 Ruby 中更新哈希值

ruby-on-rails - Heroku CI Pipeline - 在构建之间缓存 gems

html - 如何使用 SVG clipPathUnits ="objectBoundingBox"

html - flex 元素 child 的意外大小

ruby-on-rails - 如何通过Rails中的另一个模型访问关联模型?

ruby-on-rails - Ruby on Rails 3 : Trying to render a view from a different controller. 但我得到了同一个 Controller

ruby - 如何将范围分解为较小的不重叠范围

javascript - PHP实时添加动态类别

css - HTML 中的奇怪间距

ruby-on-rails - 州、市、区数据库设计