I have created a RoR based app/website where the client can upload their own background image to the cover page.
Everything works well with one image, but I would like the site to be responsive and allow my client the choice to upload 3 completely separate images that are displayed depending on the px width of the window.
Currently I have the following code (only showing relevant code), which works fine:
In covers_controller.rb:
class CoversController < ApplicationController
#
@covers = Cover.all
@cover = Cover.find_by(:published => 'yes')
#
end
In application.html.erb:
<body style="background-image: url(<%= @cover.image %>)">
I have however not succeeded in making the cover images responsive. I have tried the following...
Adding a css.erb file with the following code:
@media (max-width: 767px) {
.cover-bg {
background-image: url(<%= @cover.image %>)
}
}
@media (min-width: 768px) {
.cover-bg {
background-image: url(<%= @cover.m-image %>)
}
}
@media (min-width: 1200px) {
.cover-bg {
background-image: url(<%= @cover.lg-image %>)
}
}
For the css.erb above I added two new images to the Model called m-image and lg-image (using paperclip), and added the .cover-bg class to the body tag on application.html.erb, but got a blank page.
UPDATE: here is the model. I realize that I did not add the new images to the Model. However, when first testing the idea of using css.erb, I used images from three different Models for the different @media css, and encountered the same problem:
class Cover < ActiveRecord::Base
has_attached_file :image,
styles: { xlarge: "1600x1600>", large: "600x600>", medium: "300x300>", thumb: "100x100>" }
validates_attachment_content_type :image,
content_type: /\Aimage\/.*\z/
end
I hope there is a easy solution out there, but I have yet to find it. I also tried various application helper methods, but no luck thus far.