How to use assets in open shift rails - ruby-on-rails

I was deploying my code in openshift rails but it did not take the assets properly.
It's neither loading the javascripts nor the images.
How to make it work?

The first thing you will want to check is if the assets exist on your application. This script is run before the build.sh script (mentioned next). It attempts to create a symlink for your public/assets folder (here is the reasoning behind this). Even if you had that directory, the rake task would still work; the assets would just be wiped every push.
Your assets should be compiling whenever you push to your git repository. This is taken care of by this script in the Ruby 1.9 cartridge (this is run by the service and you have no control over it). As you can see, it will run bundle exec rake assets:precompile as long as you have a Gemfile (which should be the case for all Rails apps).
With all that being said, the first thing you should do is check your .openshift/action_hooks to make sure you are not running anything that might be overwriting your public/assets directory. You can compare them against the ones here.
The next thing you should do is actually check the directory on your OpenShift host. You can do this by SSHing into your app (instructions are here). Then check your public/assets directory. Note: Some of the output has been shortened with .....
# First we make sure it is a symlink.
> file $OPENSHIFT_REPO_DIR/public/assets
..../app-root/runtime/repo/public/assets: symbolic link to `..../data//assets'
# Then we see if there is anything in it
> ls $OPENSHIFT_REPO_DIR/public/assets
.... (should have a bunch of.js, .css, etc files)
If that directory is empty, maybe there is a problem with compiling the assets. You should pay attention to the output when you git push and see if there is any indication that it is failing (you may want to capture the output using tee like: git push 2>&1 | tee git_push.log). If there are assets in the directory, check your logs by following these steps.
If you're still having problems, swing by our IRC channel and somebody should be able to help in person.

Related

Parallax.js images render locally but not in production on Heroku

Disambiguation: There are two popular plugins called "parallax.js". I'm referring to this one.
In development, parallaxed and non-parallaxed images work perfectly in my ERB template. But in production (on Heroku), only the non-parallaxed images are rendering. I currently suspect my problem is with the Rails asset pipeline, but the recommended fixes haven't worked (see below).
The parallax.js docs suggest the following usage:
<div class="parallax-window" data-parallax="scroll" data-image-src="/path/to/image.jpg"></div>
But in order to use the asset pipeline, I've changed it to:
<div class="parallax-window" data-parallax="scroll" data-image-src="<%= image_url('image_file.jpeg') %>"></div>
(In addition to image_url, I've also tried image_tag and image_path.)
As mentioned, this all works just fine on my local machine.
But in production, the Chrome browser console logs errors for the missing images:
Failed to load resource: the server responded with a status of 404 (Not Found)
So I went down the rabbit hole:
Other SO pages (ie: here and here) suggest doing variations of rake assets:precompile RAILS_ENV=production or heroku run rake assets:precompile.
The latter command spends several minutes compiling unrelated css & js assets, and does not resolve the problem. The former command yields the following error:
Devise.secret_key was not set. Please add the following to your Devise initializer:
config.secret_key = (... and so on)
Using the heroku console I ensured proper values ARE present for ENV['SECRET_KEY'] and ENV['SECRET_KEY_BASE'].
In my devise.rb, I have:
config.secret_key = ENV['SECRET_KEY_BASE'] if Rails.env == 'production'
In production.rb I have:
config.serve_static_assets = true
config.assets.compile = true
config.assets.digest = true
This might be more info than necessary but I've included this since it all seems related.
Thank you.
Path used from Server to run the search
Your server does not find the file with the path your are giving him so please let's tell us where you are searching by with the console in both production and development type
Rails.application.config.assets.paths
Then evaluate any difference in those paths, because that is where the server is going to search for your asset
The file does not exist in production
If you set in development this parameter to false, it may also not work in development, demonstrating that the server is not finding the image also in that environment and is falling back to the file in app/assets
config.assets.unknown_asset_fallback = false
I wonder is the file present in your git repository? Because you are uploading to production through git, so you can check the file in your git repository under public/assets then you can go to your website, for example https://sprachspiel.xyz has an image
<img class="image_inverted" src="/assets/building-14c0a7cb155ad93ffbad41ab5ecb491691f2fc34684c7aa5735fedc659f99f76.svg" alt="Building">
and I can display the asset with the following path:
https://sprachspiel.xyz/assets/building-14c0a7cb155ad93ffbad41ab5ecb491691f2fc34684c7aa5735fedc659f99f76.svg
if you can not find the fingerprinted file in your git repository or you can not display it on your server/domain, it mean that the development server is just using the one in the app/assets folder and the file has not been precompiled/fingerprinted
so you need to make sure when you precompile development
rails assets:precompile
that you include those fingerprinted files in your git repository
git add .
then you need to check the files added
git status
should show in green the new precompiled files, so it will include some application-fingerprint.js .scss and also some images
then you need to commit
git commit -m 'assets precompile'
and push to production git push heroku master
Now on your heroku host you have exactly the same files from your git repository, you should check the heroku log in details and seeing what file are being uploaded, then you can go on your app domain and should be able to display all the files if you go at for example
https://sprachspiel.xyz/assets/application-461aa436b906133a708293d3f509c1908b34cf5b3d06c38332fcd7a5d75648a9.js
The problem of this is that as you can see from my git repository
https://github.com/fabriziobertoglio1987/sprachspiel/tree/master/public/assets
I have many application.js and scss, and same version of the same images with different fingerprints. That is why sometimes they tell you to delete the folder with rm -r public/assets, git add ., git commit -m 'deleted assets', then push to both github and heroku
Because it is confusing to have some many versions of the same file with different fingerprints, because heroku will only upload 1 file for application.js, scss and each image based on the settings included in the sprockets-manifest-fingerprint.json
which includes the fingerprint name of every file
{"files":{"anvil-24236-40f6914d480cedd181af319818100046542752b1af2618a9540560b9fe17441f.svg":{"logical_path":"anvil-24236.svg","mtime":"2017-06-19T00:05:49+07:00","size":3886,"digest":"40f6914d480cedd181af319818100046542752b1af2618a9540560b9fe17441f","integrity":"sha256-QPaRTUgM7dGBrzGYGBAARlQnUrGvJhipVAVguf4XRB8="},"anvil-4e059374f73b4972cad762b9ab5326dfce178997ad7dc902e5fd50c0db019c3d.svg":...}
https://github.com/fabriziobertoglio1987/sprachspiel/blob/master/public/assets/.sprockets-manifest-b19e945ef55d302416370238b2625751.json

Travis Failing Due to 'require' file LoadError after bundling

/home/travis/.rvm/rubies/ruby-2.2.2/bin/ruby -I/home/travis/.rvm/gems/ruby-2.2.2/gems/rspec-core-3.4.2/lib:/home/travis/.rvm/gems/ruby-2.2.2/gems/rspec-support-3.4.1/lib /home/travis/.rvm/gems/ruby-2.2.2/gems/rspec-core-3.4.2/exe/rspec --pattern spec/\*\*\{,/\*/\*\*\}/\*_spec.rb
/home/travis/build/jmoon90/seat_geek/lib/seat_geek.rb:13:in `require': cannot load such file -- seat_geek/taxonomies/tree (LoadError).
The command "bundle exec rake" exited with 1.
I'm setting up Travis CI for my SeatGeek wrapper gem. However, when the CI builds, it bundles correctly, but when it runs the rake tasks, it fails saying it can not load such file.
I'm not sure why that is the case because I make it available in the master file.
It's weird to me that the seat_geek/query/ folders aren't blowing up before that.
What is the problem here?
Travis expects the path to be all lower case. The problem was Github did not update my folder name from Taxonomies/concerts to taxonomies/concerts even after I've made the change and pushed it up.
Github doesn't seem to care if it's uppercase or not. So I had to change the folder name to taxonomiess and push that up. Then after that change it back to taxonomies with lower case.

Why does the Rails 3 asset pipeline always regenerate assets even when nothing has changed?

I'm running a Rails 3.2 app on Heroku, and thus am required to use the asset pipeline.
One thing that has seriously confused me for a while now about the Rails asset pipeline is the result of this little experiment:
git rm -r public/assets
git commit -m "Nuke all generated assets"
RAILS_ENV=production bundle exec rake assets:precompile
git add .
RAILS_ENV=production bundle exec rake assets:precompile
Specifically, if I examine the difference between my staged and unstaged changes, I find that the second asset precompilation has regenerated/refingerprinted/reminified all my CSS and JS.
Why should that occur when I changed nothing between asset compilations?
Obviously the current timestamp is somehow involved, but why is that so when - again - I changed nothing?
Finally, is there any way to change asset precompilation such that CSS and JS assets are only regenerated if they were changed since the last precompilation?
You can do it using a Capistrano, and basically checking the current deploy against the old version and see which files changed. This is where I learned about it.
https://github.com/capistrano/capistrano/issues/227
http://www.bencurtis.com/2011/12/skipping-asset-compilation-with-capistrano/
It will change, because every time you create a new gzip file, even if the contents are the same, the resulting file will be different, and source control systems don't know how to deal with this (and it's not supposed to do it).
You should take a look at this answer to know what files to add to gitignore.
Also you can take a look at this discussion about it in github.

From manual pull on server to Capistrano

I've always deployed my apps through SSH by manually logging in and running git pull origin master, running migrations and pre-compiling assets.
Now i started to get more interested in Capistrano so i gave it a try, i setup a recipe with the repository pointing to github and deploy_to to /home/myusername/apps/greatapp
The current app on the server is already hooked up with Git too so i didn't know why i had to specify the github url in the recipe again, but i ran cap deploy which was successful.
The changes didn't apply, so out of curiosity i browsed to the app folder on the server and found out that Capistrano created folders: shared, releases and current. the latter contained the app, so now i have 2 copies one in /home/myusername/apps/greatapp and another in /home/myusername/apps/greatapp/current.
Is this how it should be? and I have to migrate user uploads to current and destroy the old app?
Does Capistrano pull the repo on my localhost then upload it through SSH or run pull on the server? in other words can someone outline how the deployment works?
Does Capistrano run precompile:assets?
/releases/ is for previous versions incase you want to do cap:rollback.
/current/ as you rightly pointed out is for the current version of your app.
/shared/ is for files and folders that you want to persist between deployments, they typically get symlinked to your /current/ folder as part of your recipe.
Capistrano connects to your server in a shell and then executes the git commands on the server.
Capistrano should automatically put anything in public/system (the rails convention for stored user-uploaded files) into the shared directory, and set up the necessary symlinks.
If you put in the github url, it actually fetches from your github repo. Read https://help.github.com/articles/deploying-with-capistrano for more info.
It does, by default.

How do you catch errors in the rails asset pipeline before production?

I'm just getting acquainted with Rails 3.1, and I burned some time updating an old project and trying to work out how the new asset pipeline behaves in development mode versus production.
The default config.assets.precompile setting blesses only application.css and application.js, with the intention that everything should be served as a single stylesheet and a single javascript file.
Obviously there are situations when we don't want that, so we can add items to the list in that config variable...
Here's the situation I ran into with my sandbox project when going to production:
Browsed the site in development, saw that everything was working. The assets were linked as separate files and the site displayed correctly.
Uploaded the site to my server, and tried to get it working in production. The first error was saying that "ie.css" (a conditional stylesheet) isn't precompiled. (I was in Safari and this stylesheet wouldn't even be downloaded: the error was raised from the stylesheet_link_tag helper before rendering the page.)
Ran rake assets:precompile and tried again.
Added the offending item to config.assets.precompile and tried again.
Kicked the error down the curb until it hit another asset error.
GOTO 3.
Not knowing how to address this, I went around in circles a few times until I thought I got all the assets and the site was rendering in production. Then I tried it in MSIE and hit another error 500: "belated_png_fix.js" was being conditionally loaded, and it didn't crop up until then.
So my question is, other than trial and error or a heavy dependence on integration testing, how can I predict that my site isn't going to bomb out when the asset pipeline discovers that some stylesheet or javascript wasn't added to the precompile list?
I'm also curious why a missing stylesheet asset should cause the whole page to error 500 instead of just compiling it on-demand or serving a 404 when that asset is requested. Is this a deliberate design to "fail early"?
I've just released a gem called assets_precompile_enforcer, which ensures that developers won't forget to add assets to config.assets.precompile while they are developing. An exception will be raised if you include an asset via javascript_include_tag or stylesheet_link_tag, and it doesn't match a filter in config.assets.precompile.
This means that asset errors will be caught during development, instead of being discovered after deploying to production.
I had similar problems with rails 3.1 as well. The best thing you could do is to install capistrano multi stage and get a staging server.
If for any reasons this is not possible, install a virtual machine on your computer and try to replicate your servers environment.
Continuous deployment is a great thing, and you should get to the point where it is so simple that it really isn't that painful anyway. That being said, config.assets.precompile can take regexs, so how about you come up with a standard for top level sprockets "manifest" files, or a standard sub folder for things that will not be bundled up? (note that I haven't actually tried this yet...)
This may be overkill, but this works for me (it gives me clean, compiled assets). I have this in my .bash_profile file.
alias ggo='bundle exec rake assets:clean && bundle exec rake assets:precompile && git add . && git commit -m "precompile" && git push origin master && cap deploy'
and this in my config/environments/production.rb (forces production to compile when needed; shouldn't be a need to if I remember to run "ggo" first):
config.assets.compile = true
So, my workflow is:
1. code
2. git add & git commit
3. if I touched CSS/SASS/JS/CoffeeScript files, I run ggo. Otherwise, I do a normal cap deploy.

Resources