如何为Rails 3网站中的每个页面设置不同的背景图片? [英] How to set a different background image for each page in Rails 3 site?

查看:81
本文介绍了如何为Rails 3网站中的每个页面设置不同的背景图片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄清楚如何为我的Rails 3网站的每一页设置不同的背景图片。

I'm trying to figure out how to set a different background image for each page of my Rails 3 site. How can I accomplish this without having to repeat what I have in my css file for each page?

application.html.erb

<!DOCTYPE html>
<html>
<head>
  <title>Site title</title>
  <%= stylesheet_link_tag    "application", :media => "all" %>
  <%= javascript_include_tag "application" %>
  <%= csrf_meta_tags %>
</head>
<body>
<div class="container">
<div class="content">
    <%= yield %>
</div>
<%= render 'layouts/footer' %> 
</div>
</body>
</html>

pages.css.scss

.container {
    width: 800px;
    height: 640px;
    position: absolute;
    left: 50%;
    top: 50%;
    margin-top: -320px;
    margin-left: -400px;
    background-image: url(amber1.jpg); 
    background-repeat: no-repeat; 
    background-color: black;
    font-family: times new roman, serif;
    text-align: center;
    }


推荐答案

可以使用css做到这一点。即使使用scss,您必须将其编译为纯CSS,然后才能使用它,因此您无法即时更改它。但是您可以从css中省略背景图片,然后执行

I don't think you can do this using css. Even with scss, you must compile it to pure css before you can use it, so you can't change it on the fly. But you can omit the background image from the css and then do

<div class="container" style="background: url(<%= @my_computed_image_path %>)">
</div>

然后你需要做的是在某些类型的before_filter中的控制器中设置@my_computed_image_path。

And then all you have to do is set up @my_computed_image_path in the controllers in some kind of before_filter.

这篇关于如何为Rails 3网站中的每个页面设置不同的背景图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆