My production environment -
# Code is not reloaded between requests
config.cache_classes = true
config.assets.enabled = true
# Full error reports are disabled and caching is turned on
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
# Disable Rails's static asset server (Apache or nginx will already do this)
config.serve_static_assets = true
config.static_cache_control = "public, max-age=31536000"
# Compress JavaScripts and CSS
config.assets.compress = true
config.assets.js_compressor = :uglifier
#config.assets.css_compressor = :yui
# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = true
# Generate digests for assets URLs
config.assets.digest = true
# See everything in the log (default is :info)
config.log_level = :warn
config.log_tags = [:remote_ip, lambda { |req| Time.now }]
# Enable serving of images, stylesheets, and JavaScripts from an asset server
ActionController::Base.asset_host = Proc.new { |source|
unless source.starts_with?('/stylesheets' || '/javascripts')
"//dddd.cloudfront.net/"
end
}
However, when I using image_tag it still returns me '/assets..' relative url and not absolute url to the asset host.
irb(main):034:0> helper.image_tag('noimage.gif')
=> "<img alt=\"Noimage\" src=\"/assets/noimage.gif\" />"
irb(main):035:0> helper.image_path('noimage.gif')
I can not seem to figure what might be missing. I even tried doing simple config.asset_host setting, and still it does not recognize the setting.
Have you tried setting the designated config?
config.action_controller.asset_host = 'https://abcd.cloudfront.net'
Not sure it works with protocol relative urls. I simply use https in my apps.
It might also be worth noting that action mailer has a similar setting:
config.action_mailer.asset_host = 'https://abcd.cloudfront.net'
There's something odd in Rails configuration. According to the documentation asset_host is supposed to hold just the host part ('yoursite.com'). I resolved with an helper:
module EmailsHelper
def email_image_tag(src)
protocol = ActionMailer::Base.default_url_options[:protocol] || 'http'
image_tag asset_path(src, protocol: protocol)
end
end
Configure it in your mailer:
class EmailNotificationsMailer < ActionMailer::Base
add_template_helper(EmailsHelper)
...
end
There's no way to force image_tag to use a URL other than to pass a URL to it.
Related
For context, I essentially followed this guide to get my rails app set up: https://medium.com/#jatescher/how-to-set-up-a-rails-4-2-app-on-aws-with-elastic-beanstalk-and-postgresql-3f9f29c046e2
My app is now running on AWS:
http://ems-heroes-dev.elasticbeanstalk.com/
64bit Amazon Linux 2015.03 v2.0.1 running Ruby 2.2 (Puma)
However, I can't get any of my assets to load.
Here is my config/environments/production.rb:
EmsHeroes::Application.configure do
config.cache_classes = true
config.eager_load = true
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
config.serve_static_assets = true
# Compress JavaScripts and CSS.
config.assets.js_compressor = :uglifier
config.assets.compress = true
config.assets.compile = true
# Bower asset paths
root.join('vendor', 'assets', 'components').to_s.tap do |bower_path|
config.sass.load_paths << bower_path
config.assets.paths << bower_path
end
# Precompile Bootstrap fonts
config.assets.precompile << %r(bootstrap-sass/assets/fonts/bootstrap/[\w-]+\.(?:eot|svg|ttf|woff2?)$)
# Minimum Sass number precision required by bootstrap-sass
::Sass::Script::Value::Number.precision = [8, ::Sass::Script::Value::Number.precision].max
config.less.paths << "#{Rails.root}/lib/less/protractor/stylesheets"
config.less.compress = true
# Generate digests for assets URLs.
config.assets.digest = true
config.assets.enabled = true
# Version of your assets, change this if you want to expire all your assets.
config.assets.version = '1.0'
# Specifies the header that your server uses for sending files.
# config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache
config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx
# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
config.force_ssl = false
config.log_level = :info
config.i18n.fallbacks = true
config.active_support.deprecation = :notify
config.log_formatter = ::Logger::Formatter.new
end
My assets are loading fine via heroku though. (However, I dont plan on using heroku any more)
EDIT
It looks like its an issue with rake assets:precompile providing the wrong file names. Ex: My compiled css file is: "public/assets/application-79dc234c01a4f604b52fc53ff49ac89d.css" but Im getting a 404 looking for "application.css". Renaming the file to "public/assets/application.css" will load it properly.
Any idea why this would be the case and how I can get it to precompile properly?
This string after file name is called "digest", it's used to notify browser when file content changes. You need to use view helpers (asset_path, etc) to get name with digest, or you can turn off this feature with config.assets.digest = false
Using Rails 3.1.1 on Heroku, Dalli gem and Memcachier.
I am trying to troubleshoot an issue where keys disappear from Memcachier, in order to do this I would like to make sure that only the content from my caches_action will be cached in the memcache. I would like my assets (jpg's etc) to be cached elsewhere.
I believe it is the same issue as this question but when I do the same, i.e. adding
config.action_dispatch.rack_cache = {
:metastore => Dalli::Client.new,
:entitystore => 'file:tmp/cache/rack/body',
:allow_reload => false
}
to my production.rb, it doesn't really seem to do anything.
My production.rb in full:
MyDemoApp::Application.configure do
# Settings specified here will take precedence over those in config/application.rb
# Code is not reloaded between requests
config.cache_classes = true
# Full error reports are disabled and caching is turned on
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
# Disable Rails's static asset server (Apache or nginx will already do this)
config.serve_static_assets = true
# Compress JavaScripts and CSS
config.assets.compress = true
# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = true
# Generate digests for assets URLs
config.assets.digest = true
# Defaults to Rails.root.join("public/assets")
# config.assets.manifest = YOUR_PATH
# Specifies the header that your server uses for sending files
# config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache
# config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx
# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
# config.force_ssl = true
# See everything in the log (default is :info)
# config.log_level = :debug
# Prepend all log lines with the following tags
# config.log_tags = [ :subdomain, :uuid ]
# Use a different logger for distributed setups
# config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new)
# Use a different cache store in production
config.cache_store = :dalli_store
config.action_dispatch.rack_cache = {
:metastore => Dalli::Client.new,
:entitystore => 'file:tmp/cache/rack/body',
:allow_reload => false
}
# Set expire header of 30 days for static files
config.static_cache_control = "public, max-age=2592000"
# Enable serving of images, stylesheets, and JavaScripts from an asset server
# config.action_controller.asset_host = "http://assets.example.com"
# Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
# config.assets.precompile += %w( search.js )
# Disable delivery errors, bad email addresses will be ignored
# config.action_mailer.raise_delivery_errors = false
# Enable threaded mode
# config.threadsafe!
# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
# the I18n.default_locale when a translation can not be found)
config.i18n.fallbacks = true
# Send deprecation notices to registered listeners
config.active_support.deprecation = :notify
end
Caching assets work fine (get cache fresh-responses) and caching actions like this:
caches_action :show, :layout => false, :expires_in => 3.days
Also creates a cached entry in Rails cache (although they disappear after a while).
My question now is: How can I, if possible, do so that only my caches_action fragments end up in my Memcache (and not my assets)?
A bonus question would be, do you see anything out of the ordinary in my production.rb?
I use the gem 'asset_sync' to move all my assets out to Amamzon S3
See Instructions here
You get three advantages:
Your assets are served from Amazon so the page loads faster for the
user
Your slug size on Heroku is smaller and is supposed to be more
performant
Your assets will no longer be in memecahce
All my assets cost me about 20cents every three month to host on Amamzon
I am having issues with asset_path in production. Rails 3.1.1
#config/environments/development.rb
Scc::Application.configure do
# Settings specified here will take precedence over those in config/application.rb
# In the development environment your application's code is reloaded on
# every request. This slows down response time but is perfect for development
# since you don't have to restart the web server when you make code changes.
config.cache_classes = false
# Log error messages when you accidentally call methods on nil.
config.whiny_nils = true
# Show full error reports and disable caching
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
# Don't care if the mailer can't send
config.action_mailer.raise_delivery_errors = false
# Print deprecation notices to the Rails logger
config.active_support.deprecation = :log
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
# Only use best-standards-support built into browsers
config.action_dispatch.best_standards_support = :builtin
# Do not compress assets
config.assets.compress = false
# Expands the lines which load the assets
config.assets.debug = true
end
and
#config/environments/production.rb
Scc::Application.configure do
# Settings specified here will take precedence over those in config/application.rb
# Code is not reloaded between requests
config.cache_classes = true
# Full error reports are disabled and caching is turned on
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
# Disable Rails's static asset server (Apache or nginx will already do this)
config.serve_static_assets = false
# Compress JavaScripts and CSS
config.assets.compress = true
# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = false
# Generate digests for assets URLs
config.assets.digest = true
# Defaults to Rails.root.join("public/assets")
# config.assets.manifest = YOUR_PATH
# Specifies the header that your server uses for sending files
# config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache
# config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx
# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
# config.force_ssl = true
# See everything in the log (default is :info)
# config.log_level = :debug
# Use a different logger for distributed setups
# config.logger = SyslogLogger.new
# Use a different cache store in production
# config.cache_store = :mem_cache_store
# Enable serving of images, stylesheets, and JavaScripts from an asset server
# config.action_controller.asset_host = "http://assets.example.com"
# Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
# config.assets.precompile += %w( search.js )
# Disable delivery errors, bad email addresses will be ignored
# config.action_mailer.raise_delivery_errors = false
# Enable threaded mode
# config.threadsafe!
# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
# the I18n.default_locale when a translation can not be found)
config.i18n.fallbacks = true
# Send deprecation notices to registered listeners
config.active_support.deprecation = :notify
end
I have the following code:
.right-bar-filler{
background:url(asset_path('right_bar_filler.jpg', image)) repeat-y;
padding-top:0px;
}
In development it ends up being:
.right-bar-filler {
background: url("/assets/right_bar_filler.jpg") repeat-y;
padding-top: 0px;
}
In production it outputs as:
.right-bar-filler {
background: url(asset_path("right_bar_filler.jpg", image)) repeat-y;
padding-top: 0px;
}
What am I completely missing?
Thank you for any help.
This doesn't look correct:
.right-bar-filler{
background:url(asset_path('right_bar_filler.jpg', image)) repeat-y;
padding-top:0px;
}
If you want to use the asset_path helper, it needs to run inside the erb tags (<% %>)
.right-bar-filler{
background:url(<%= asset_path('right_bar_filler.jpg', image) %>) repeat-y;
padding-top:0px;
}
and make sure you name the file correctly, i.e. example_filename.css.erb
UPDATE: Sorry, I didn't notice you were using SASS, not CSS. My above answer is not what you need.
Try this instead:
.right-bar-filler{
background:url(asset-path('right_bar_filler.jpg', image)) repeat-y;
padding-top:0px;
}
I.e. I think the asset path helper uses hyphens in SASS, not underscores
http://rubydoc.info/github/petebrowne/sprockets-sass/master/Sprockets/Sass/Functions
Did you rake assets:precompile?
By default, Rails will not compile assets in production. The recommended workflow is to compile assets as part of your deployment.
I recently updated my Rails to 3.1.
Here's a part where I added:
<%= asset_path('logo_symbol.png') %>
This renders /assets/logo_symbol.png which works perfectly fine in development environment. However, when i push the code to production on heroku, it shows a broken image, with the url: assets/logo_symbol-135ddc8db2c9b59f032bed7db520137a.png. I am guessing the new name is for the reason of some optimization.
It is however interesting to note that when I go to the assets/logo_symbol-135ddc8db2c9b59f032bed7db520137a.png url on production, I see a blank page, but when I change that url to anything random, like adding numbers to it, it shows a page not found. So clearly it is finding something on that url. It also shows a blank page when I go to /assets/logo_symbol.png directly on production/heroku.
If this is any help, heroku does not precompile successfully when I push the code and heroku's documentation says that there is currently no work around for that issue.
Any help in this will be greatly appreciated.
My guess is that it has something to do with some configuration related to environments. I am attaching contents of my application.rb, development.rb and production.rb files
here are the contents of my production.rb file
# Settings specified here will take precedence over those in config/application.rb
# In the development environment your application's code is reloaded on
# every request. This slows down response time but is perfect for development
# since you don't have to restart the webserver when you make code changes.
config.cache_classes = false
# Log error messages when you accidentally call methods on nil.
config.whiny_nils = true
# Show full error reports and disable caching
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
# Don't care if the mailer can't send
#config.action_mailer.default_url_options = { :host => 'localhost:3000' }
#config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.delivery_method = :smtp
# Print deprecation notices to the Rails logger
config.active_support.deprecation = :log
# Only use best-standards-support built into browsers
config.action_dispatch.best_standards_support = :builtin
# Do not compress assets
config.assets.compress = false
# Expands the lines which load the assets
config.assets.debug = true
end
module ActiveAdmin
class Reloader
def attach!
end
end
end
and here are the contents of my development.rb file
# Settings specified here will take precedence over those in config/application.rb
# In the development environment your application's code is reloaded on
# every request. This slows down response time but is perfect for development
# since you don't have to restart the webserver when you make code changes.
config.cache_classes = false
# Log error messages when you accidentally call methods on nil.
config.whiny_nils = true
# Show full error reports and disable caching
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
# Don't care if the mailer can't send
#config.action_mailer.default_url_options = { :host => 'localhost:3000' }
#config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.delivery_method = :smtp
# Print deprecation notices to the Rails logger
config.active_support.deprecation = :log
# Only use best-standards-support built into browsers
config.action_dispatch.best_standards_support = :builtin
# Do not compress assets
config.assets.compress = false
# Expands the lines which load the assets
config.assets.debug = true
end
module ActiveAdmin
class Reloader
def attach!
end
end
end
Here are the contents of my production.rb file
# Settings specified here will take precedence over those in config/application.rb
# The production environment is meant for finished, "live" apps.
# Code is not reloaded between requests
config.cache_classes = true
# Full error reports are disabled and caching is turned on
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
# Specifies the header that your server uses for sending files
config.action_dispatch.x_sendfile_header = "X-Sendfile"
# For nginx:
# config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect'
# If you have no front-end server that supports something like X-Sendfile,
# just comment this out and Rails will serve the files
# See everything in the log (default is :info)
# config.log_level = :debug
# Use a different logger for distributed setups
# config.logger = SyslogLogger.new
# Use a different cache store in production
# config.cache_store = :mem_cache_store
# Disable Rails's static asset server
# In production, Apache or nginx will already do this
config.serve_static_assets = false
# Enable serving of images, stylesheets, and javascripts from an asset server
# config.action_controller.asset_host = "http://assets.example.com"
# Disable delivery errors, bad email addresses will be ignored
# config.action_mailer.raise_delivery_errors = false
#config.action_mailer.default_url_options = { :host => 'ha1.heroku.com' }
config.action_mailer.delivery_method = :smtp
# Enable threaded mode
# config.threadsafe!
# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
# the I18n.default_locale when a translation can not be found)
config.i18n.fallbacks = true
# Send deprecation notices to registered listeners
config.active_support.deprecation = :notify
# Compress JavaScripts and CSS
#config.assets.compress = true
# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = false
# Generate digests for assets URLs
config.assets.digest = true
# Defaults to Rails.root.join("public/assets")
# config.assets.manifest = YOUR_PATH
config.assets.js_compressor = :uglifier
config.assets.css_compressor = :scss
I have compared my config files with the rails documentation for 3.1 and seems like I have all the defaults needed. However I am still seeing no image. Any help will be much appreciated
Remove this line from production.rb:
config.action_dispatch.x_sendfile_header = "X-Sendfile"
You should also align the settings in your config files with those in section 9 of the pipeline guides.
Sendfile headers contain information for the upstream webserver of where to find the file (on the file system) to serve it. This removes the load from the backend (Rails/Sprockets). When sendfile is on the HTTP response contains no body (it is zero length) which is why you see nothing.
On heroku the nginx servers do not have access to the application filesystem, so this won't work.
See this note on the Heroku dev site re sendfile.
If you are using heroku, this document outlines the best options for using the pipeline effectively.
You need to do two things to resolve it.
First, change these two lines from false to true in production.rb file.
config.assets.compile = true
config.assets.digest = true
Second, if you've syntax like this for your images
background: url("imgo.jpg")
Change it to
background: image-url("image.jpg")
I hope it does your job.
I have trouble using any form of the asset pipeline within mailer, wether the Mailer itself or the view.
The following produces and empty src image tag.
<%= image_tag "emails/header-general.png" %>
The empty image tag looks like this:
img alt="Header-general"
The following form of attaching a file through the model and using it in the view attaches an empty image.
attachments.inline['header.jpg'] = 'emails/header-general.png'
...
<%= image_tag attachments['header.png'] %>
I did check the path and even tried with multiple paths and so on but with no luck.
Please help. Any form of including an image within the email would be helpful.
Here is the production env.
Xenium::Application.configure do
# Settings specified here will take precedence over those in config/application.rb
# Code is not reloaded between requests
config.cache_classes = true
# Full error reports are disabled and caching is turned on
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
# Disable Rails's static asset server (Apache or nginx will already do this)
config.serve_static_assets = false
# Compress JavaScripts and CSS
config.assets.compress = true
# Choose the compressors to use
config.assets.js_compressor = :yui
config.assets.css_compressor = :yui
# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = true
# Generate digests for assets URLs
config.assets.digest = true
# Defaults to Rails.root.join("public/assets")
# config.assets.manifest = YOUR_PATH
# Specifies the header that your server uses for sending files
# config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache
config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx
# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
# config.force_ssl = true
# See everything in the log (default is :info)
config.log_level = :fatal
# Use a different logger for distributed setups
# config.logger = SyslogLogger.new
# Use a different cache store in production
config.cache_store = :mem_cache_store
# Enable serving of images, stylesheets, and JavaScripts from an asset server
#config.action_controller.asset_host = "http://asset.xenium.bg"
# Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
# config.assets.precompile += %w( search.js )
# Disable delivery errors, bad email addresses will be ignored
config.action_mailer.raise_delivery_errors = true
#config.action_mailer.perform_deliveries = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "localhost",
:port => 25,
:domain => 'xenium.bg',
#:user_name => '<username>',
#:password => '<password>',
#:authentication => 'plain',
:enable_starttls_auto => false
}
# Enable threaded mode
# config.threadsafe!
# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
# the I18n.default_locale when a translation can not be found)
config.i18n.fallbacks = true
# Send deprecation notices to registered listeners
config.active_support.deprecation = :notify
end
Thanks!
set config.action_controller.asset_host and config.action_mailer.asset_host and this works well.
config.action_mailer.asset_host = URL from where pick image
<%= image_tag image_path('logo.png') %>
According to the 2.3.3 Making Inline Attachments section, to create an inline attachment you will do as follows
attachments.inline['image.jpg'] = File.read('/path/to/image.jpg')
So in your case, it should be
attachments.inline['header.jpg'] = File.read("#{Rails.root}/app/assets/images/emails/header-general.png"