Rails 5.2 Asset Pipeline images not loading on heroku - ruby-on-rails-5

I am working with a Rails 5.2 application
Locally I added a new image to app/assets/images/my-file-name.jpg
and then in my .erb file I reference it using
<%= image_tag "my-file-name.jpg" %>
Then when I deploy to Heroku I run the following steps locally
rake assets:clobber <!-- this destroys the public/assets folder as expected
RAILS_ENV=production bundle exec rake assets:precompile this creates a new public/assets folder but now each of the files within the assets folder have a hash after their name. So my file my-file-name.jpg has become my-file-name-{BigLongCacheBustingHashHere}.jpg
git push staging <-- Deploy to my staging environment and check that the log says assets compiling -- all good.
Check staging environment if the image is being served and I notice that the app cannot find my image because my <%= image_tag my-file-name.jpg %> is still producing a static html link WITHOUT the hash. It looks like this <img src="my-file-name.jpg"> when I believe it should be producing a tag that looks like <img src="my-file-name-{BigLongCashBustingHashHere}.jpg"
Why do you think the <%= image_tag %> is not producing production friendly urls?
I can see that it is working elsewhere in the app so not sure where I am going wrong.

OMG I lost hours of my life on this. Rails assets does not like .jpeg extensions and will flat out ignore them in production. So I just changed the file name to my-file-name.jpg and now Rails is happy

Related

Asset was not declared to be precompiled in production (Rails 5.0.0.1)

I am new to Rails 5.0.0.1
I followed the guidelines in http://guides.rubyonrails.org/asset_pipeline.html#controller-specific-assets:
You can also opt to include controller specific stylesheets and JavaScript files only in their respective controllers using the following:
<%= javascript_include_tag params[:controller] %> or <%= stylesheet_link_tag params[:controller] %>
When doing this, ensure you are not using the require_tree directive, as that will result in your assets being included more than once.
I generated some scaffolds (which also generated the assets -- .coffee and .scss)
When I do rails s and access one of the scaffold's main page, it generated an error:
Asset was not declared to be precompiled in production.
Add Rails.application.config.assets.precompile += %w( myscaffold.css ) to config/initializers/assets.rb and restart your server
I do as it said, and it worked, but is there any other way to simplify this process?
I wish that I can generate some other scaffolds or controllers with their assets without doing any more concern of adding new lines inside the config/initializers/assets.rb.
I will receive any other alternatives too.
you just try this..
check your Gemfile gem 'sprockets-rails', '2.3.3'
And bundle install
Navigate files
config/initializers/assets.rb
Then add
Rails.application.config.assets.precompile += %w( your_file_name.scss )
Restart server

using asset pipeline and public/images with jruby & warbler

I'm doing some work on a RoR application that gets deployed as a war file using warbler. Prior to my involvement, the few images in the app were in the asset pipeline, but my role is to introduce slippy maps and homegrown map tiles.
Because the tiles take up a lot of space, and because they won't change anywhere near as often as the app, the idea was to simply serve them from public/images/tiles. The war file for the rest of the app would be deployed, then the tiles would be untarred into the proper directory.
This works fine in development mode, but the warbler deployment uses a context path and the tiles get 404ed because they're found at /contextpath/images , not /images
I haven't worked in a jruby environment before and the rest of the team just so happens to be out on vacation this week (ha!). Nick Sieger's recommendation about using config.action_controller.asset_host at Warbler: Where are my images wrecks the images and css that do work via assets, so I'd be grateful for any other suggestions that allow assets and public to coexist.
In production.rb try this line config.assets.precompile += %w( *.js *.scss *.coffee *.css ) then run jruby -S rake assets:precompile.
It will compile all you assets to static assets and warbler will then package them accordingly, it should solve your problem.
Another thing you can check is how you are pointing to the images in the Views. if you want them to be found in /image you should write the path as <%= image_tag "\image" %>. Regard the trailing \.

Rails3 couldn't find file 'bootstrap'

This has hit me twice and I cannot get past it this time. The first time, I pushed/pulled from github and got webrick to boot up. This a.m. I attempted to do the same thing. I do recall restarting my dev machine yesterday which would cause webrick to restart.
Background, this is my first 'production' app and I'm using danielkehoe's starter template on github that uses cucumber, rails3 and devise. I've chosen Twitter-bootstrap as my CSS generator.
Hunting on the web suggested bundle update, rebooting webrick. Of those google results that directly mirror mine, there was no resolution posted. I have branched out and installed rails 3.2 but that made no difference.
Here is the Exception:
couldn't find file 'bootstrap'
(in /Users/sam/apps/keriakut/app/assets/stylesheets/application.css.scss:6)
Extracted source (around line #10):
9: <meta name="author" content="">
10: <%= stylesheet_link_tag "application", :media => "all" %>
11: <%= javascript_include_tag "application" %>
My trace:
app/views/layouts/application.html.erb:10:in
`_app_views_layouts_application_html_erb___2282066720260402873_2178181480'
I would have thought that caching is not used in development mode and doesn't that file look like it's cached?
It seems there is no bootstrap document for twitter-bootstrap, but it seems to be incorporated into sass, scss files? This is my inexperience showing.
Based on another stackoverflow answer I added: gem "twitter-bootstrap-rails", "~> 2.0rc0" into my Gemfile but after updating, the results are not the same. I want to understand what I'm doing wrong with my environment to keep this coming up, sam
I think you're including too many unrelated details. Sounds like you have /*= require twitter/bootstrap or similar in your application.css.scss manifest and it can't be found. Bootstrap doesn't come with Rails or Sass. You can either drop it manually into your stylesheets directory from the Bootstrap website and require it or you can use one of the gems out there that contains it.
Since you're using the twitter-bootstrap-rails gem, the documentation says that you install it by running:
rails generate bootstrap:install
That will probably unpack the bootstrap css files from the gem into your stylesheets directory.
And your application.css.scss should contain:
*= require twitter/bootstrap

Preventing Heroku from using precompiled assets in development mode

Currently it seems Heroku is determined to pre-compile assets when I push my code up to my instances.
This is great for production servers, however for my "RAILS_ENV=development" server, this causes issues, as I now get pages with all the JavaScript files served up individually from my asset manifest, and then another file with the same code all grouped up as the pre-compiled asset.
This cause my jquery datatables libraries to break, throwing popup errors, which I don't get on my local environment (development or production) or in my production Heroku instance.
Is there anyway to disable pre-compilation of assets on Heroku for development mode instances ? Or is there any reason why these aren't already disabled on development Heroku servers already ?
If Heroku detect a public/assets/manifest.yml file then they will not attempt to precompile your assets and assume you are dealing with them yourself. More details at http://devcenter.heroku.com/articles/rails31_heroku_cedar
AFAIK, Heroku has to precompile assets to work around their readonly FS and the fact that the Rails asset pipeline wants to write files to the FS. The only thing I could suggest would be to work out why your assets are breaking when being compiled.
I worked around this by adding some voodoo to my Rakefile to disable the assets:precompile rake task.
first I add the user-env-compile labs component
heroku labs:enable user-env-compile
then add this to the beginning of my Rakefile
# found from http://blog.jayfields.com/2008/02/rake-task-overwriting.html
# used by conditional heroku asset compile magick
class Rake::Task
def overwrite(&block)
#actions.clear
enhance(&block)
end
end
Then I add this rake task in lib/tasks/disable_assets_on_heroku.rake
if ENV['RAILS_ENV'] == 'development'
namespace :assets do
task(:precompile).overwrite do
puts "Asset precompile skipped in #{ENV['RAILS_ENV']}"
end
end
end

cache manifest with asset pipeline

What would be the best way to have a html5 cache manifest with the rails asset pipeline? I'm thinking of adding an erb file to app/assets that has the paths of all the assets contained in it. This would work but has a couple problems right off the bat:
How could I increment the version number?
How can I make sure the http content type is set correctly?
Here's how we're doing it on an app right now:
To handle the generation of the cache manifest file, we are using Rack::Offline
We then configure this to point to, for instance, "/assets/application.css"
In the layouts/views, we are NOT using the stylesheet_link_tag, javascript_include_tag or image_tag helpers for cache-able assets so that we don't get a link to the assets with the hash in it, eg "/assets/application-2345234...2344.css"
This works because when the assets are precompiled, rake assets:precompile:nondigest creates versions of the files without the hash in the name, and then Rack::Offline checks these to generate a new manifest (or not).
Sprockets provides you with one by default.
in one of your environment configs (/config/environments/development.rb)
config.assets.compress = false
config.assets.debug = false
and in your html file:
<html manifest="manifest.yml">
running
rake:precompile
will give you a manifest file to public/assets/manifest.yml