Has anyone got the asset_sync gem to work with Rails 4? I have never had a problem with it with Rails 3, but i cannot precompile my assets to my S3 bucket anymore, what happens is everything is just compiled into my public folder.
Could anyone offer advice on resources to look at or summarise the key differences between Rails 3 and 4 that would cause this to fail. Some examples of configuration used would be helpful from those who have got it working. I am at a loss on how to start debugging this
Any advice and help appreciated
Thanks
EDIT
Current config
asset_sync.rb # Within Initializer
if defined?(AssetSync)
AssetSync.configure do |config|
config.fog_provider = ENV['FOG_PROVIDER']
config.aws_access_key_id = ENV['AWS_ACCESS_KEY_ID']
config.aws_secret_access_key = ENV['AWS_SECRET_ACCESS_KEY']
config.fog_directory = ENV['FOG_DIRECTORY']
config.fog_region = ENV['FOG_REGION']
config.existing_remote_files = "delete"
config.gzip_compression = true
config.manifest = true
config.custom_headers = { '.*' => { cache_control: 'max-age=315576000', expires: 1.year.from_now.httpdate } }
end
end
Production.rb
YmcaView::Application.configure do
config.action_controller.asset_host = "//#{ENV['FOG_DIRECTORY']}.s3.amazonaws.com"
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
config.assets.js_compressor = :uglifier
config.assets.compile = true
config.assets.digest = true
config.assets.version = '1.0'
config.log_level = :info
config.i18n.fallbacks = true
config.active_support.deprecation = :notify
config.log_formatter = ::Logger::Formatter.new
end
Output when running rake assets:precompile RAILS_ENV=production
I, [2014-03-04T13:04:41.176230 #6085] INFO -- : Writing /home/richardlewis/Rails/ymca_view/public/assets/760x380_10-477c3cdc939905d6b32f9997e7f93072.jpg
I, [2014-03-04T13:04:41.177963 #6085] INFO -- : Writing /home/richardlewis/Rails/ymca_view/public/assets/aboutus-d9ad504fcd86071255015d24780caef8.jpg
I, [2014-03-04T13:04:45.018794 #6085] INFO -- : Writing /home/richardlewis/Rails/ymca_view/public/assets/application-90d317f561a8b0e84124ce7bb872f867.js
I, [2014-03-04T13:04:46.666640 #6085] INFO -- : Writing /home/richardlewis/Rails/ymca_view/public/assets/application-b422f803b56ae2f5c56a648891ec553e.css
[fog][WARNING] Unable to load the 'unf' gem. Your AWS strings may not be properly encoded.
Yep we've got it working in Rails 4
You need to do this:
#GemFile (might need aws_sdk gem too)
gem "asset_sync", "~> 1.0.0"
#config/environments/production.rb
config.action_controller.asset_host = "//#{ENV['FOG_DIRECTORY']}.s3.amazonaws.com"
$-> rake assets:precompile RAILS_ENV=production
We use Figaro to store ENV vars locally, here's what we got:
#config/application.yml
FOG_DIRECTORY: "***BUCKET_NAME****"
FOG_PROVIDER: "AWS"
FOG_REGION: "eu-west-1"
ASSET_SYNC_GZIP_COMPRESSION: "true"
ASSET_SYNC_MANIFEST: "true"
ASSET_SYNC_EXISTING_REMOTE_FILES: "delete"
I didn't need to use the 1.0.0 version. I used the latest gem, it seems that it doesn't actually upload (at least on EY) when it is in the :assets group. I had to add it to the general group in my Gemfile and it worked fine.
Related
I've started working on a rails 4.0.13 app and we're trying to move the assets to a CDN
the problem appears on our staging server , the generated url are missing the prefix and digest
Ex:
https://xxyyzz.blob.core.windows.net/stylesheets/application.css
on my local machine using the same environment as on the staging server everything works fine
Ex:https://xxyyzz.blob.core.windows.net/c532aef6b12d99b3ce3f05b4fc17c02d8a682b13/application-dd1ee03f18e6ee4df65b6a21d8cfcdeb.css
config/environments/dev_1.rb :
...
config.cache_classes = true
config.eager_load = true
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
config.serve_static_assets = false
config.assets.compile = false
config.assets.digest = true
config.assets.debug = false
config.assets.version = '1.0'
config.log_level = :info
config.i18n.fallbacks = true
config.active_support.deprecation = :notify
config.log_tags = [CustomTagger::USER_TOKEN_LAMBDA, CustomTagger::PROCESS_PID]
config.cache_store = :redis_store, RedisStores.new.url(db: 3), { expires_in: 1.day, driver: :hiredis }
config/application.rb :
...
config.assets.enabled = true
config.assets.precompile = Proc.new do |filename, path|
case
# JS
when /application-.+\.js/ =~ filename then true
when filename == 'vendor.js' then true
# CSS
when filename.end_with?('master-default.css') then true
when filename == 'application.css' then true
# IMG
when /assets\/images\// =~ path then true
# Exclude everything else.
else false
end
end
config.asset_host = 'https://xxyyzz.blob.core.windows.net'
# Each environment must have a different asset path (the SHA1 digest of
# Rails.env + Rails.application.revision).
#
# See http://guides.rubyonrails.org/configuring.html#rails-general-configuration.
# See https://github.com/rails/sprockets-rails/blob/master/lib/sprockets/railtie.rb#L174-L186
config.after_initialize do
ActiveSupport.on_load(:action_view) do
self.assets_prefix = Digest::SHA1.hexdigest(Rails.env + Rails.application.revision)
end unless Rails.env.development?
end
config.autoload_paths += %W(#{config.root}/lib)
config.middleware.insert_before Rack::Sendfile, 'HttpMethodNotAllowed'
Has anyone else encounter this ?
Found out what the problem was https://github.com/rails/rails/issues/15873
It seems rails checks if the file is present in public/assets and if it's not it doesn't set the prefix & digest .
similar issue : https://serverfault.com/questions/638905/does-rails-4-asset-path-helper-uses-asset-prefix
A possible solution would be keeping the manifest.json file in the repo
or adding a deploy task that precompiles only the manifest file .
https://github.com/rails/sprockets-rails/issues/107
I use bootstrap-sass to display glyphicons, it works well at localhost under development, but after i deploy it to a server use production, it display as a little square.
I have already search for answers, they just tell me to precompile something. But they didn't work.
This is my production.rb
OperationBackend::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 = false
config.assets.js_compressor = :uglifier
config.assets.compile = false
config.assets.digest = true
config.log_level = :info
config.i18n.fallbacks = true
config.active_support.deprecation = :notify
config.assets.precompile += ["*.woff", "*.eov", "*.svg", "*.ttf"]
config.log_formatter = ::Logger::Formatter.new
config.active_record.dump_schema_after_migration = false
Paperclip.options[:command_path] = "/usr/local/bin"
end
I also try make config.assets.complie = true
and do RAILS_ENV=production rake asset:precompile but they all didn't work
This problem torture me hours! Wish to got help from you guys!
config.assets.compile = true
should help
This did it for us. The glyphicons are in the vendors folder, so this is what we had to do to get them to work.
For some reason this worked for our production environment, but not our staging. Our staging.rb file is exactly the same as the production.rb file so this is quite the mystery.
# Disable Rails's static asset server (Apache or nginx will already do this).
config.serve_static_assets = false
# Compress JavaScripts and CSS.
config.assets.js_compressor = :uglifier
config.assets.css_compressor = :sass
# Do not fallback to assets pipeline if a precompiled asset is missed.
config.assets.compile = false
# Generate digests for assets URLs.
config.assets.digest = true
config.assets.precompile << lambda do |filename, path|
path =~ /vendor\/assets/ && !%w(.js .css).include?(File.extname(filename))
end
We've just deployed a Rails 4.0.3 app to production and have found that asset paths generated by stylesheet_link_tag and javascript_link_tag are missing their fingerprints. So instead of requesting something like application-c841bd1c82c25bb1de8452d2338479f7.js, the page is just request application.js.
RAILS_ENV=production bundle exec rake assets:precompile is successful and generates fingerprinted files.
The bits from config/environments/production.rb that seem relevant are:
# 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
# Disable Rails's static asset server (Apache or nginx will already do this)
config.serve_static_assets = false
This is running on our own Apache server, not Heroku. I've looked around quite a bit and found similar problems, but none of the troubleshooting steps for those are helping here. Thanks.
More Information
In case it is helpful, here are the full contents (commented lines removed) of our config files:
application.rb
require File.expand_path('../boot', __FILE__)
require 'rails/all'
if defined?(Bundler)
# If you precompile assets before deploying to production, use this line
Bundler.require(*Rails.groups(:assets => %w(development test)))
# If you want your assets lazily compiled in production, use this line
# Bundler.require(:default, :assets, Rails.env)
end
module Ctrc
class Application < Rails::Application
config.ceal.application_title = "CTRC Budgeting"
config.encoding = "utf-8"
config.filter_parameters += [:password]
config.active_support.escape_html_entities_in_json = true
config.assets.enabled = true
config.assets.version = '1.0'
config.pmacs_redmine.project_identifier = 'itmat-gcrc'
config.app_data_path = '/data/web/apps/itmat/ctrc'
config.paperclip_defaults = {
path: "/data/web/apps/itmat/ctrc/:attachment/:id/:style/:basename.:extension"
}
if ENV['RAILS_RELATIVE_URL_ROOT']
config.assets.prefix = ENV['RAILS_RELATIVE_URL_ROOT'] + '/assets'
end
end
end
production.rb
Ctrc::Application.configure do
config.cache_classes = true
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
config.serve_static_assets = false
config.assets.compress = true
config.assets.compile = false
config.assets.digest = true
config.i18n.fallbacks = true
config.active_support.deprecation = :notify
config.eager_load = true
end
I know that the application is running in production mode because if I set
config.assets.compile = true
in that file, the CSS and JavaScript are compiled and requested correctly.
I'm including those assets in the page like so:
<%= stylesheet_link_tag "application", media: "all" %>
<%= javascript_include_tag "application" %>
but it is still generating links to those assets like this:
<link href="/apps/itmat/ctrc/stylesheets/application.css" media="all" rel="stylesheet">
<script src="/apps/itmat/ctrc/javascripts/application.js"></script>
instead of the fingerprinted links I would expect to see.
I had a similar issue and it turned out to be a problem with the gem AssetSync. Either setting config.assets.compile = true or removing the gem resolved the issue. Compiling assets on the fly and having your Rails serve your assets might be acceptable if you're using a CDN, but generally, taking another approach is usually recommended.
For rails 4.x be sure to set the following:
config/application.rb
config.assets.enabled = true
config.assets.version = '1.0'
config/environments/production.rb
config.assets.compile = false
I'm going a little nuts here.
I have 2 servers (heroku, same configuration) in 2 environments (staging, production).
Staging compiles my /assets/templates on demand and caches it without a problem.
Production serves the file uncompiled as html (being haml markup)
The problem is: the configuration of the 2 environments is identical (except mail server)
Gems, environment config and yml are the same.
Any ideas?
Update:
The haml assets where "precompiled" on both servers but raw. Staging didn't get the file missing and re-compiled because assets where compiled for production.
Now I'am stuck with adding haml to the asset compile pipeline with initialize_on_precompile false.
staging.rb / production.rb
config.cache_classes = true
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
config.serve_static_assets = true
config.static_cache_control = "public, max-age=2592000"
config.assets.compress = true
config.assets.compile = true
config.assets.digest = true
config.assets.precompile += %w( jquery.js modernizr+respond.js polyfills.js )
config.assets.precompile += %w( gp.js ad.js richmarker.js infobox.js )
config.assets.css_compressor = :yui
config.assets.js_compressor = :uglifier
config.assets.compress=true
config.threadsafe! unless $rails_rake_task
config.i18n.fallbacks = true
config.logger = Logger.new(STDOUT)
config.logger.level = Logger.const_get(ENV['LOG_LEVEL'] ? ENV['LOG_LEVEL'].upcase : 'INFO')
config.active_support.deprecation = :notify
config.action_dispatch.rack_cache = {
:metastore => Dalli::Client.new,
:entitystore => 'file:tmp/cache/rack/body',
:allow_reload => false
}
Haml::Template.options[:ugly] = true
Solution was to add the haml into the load. Add the following to application.rb
Sprockets.register_engine '.haml', Tilt::HamlTemplate
My app works dandy in development but yesterday I tried to move it to my production server and after some work with permissions and paths I though I got it to work.
But it seems like I'm unable to load the JS assets while images and css works fine.
At first I thought it was the bootstrap js file that made my life misserable but removing it just gives me an error at the next JS asset.
https://dl.dropboxusercontent.com/u/32242733/jsfail.png
Production.rb
# 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 = true
# Generate digests for assets URLs
config.assets.digest = true
# Defaults to nil and saved in location specified by config.assets.prefix
# 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 = :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
config.action_mailer.default_url_options = { :host => 'example.com' }
# ActionMailer Config
# Setup for production - deliveries, no errors raised
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = false
config.action_mailer.default :charset => "utf-8"
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
domain: "example.com",
authentication: "plain",
enable_starttls_auto: true,
user_name: ENV["GMAIL_USERNAME"],
password: ENV["GMAIL_PASSWORD"]
}
# Log the query plan for queries taking more than this (works
# with SQLite, MySQL, and PostgreSQL)
# config.active_record.auto_explain_threshold_in_seconds = 0.5
Assets
root#flypatterns:/var/www/flypatterns/current# ls public/assets/
application-35684ef44e5902293fd317c512600f66.css glyphicons-halflings.png
application-35684ef44e5902293fd317c512600f66.css.gz glyphicons-halflings-white-6cccd17a7aed91dbc0157d343c68c0d9.png
application-66ba1e6c258ba515e18111b4b93c8c57.js glyphicons-halflings-white.png
application-66ba1e6c258ba515e18111b4b93c8c57.js.gz images
application.css jquery.min-6c267bfd2b3f36e6edccb2e584934c1c.map
application.css.gz jquery.min.map
application.js manifest.yml
application.js.gz patterns
gallery_images users
glyphicons-halflings-2851b489e8c39f8fad44fc10efb99c3e.png
i follow this step for Heroku
hello do follwing changes before deploy following file
------enviorment.rb-----
::ActiveSupport::Deprecation.silenced = true
------Production.rb-------
config.assets.compile = ['*.js', '*.css']
config.active_support.deprecation = :silence
-------application.rb-------
config.assets.enabled = true
config.assets.initialize_on_precompile = false
try changing
config.serve_static_assets = false
to
config.serve_static_assets = true
I encountered a problem with my app while pushing it to heroku. I had used twitter-bootstrap, there was some errors while precompiling assets. So i switched to manual compiling rather than automatic precompiling. For that i did the following:
I had set initialize_on_precompile to false in config/application.rb as follows:
config.assets.initialize_on_precompile = false
and then i did manually by running:
bundle exec rake assets:precompile RAILS_ENV=production
Finally it worked...
This maynot be the solution you looking for unless you use any predefined front-end frameworks.
check your application.js file i had added some code to do a java script code that added to some funk weird file. the problem was i was missing an "/" for some code that must have been commented out :-( stupid me it took forever
bundle exec rake assets:precompile RAILS_ENV=production
showed me what file and line to look at and i opened that file in sublime. I hope this helps took me5 days to find this damn error smh