NothingToSeeHere NothingToSeeHere - 18 days ago 5
Sass (Sass) Question

Rails 4 configuration and erb in scss for conditional cloudfront paths

I am trying to add dynamic cloudfront asset full paths into my scss. The path changes based on the evironment (staging, etc)

mystyle.scss

.some-image {background-image: url(asset_path("#{Rails.configuration.adjusted_s3_cloudfront_path}landing/this-image.jpg"))}


or mystyle.scss

.some-image {background-image: url(Rails.configuration.adjusted_s3_cloudfront_path('/landing/this-image.jpg'))}


or mystyle.scss.erb

.some-image {background-image: url(<% Rails.configuration.adjusted_s3_cloudfront_path %>"/landing/this-image.jpg")}


development.rb

config.adjusted_s3_cloudfront_path = "https://somecloufrontnumber.cloudfront.net"


production.rb

config.adjusted_s3_cloudfront_path = "https://#{ENV['S3_CLOUDFRONT_URL']}"

Answer

Why don't you remove config.adjusted_s3_cloudfront_path = "https://#{ENV['S3_CLOUDFRONT_URL']}" in favor of declaring a helper in app/controllers/application.rb?


.some-image {
  background-image: url(<%= cloudfront_path('landing/this-image.jpg') %>)
}

app/controller/application.rb

class ApplicationController < ActionController::Base
  def cloudfront_path(asset_uri)
    return ENV['S3_CLOUDFRONT_URL'] + 'asset_url'
  end
end
Comments