Search code examples
ruby-on-railsrubythin

Rails not serving static files


I'm running the latest version of Thin, Rails, and Ruby. The relevant parts of my build script are:

export RAILS_ENV=production
export RAILS_SERVE_STATIC_FILES=true
# generate static assets
RAILS_ENV=production rake assets:precompile
# restart server
rails server thin -d

And in my production.rb I have

config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?

And I include these files in my view using

<%= stylesheet_link_tag 'application', media: 'all' %>
<%= javascript_include_tag 'application' %>

This successfully compiles my assets and moves them to the /public/assets folder. However, none of these assets are actually served when loading the page. The generated HTML is

<link rel="stylesheet" media="all" href="/stylesheets/application.css" />
<script src="/javascripts/application.js"></script>

These don't have the digests included in the filename, so I believe they're incorrect. Attempting to manually load the filenames both with and without their digest included all fail with a 404 as well. What am I doing wrong?


Solution

  • put the js files under app/assets/javascript.

    put the css files under app/assets/stylesheets.

    All your js and css files are loaded in the header of application.html.erb, which loads the application.css and application.js. This is what these two lines of code does.

    <%= stylesheet_link_tag 'application', media: 'all' %>
    <%= javascript_include_tag 'application' %>
    

    These two files, there is a line require_tree . which loads all the files in the corresponding directory:

    app/assets/javascript
    app/assets/stylesheets
    

    I usually use public folder only for external libraries when I am not using a CDN. And you will need to explicitly include them in the application.js and application.css file.