1

I want to change my application back ground dynamically.For that background image, i'm saving the image in DB with only one row,if i upload new background image it will update the DB first row and it will affect background as well.

here is my view code

<%=ChangeBackdrops.first.media.url%>

here is my css

body {
  background-image: url(/../../../images/back.jpg);
  background-repeat:no-repeat;
  background-position:center center;
  background-attachment:fixed;
  -o-background-size: 100% 100%, auto;
  -moz-background-size: 100% 100%, auto;
  -webkit-background-size: 100% 100%, auto;
  background-size: 100% 100%, auto;
}

How can I pass my ruby tag in that CSS? That means like this?

background-image: url(<%=ChangeBackdrops.first.media.url%>)
Holger Just
  • 52,918
  • 14
  • 115
  • 123

2 Answers2

1

The simplest way you can achieve that is just placing the code inside the style tag while rendering the view. Try writing the following code inside the application.html.erb (for example):

<head>
  ...
  <link rel="stylesheet" ... /> <!-- load your common stylesheet --!>
  <!-- and now override the background-image --!>
  <style>
    body {
      background-image: url("#{ChangeBackdrops.first.media.url}");
    }
  </style>
</head>
oldhomemovie
  • 14,621
  • 13
  • 64
  • 99
0

What rails version do you use? Do you use Asset Pipeline? In that case you can just add erb extension to your css file and use your suggested line <%=ChangeBackdrops.first.media.url%>. You may want to check this guide, the 'preprocessing' part of it

m.silenus
  • 492
  • 7
  • 15