Referencing rails assets without the digest? - ruby-on-rails

I am referencing assets in my rails app directly, for ex. background-image: url('/assets/bg.png').
But I just realized this wouldn't work in production since digested assets are served (/assets/bg-dddasd434r4tfdfs...sada.png) in production.
Is my only choice to use helper methods (for ex, image-url) throughout the application or is there a more simpler solution to this?

You can try asset_path('app.js', :digest => false) or disable digest in the prodution env:
# config/environments/prodution.rb
config.assets.digest = false
Rails pipeline

In config/application.rb change the value of
config.assets.digest to false

This is an ancient question, but since I happened upon it with Google...
In Rails 5 there is the asset_path helper which even in the .scss files can return the proper path to a file.

Related

Hashed assets on multi-site Rails site

Rails: 4.2.7.1, Ruby: 2.3.1
We used to use non-hashed assets for serving our assets on our multi-site application by hardcoding the path to the asset in the individual site's headers. We are now pre-compiling hashed assets with Sprockets on each deploy in production with these settings:
production.rb
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
config.serve_static_files = true
config.assets.prefix = "/assets"
config.assets.digest = true
config.assets.initialize_on_precompile = true
We end up with hashed assets, for example, in /public/assets/skins/site1/skin-70ee38249235f8521a243a8f955ed08c.css.
We have a skin file for each path that we use to load our styles per site. We do generate a site application.css that remains unused. Each skin file contains variables for each site and then imports the site's manifest file containing the sass stylesheets.
What we tried to do was create a way for our CMS to look for the hashed asset:
stylesheet_link_tag("/assets/skins/site1/skin.css", media: "all")
The issue is that the site is still looking for the non-hashed skin file. If we have the plain skin.css, it will work.
Same goes for Javascript:
javascript_include_tag "/assets/admin.js"
It will look for the non-hashed asset, even though the hashed asset is present. I tried to remove the "/assets" part from the link tags and it will fail.
Is there a setting that we are missing to have Rails look for the proper hashed asset? Any advice is appreciated.

duplicate Rails 4 asset fingerprint file on Heroku

I'm getting duplicate asset fingerprint javascript file on Heroku production.
This initially creates around 3-4 files then after a while (a day), it creates another set of those files again. Also every time I refresh those files get rotated in the source.
On production.rb:
config.assets.enabled = true
config.assets.digest = true
config.action_controller.asset_host = "//#{ENV['FOG_DIRECTORY']}.s3.amazonaws.com"
config.assets.initialize_on_precompile = true
config.assets.precompile += %w( '.woff', '.eot', '.svg', '.ttf', '*.css.scss', application_user.js, popcorn.js )
On application.rb:
config.assets.enabled = true
config.assets.digest = true
Surely this doesn't matter?
Structure
The Rails structure is such that it should allow you to use whatever fingerprinted file you need, and it will show (using the dynamic javascript include helpers)
If you're unable to read a particular file because it's not exactly the same as it was before is, in my opinion, a highlight of a poor system design
Files
I think I remember your issue from another day -- you can just use the helper method to call the files you need. It shouldn't cause any issues with the different names. It's all part of the asset pipeline
I'd recommend looking into how you're calling the files - if you're trying to call the hashed filename directly, you're going to have an issue

Different Asset Digests depending on css.sass or css

Using Rails 3.2.13 I get different asset paths for asset_path depending on whether or not i use .css.sass or just .css is this expected?
For example if I only have a app/assets/stylesheets/foo.css.sass in my stylesheet directory and nothing else, if I accidentally use an asset_path with the sass version I get a file that does not exist:
Loading production environment (Rails 3.2.13)
irb(main):001:0> helper.asset_path "foo.css.sass"
=> "/assets/foo-b148cf064a02aef053d2902b6a1fa5a9.css"
Using asset_path with just plain css produces the correct path.
irb(main):002:0> helper.asset_path "foo.css"
=> "/assets/foo-1b1fbb65d54bbc52b702c0f1d2efcc38.css"
Only foo-1b1fbb65d54bbc52b702c0f1d2efcc38.css is generated when i run rake assets:precompile. Does anyone know where the different hash value comes from?
Quick answer: I think that hash value is generated from the latest mtime of the compiled files.
Long answer: I dug around and can't confirm it, but I believe I've read that this hash-type string is generated from the latest mtime of all the file(s) you want to compile.
So my guess is foo.css is newer than foo.css.sass and so that mtime wins when precompiling.
If you want to dig further into it, I've tracked it to a digest_path method from within digest_for.

Using url_helpers in a coffeescript file with a base url in production

I am trying to use Rails url_helpers inside of coffescript files. I append the .erb extension to the filename (profile.js.coffee.erb) and use ERB to access the helpers like so:
jQuery ->
window.Intl.Models.Profile = Backbone.Model.extend
url: '<%= Intl::Application.routes.url_helpers.profile_path %>'
This works great in development and it will return /profile. In production, I am using a URL root of /intl so I tell the asset pre-compiler to consider this in deploy.rb
set :asset_env, "#{asset_env} RAILS_RELATIVE_URL_ROOT='/intl'"
However, this doesn't seem to affect the url_helpers because it will still return /profile instead of /intl/profile
I tried setting ENV["RAILS_RELATIVE_URL_ROOT"] in production.rb and development.rb respectively and that didn't work either.
Any suggestions would be appreciated. Thank you.
You may need to set this at the Rack level. If you are using Passenger, you can set the RackBaseURI directive.
Same problem here.
As a partial solution I use
set :asset_env, "#{asset_env} RAILS_RELATIVE_URL_ROOT=/csc"
in deploy.rb for Capistrano deploymment. This works fine for all other aspects of the asset precompilation except this single aspect (.coffee.erb) wich seems completely unaffected by that setting.

How to use Sprockets 2 with Rails 3.0.x (how to use precompiled assets)

I'm trying to replicate the basics of the asset pipeline introduced in rails 3.1 in my rails 3.0 app.
So far, I've got something like this:
https://gist.github.com/1112393.
It works great:
I have my assets in app/assets/, lib/assets, vendor/assets...
They're all served at /assets
I can use everything sprockets 2 offers etc...
The thing is, I don't want the rails app to serve static assets. The server should do it.
That's why you can precompile assets in rails 3.1, if I understood correctly.
So I've made a rake task that does just that (using the precompile method of Sprockets::Environment). And it works, I have all my assets at /public/assets/.
For instance, I have
application-02f8c96b342b4569513d0edf39ef55eb.css
application-505e8f472350fb1e0d15f6ad2f5e0389.js
gallery-icons-0e922050a85718fef3cd570df4eb5845.png
But in rails 3.1, you can do something like that in your style.css.scss.erb
background: url(<%= asset_path("gallery-icons.png") %>)
and you'd get
background: url(/assets/gallery-icons-0e922050a85718fef3cd570df4eb5845.png)
in the precompiled file.
Same for stylesheet_link_tag, javascript_link_tag which are overwritten in rails 3.1 to add the hash, if I'm not mistaken.
How can I do this?
Give me every idea you can have! Thanks.
Josh answered me here: https://github.com/sstephenson/sprockets/issues/151
Assets = Sprockets::Environment.new(Rails.root) do |env|
assets = ["javascripts", "stylesheets", "images", "fonts"]
paths = ["app/assets/", "lib/assets/", "vendor/assets/" ].map{|p| assets.map{|f| "#{p}#{f}" } }.flatten
paths.each{ |path| env.append_path path }
env.static_root = Rails.root.join("public", "assets")
end
So basically, I have a rake task to precompile the assets:
namespace :assets do
task :precompile => :environment do
Assets.precompile(*Rails.application.config.assets.precompile)
end
end
My problem was mainly to know how to request these assets.
The answer is quite simple:
Assets['application.js'].digest
Having the fingerprint, it's easy to get the filename.
I created helpers to include these assets: sprockets_include_tag and sprockets_image_tag.
Done deal.
(Although right now, I can't use these helpers in my stylesheets (style.css.scss.erb))
Edit: Harry Brundage did a rewrite of my gem which uses more recent versions of everything, it's probably what you want to use:
https://github.com/hornairs/sprockets-rails
Old suggestion:
I've made a gem you can include in your Rails 3.0.x Gemfile which is an extraction of the Rails 3.1 sprockets integration:
https://github.com/jamesmacaulay/sprockets_rails3_backport
There are some differences from the Rails 3.1 behaviour, but they are well documented in the README. With most of the stuff you'd want to tweak, you can just uncomment the lines I've commented out.

Resources