css - 为什么这段代码在我的所有页面上呈现背景图像,而不仅仅是我的索引操作?

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

我试图只让我的索引操作呈现样式表/背景图像,但由于某种原因,背景图像也出现在所有其他页面上。我的欢迎 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/

相关文章:

sql-server - SSRS css 搞砸了?

ruby-on-rails - 验证中的正则表达式或 `nil`

ruby-on-rails - redirect_to , Controller 和模型之间的关系

ruby-on-rails - 将 Rails 链接设为 Bootstrap 按钮

ruby - 使用 x-www-form-urlencoded 的 POST 请求失败

ruby - 将字符串转换为 Nokogiri 节点

mysql - Ruby on Rails - 基本表单提交

css - Bootstrap : Col-md-3 Not starting from left on new line

javascript - FitText.js 不工作

html - 编辑 <li> 的高度和宽度真的不可取吗?