Rails App Assets Not Being Loaded on Development Environment - ruby-on-rails

Every time I spin up my Rails App locally, none of the CSS shows up. The debugger in the browser shows a get request for all of the asset files with fingerprinting, but they all fail since the files can't be found. Here is what one of those requests looks like:
GET http://localhost:3000/assets/announcements.self-e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855.css?body=1
In the Rails Server Console, I get a Rack App Error, as well as a NoMethodError for the undefined method 'Silence'. Here is what one of those errors looks like:
2017-01-08 17:24:49 -0800: Rack app error handling request { GET /assets/normalize.self-cadbbf81015902777c37852b3ea54398335fd421c6c76891c077ec5954211bb4.css }
NoMethodError: undefined method 'silence' for #<Logger:0x007fe1062c1c78
Both Error logs (Browser Console and Localhost Server Console) contain multiple renditions of the same error, all pointing to Assets (*.js, and *.css).
I have searched through countless StackOverflow Posts but to no avail. This also for some reason only affects this one application; all my other Rails applications run fine. Here are my specifications:
Rails Version: 5.0.1
Ruby Version: 2.3.1p112
Things I have tried:
assets:clean, assets:precompile, rm -rf /tmp and /public/assets directpories, all of the previous prepended with the development environment, recloning the repository from github and bundle installing.

I know this is an old question, but stumbled across this while trying to fix the same problem.
I found as well as Ryan Bigg's answer I needed to comment out the following line in development.rb to stop Sprockets from bombing in Rails 5.2.2:
# Suppress logger output for asset requests.
# config.assets.quiet = true

It looks like you're running into this issue: https://github.com/rails/sprockets-rails/issues/376.
Where you've made this configuration change you should be using ActiveSupport::Logger instead. Where you have Logger.new, use ActiveSupport::Logger instead.

check The Sprockets 4 Manifest in rails 5
http://eileencodes.com/posts/the-sprockets-4-manifest/
It might help you.

Related

rails 4.2 capistrano 3 Ubuntu nginx puma, getting routing error/no assets shows up

After following this tutorial, I tried was able to set up a rails application on an ec2 Ubuntu instance, running nginx web server and puma app server, deployed with capistrano 3. However, none of my assets are showing up, and I'm getting routing errors for basic functions of the Devise gem such as logging out. The chrome dev tool console shows 404 errors for the compiled application.css and application.js files.
I think the assets are there because if I ssh into the instance and go to the folder where my app is, I can see a bunch of files under public/assets. Also, if I check the capistrano logs, I can find the line bundle exec rake assets:precompile, and the status for that is successful. I tried things like going into the production.rb file and changing config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present? to config.serve_static_files = true
but still no assets. I think the biggest suspect is that there is some sort of routing error, because I don't really understand how web servers, app servers, and aws instances interact with each other. Could anyone point me in the right direction on debugging this? If you need to see a specific config file please comment below.
Ok it turns out all I had to do is copy the secrets.yml from the local repo for my app to the shared folder that is in [my_app_name]/shared/config. So my app didn't know where to look for the secret key base.
Although I'm still confused on why not having the secret.yml would prevent assets from served...

How to properly diagnose a 500 error (Rails, Passenger, Nginx, Postgres)

I'm having a real tough time diagnosing a 500 error from my application running in production. I've had it working before, but after re-deploying via Capastrano I am unable to get it going.
Here are the facts:
The server is setup with nginx + passenger, and I'm using
PostgreSQL.
Static assets are working properly, as in I'm able to access them just fine in a browser.
I can access the rails console via RAILS_ENV=production bundle exec rails console and perform Active Record actions (like
retrieving data from the db).
Within console, I can run app.get("/"), which returns a 500 error as well (after first showing the query that was run to load
the model).
The production.log file is never written to. I've set permissions 777 on it just for the hell of it. I've also set the log level to
:debug with nothing to show for it.
The nginx log (which passenger also uses) shows no indication of errors, it just notifies about cache misses.
Because nothing of use is being logged, I have no idea what to do here. I've tried setting full permission on the entire app directory with no help. Restarted the server multiple times, nothing. The database is there and rails can clearly communicate with it. I'm not sure what I did to get it to run the first time around. I just don't know why rails isn't outputting anything to the log.
Okay, I figured this out. The app ran fine in development mode, so I knew something production-specific was screwing it up. I went into config/environments/production.rb and changes these settings:
# Full error reports are disabled and caching is turned on
config.consider_all_requests_local = false # changed from true
config.action_controller.perform_caching = true # changed from false
And then after restarting passenger, rails showed me the error w/ stacktrace. Turns out I was forgetting to precompile the asset pipeline!
Things to check
1) Are you sure you are running in production environment?
Check to see if any entries are in the development.log file
2) Set up your app to email you when a 500 error occurs with a full stack trace. I use the Exception Notifier gem but there are plenty of other solutions for this.
3) When checking your app in the console are you sure you are starting the console in production mode? It is possible that the app is not starting up at all and you just forgot to set the production param thereby thinking that the app runs fine when it doesn't.
4) Are you getting an nginx 500 error or the Rails 500 error? If nginx then it is likely that your app is not starting at all and highly unlikely that you will get any rails error in your log file. The assets are static files and navigating to them proves nothing other than that they exist.
5) Are you sure you are checking the right folder on the server? Sounds really stupid but capistrano could be deploying the app to a folder that is different to the folder that nginx is looking for for your app so double check both the folder capistrano is deploying to and the folder that nginx is looking for are the same.
Just a suggestion, I would use puma rather than passenger. It's awesome with nginx.
My problem is passenger's log file (error.log) has nothing. Then it's a rotation log issue. Run
passenger-config reopen-logs
solved my problem. More.
Have you tried running in development mode to see if the error reports itself?

No log messages in production.log

I wrote a demo HelloWorld Rails app and tested it with WEBrick (it doesn't even use a DB, it's just a controller which prints "hello world"). Then I tried to deploy it to a local Apache powered with Passenger. In fact this test is just to get Passenger working (it's my first deploy on Apache). Now I'm not even sure that Passenger works, but I don't get any error on the Apache side.
When I fire http://rails.test/ the browser shows the Rails 500 error page - so I assume that Passenger works. I want to investigate the logs, but it happens that production.log is empty! I don't think it's a permission problem, because if I delete the file, it is recreated when I reload the page. I tried to change the log level in conf/environments/production.rb, tried to manually write to log file with Rails console production and
Rails.logger.error('asdf')
it returns true but nothing gets written to production.log. The path (obtained per Rails.logger.inspect) is correct, and I remark that the file is recreated if I manually remove it. How can I know what's going on?
(I already checked the Apache logs, plus I set the highest debug level for Passenger but it seems a Rails problem, so is not logged by the server)
Assuming you're running Rails 3.2.1, this is a bug. It was patched in 3.2.2.
If you can't upgrade to 3.2.2 for any reason, this comment on GitHub has a workaround:
# config/initializers/patch_rails_production_logging.rb
Rails.logger.instance_variable_get(:#logger).instance_variable_get(:#log_dest).sync = true if Rails.logger
Setting this works on Rails 3.2.11:
Rails.logger = ActiveSupport::BufferedLogger.new(Rails.root.join("log","production.log"))

Rails 3.1.1 asset pipeline Heroku caching gotcha

The problem in a nutshell is that in development mode we'd make changes to CSS or JS files but would always get cached/old versions of these files. Nothing I did had any effect. I checked configuration dozens of times and tried every combination of config values but always kept getting the same results: stale/cached files. I had to actually run in production mode and restart the server after every change to test.
I spent days tearing my hair out over this issue, looked at dozens of stackoverflow questions on the asset pipeline but never found one that addressed it so I thought I'd post it here for posterity.
We use Heroku and precompile our assets because Heroku fails to precompile for us (we also use devise which apparently is the cause of the heroku precompilation failure). So in order to push our precompiled assets up to Heroku we have to check them in to git.
Here's the problem.
When we upgraded to Rails 3.1.1 asset precompilation produced files both with and without the MD5 hash in the name. I didn't think much of this and went ahead and checked all these files in so I could push to heroku. Sometime later I noticed the problem with cached results in development mode. The precompiled and checked in assets without the MD5 hashes were being served from /public/assets as static files which prevented us from seeing any changes we were making in /app/assets.
After finally realizing this I ran git rm /public/assets and everything works again. So the takeaway is: Be careful checking assets into git!
To turn this into a question: how do others do this? Am I missing something obvious? What I'd really like is for Heroku to precompile my assets for me but it is failing with a db connection error that I gather is because of devise. I had hoped Rails 3.1.1 fixed this but it didn't.
Have you checked out this devise issue on github? Specifically Jose Valim says
Rails 3.1.1 final has a method called
config.assets.initialize_on_precompile. If you set it to false, you
should be good but it won't allow you to access model information on
your assets (which you probably shouldn't anyway).
Maybe this will allow the precompile to happen on Heroku for you.
The reason the asset precompilation does not work could well be, that the Heroku ENV vars are not present on slug compilation (deploy) as stated here:
http://devcenter.heroku.com/articles/rails31_heroku_cedar
There is an (experimental) way to enable the ENV vars during deploy for exactly this reason, find information here:
http://devcenter.heroku.com/articles/labs-user-env-compile
Hope this helps.
Check this guide from Heroku. It outlines the 3 ways to deploy Rails 3.1 apps. Two of these do not required local precompilation.

rails 3.1 ActionController::RoutingError (No route matches [GET] "/assets/rails.png"):

Standard new rails app has issue showing the rails.png
ActionController::RoutingError (No route matches [GET] "/assets/rails.png"):
I have tried moving the .png file around to various places in assets and assets/images and also the older place 'public' or 'public/images' and changing the page but nothing has helped. Please answer if you have seen and resolved this. I have tried about 20 different combo's myself.
Version:
'rails', '3.1.0.rc4'
I just had a problem throwing a similar error. In my case I was starting the rails server in production mode in Mac OSX using the standard WEBrick. It threw this error because of the line:
config.serve_static_assets = false
in config/environments/production.rb.
That is set because on most production machines the web server itself will handle this.
It looks like you were having a different problem, but I'll post this here for others that run into this error.
It must have been an rc4 issue as the final release didn't have this issue.
11/27/11 - I now wonder if this was just due to the asset pipeline that was introduced in rails 3.1, requiring the rake assets:precompile command (compiles and copies images, css and js from app/assets to public/.
If anyone finds that to be the case, please add a comment!

Resources