Rails production static files routing error - ruby-on-rails

When I run my app locally in test/dev my views come up nicely and everything is happy. When I try to navigate to those same erb files running on my remote server/local production server I get errors like the following:
ActionController::RoutingError (No route matches "/stylesheets/scaffold.css")
I've seen similar questions here on SO but none have been able to solve my problem. The closest thing I've found to an answer is the first answer here: Rails 404 error for Stylesheet or JavaScript files
As I understand it the best thing to do would be to configure my webserver to serve static files. How do I do this locally/on Heroku?
Update
As per raidfive's suggestion I changed config.serve_static_assets from false to true and this fixed my issue. However, I see that it says in production.rb that Apache or nginx should already be serving static assets. Is it any less good/professional to serve static assets in this way and if so how would I achieve the desired results if I'm using Heroku?
Update 2
Apparently Heroku does this automatically, I had an extra comma that was causing the mischief. I was able to look in the extended Heroku logs using the following tip to track down the trouble. Thanks SO!

Are you using Rails 3? By default Rails 3 / webrick does not serve static files in production mode. You can easily enable this by setting
config.serve_static_assets to true in your production.rb file.

In Rails5, you should comment
"config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?"
in config/enviroment/production.rb

Related

Rails 4 Heroku - changes not getting pushed?

I am using Heroku to try to deploy a personal Ruby on Rails project and everything was going great until today.
I am very very new to Ruby on Rails and Heroku so please bare that in mind. I am not sure what is causing my issue and therefore not sure what code or information is best to supply so please ask me what you think you need to know to help resolve the issue and I will provide it.
My Ruby on Rails app worked fine both locally and on Heroku until I followed the information here to try and serve static images from Amazons S3 bucket. Note I only went as far as the static assets section.
This appeared to stop my Ruby on Rails application from recognising changes in my code. So I would make a change to a HTML file in my editor but the server was serving up the older version of the HTML file, even restarting the server didn't fix this.
I have been searching the web for hours trying to figure out what has gone wrong.
I deleted everything under public assets and I ran the precompile command:
rake assets:precompile
And this seems to have improved things locally, when I edit a HTML file the changes are reflected on localhost. However when I push to Heroku and go to my application hosted on Heroku it still shows the older HTML file no matter how many changes I make and pushes I do to Heroku.
The HTML files that are not updating are located here:
app/assets/templates
I'm not sure what I may have changed that has caused the HTML files not to get updated on Heroku?? What should I look at and try? What other information would be useful in helping track down the issue?
The answer marked as correct in this StackOverflow question worked for me - Updated CSS Stylesheet not loaded following deployment to Heroku? - It looks like I accidently added assets precompiled file in my git repo somewhere along my development and that caused the issue.

Dragonfly not working in production

I am using Dragonfly with Nginx, unicorn and mini_magick. I am using mem_cache_store in production.
Image upload and display works fine in development environment. But on production it fails with internal server error 500. I cannot see any logs on my production log, but it fails on production.
I get error for URLs of following type.
http://cdn.mydomain.com/media/BAhbB1sHOgZmSSIoMTAvcHJvZHVjdHMvMjUvR3JvdW5kJTIwbnV0XzQwOS5qcGcGOgZFVFsIOgZwOgp0aHVtYkkiCjUweDUwBjsGVA.jpg
Error code is 500
I ve been searching all the documentation which comes along with the gem. I can see the following middleware on top of the middleware stack in production as well:
use Rack::Cache
use Dragonfly::CookieMonster
use Dragonfly::Middleware
I could not get much help from forums - though there is a similar bug posted 2 years ago and is supposed to be fixed. And fixes there also did not help.
Please help to fix this. Any direction as to how I can debug this would also help.
I ran into an issue like this and turned out that the problem was that I didnt have imagemagick installed in production. This post helped me Dragonfly - Uploaded images not displayed
Btw I wasnt seeing any logs too.
You need to add:
Config.middleware.insert 0, Rack::Sendfile, config.action_dispatch.x_sendfile_header
so that sendfile is before rack:cache and it should work.

Rails get production environment working with SSL on local machin

I am having trouble with my site with precompiled assets looking nothing like my site with assets served on-the-go. So I figured, I should try things out in production.
Here's the problem, when I do, I get:
!! Invalid request
in my terminal log and in my browser I see
Error 107 (net::ERR_SSL_PROTOCOL_ERROR): SSL protocol error.
The graphic designer is not amused at the mess thats up right now, and I'd love to be able to reproduce the problem. Any ideas on how to get past this SSL issue?
The only way that I've ever been able to get sites implementing SSL working on my development box in production mode has been to set up Apache in front of my rails server (Webrick, or whatever). This is kind of a pain to set up, but it works. You should be able to find a guide on how to set it up using your exact configuration with a quick search. Another option would be to enable SSL via a configuration file, instead of by environment, so you could simply disable it regardless of your current environment. This is assuming everything else is actually correct and working...

Why does Rails sometimes create an asset cache with the wrong permissions?

I have an EC2 instance with the usual stack of Apache, Passenger, and Rails. The app is running in production, so Javascript files are cached into an all.js file.
Passenger is configured to pre-start the application instance using the PassengerPreStart directive.
On occasion, the all.js that is created by Rails in public/javascripts has the wrong permissions, 0600 for the deployment user. It's supposed to be 0640, so that Apache can serve the files (they're in the same group, www-data).
Do you have any explanation as to why this is happening? Is there any issue known with PassengerPreStart and Rails' asset caching?
I'm pretty sure this is because of the timestamps that come after the assets. If you do a view source the numbers after the asset/files are a timestamp and I think the browsers get confused, or rails messes it up or something.
You change the default of the timestamp to an empty string but putting the following code somewhere in your Rails application.
/application.rb
ENV[RAILS_ASSET_ID] = ""
That will set it to an empty string. See how that goes.

Rails image_tag is looking at my production server for assets!

I read that by setting ActionController::Base.asset_host, you can control where rails looks for assets. However, by default Rails should look in '/public/...'
Somehow my Rails app knows to look at my production address when in development mode and I have not set ActionController::Base.asset_host anywhere. I am not sure where it is finding this information. Is there anywhere else this can be set? I don't think my deploy recipes should have anything to do with it...
<%= image_tag 'my_logo.png' %>
causes Rails to check my production server for the file. Same with stylesheets.
I am not quite sure how Rails knew to look for my production domain. I removed it almost everywhere in my code. According to the docs, "By default, Rails links to these assets on the current host in the public folder" but I am not quite sure what that means. To prevent this from ever being an issue again and to allow some flexible configuration, I added asset host domains to my environment configuration. I now have something like:
EnvironmentConfig = YAML.load(File.read(Rails.root + "config/#{rails_env}" + 'environment.yml'))[rails_env]
ActionController::Base.asset_host = EnvironmentConfig['asset_host']
Everything works great now. Still don't know how Rails knew to look at my production domain.

Resources