rails 4 enabled rails to serve static assets: is it correct? (on heroku)

19,944

Solution 1

This is deprecated in Rails 4.2, and it is now an alias slated to be removed in Rails 5.0

config.serve_static_assets = true

It should be changed to:

config.serve_static_files = true

Solution 2

In previous Rails versions, Heroku injected a plugin that enabled serving of static assets so this issue didn't exist. As this plugin system was removed in Rails 4, they now created a gem which does the same. You enable it in your Gemfile via:

gem 'rails_12factor', group: :production

See Getting Started with Rails 4.x on Heroku

You could also of course use a CDN for your assets, but you're not required to.

For Rails 5+ work on twelve-factor platforms out of the box and the gem is no longer required

Solution 3

As Dean Winchester mentioned it, it is a good idea to use a CDN for your static assets. In fact when using only Heroku your Rails application would have to be responsible to serve static assets since Heroku Cedar architecture will not do that for you.

Setting config.serve_static_assets = true is the way to go if you don't want to configure a CDN and use only Heroku.

Solution 4

The rails guides are wrong. Try...

config.assets.serve_static_files = true
Share:
19,944
Mike
Author by

Mike

Updated on June 19, 2022

Comments

  • Mike
    Mike almost 2 years

    Environment: heroku

    Rails: 4

    Ruby: 2

    We deployed an app to heroku, and it seemed as though anything in the public folder was not accessible (didn't see the static file so the router kicked in and then complains about no route matching).

    To get around this, we set

    # Rails 4 only flag
    config.serve_static_assets = true
    

    in our production.rb file. Is this really the best way to handle this? Or did we fail to configure the app some how to be hosted on heroku properly?