Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3.1 assets not resolving

Problem

I have a Rails 3.0.4 app which I upgrade to 3.1.4 following instructions from Railscast video: "Upgrading to rails 3.1".

Now I have problems with assets because they are not resolved, giving messages in server log like this:

Started GET "/assets/application.css" for 127.0.0.1 at 2012-04-08 03:57:13 -0500
Served asset /application.css - 404 Not Found (15ms)

ActionController::RoutingError (No route matches [GET] "/assets/application.css"):
Rendered /usr/local/rvm/gems/ruby-1.9.2-p318/gems/actionpack-3.1.4/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (14.3ms)

Started GET "/assets/application.js" for 127.0.0.1 at 2012-04-08 03:57:13 -0500
Served asset /application.js - 404 Not Found (35ms)

ActionController::RoutingError (No route matches [GET] "/assets/application.js"):

These files are in assets directory:

$ ls app/assets/*/application*
app/assets/javascripts/application.js   app/assets/stylesheets/application.css

and contains this:

$ cat app/assets/javascripts/application.js
//= require jquery
//= require jquery_ujs
//= require_self
//= require_tree .
$ cat app/assets/stylesheets/application.css
/*
*= require_self
*= require_tree .
*/

I suppose everything is configured. Here I put some chunks of files:

  • Gemfile, contains assets gems required:

    group :assets do
      gem 'sass-rails', '~> 3.1.3'
      gem 'coffee-rails', '~> 3.1.0'
      gem 'uglifier'
    end
    
  • application.rb, has assets enabled:

    config.assets.enabled = true
    config.assets.version = '1.0'
    
  • development.rb:

    config.assets.compress = false
    config.assets.debug = true
    
  • finally... application.html.erb layout:

    <%= stylesheet_link_tag 'application' %>
    <%= javascript_include_tag 'application' %>
    

Stage

I am now in development environment.

Tests

  1. If I request http://localhost:3000/assets/images/rails.png, it throws No route matches [GET] "/assets/images/rails.png", but file is there:

    $ ls app/assets/images/rails.png 
    app/assets/images/rails.png
    
  2. Then I execute rake assets:precompile:

    $ bundle exec rake assets:precompile
    /usr/local/rvm/rubies/ruby-1.9.2-p318/bin/ruby /usr/local/rvm/gems/ruby-1.9.2-p318/bin/rake assets:precompile:all RAILS_ENV=production RAILS_GROUPS=assets
    /usr/local/rvm/rubies/ruby-1.9.2-p318/bin/ruby /usr/local/rvm/gems/ruby-1.9.2-p318/bin/rake assets:precompile:nondigest RAILS_ENV=production RAILS_GROUPS=assets
    

    Then, execute server (rails s) and request again an image (rails.png):

    $ rails s
    => Booting WEBrick
    => Rails 3.1.4 application starting in development on http://0.0.0.0:3000
    => Call with -d to detach
    => Ctrl-C to shutdown server
    /usr/local/rvm/gems/ruby-1.9.2-p318/gems/actionpack-3.1.4/lib/action_dispatch/http/mime_type.rb:101: warning: already initialized constant PDF
    [2012-04-08 04:53:06] INFO  WEBrick 1.3.1
    [2012-04-08 04:53:06] INFO  ruby 1.9.2 (2012-02-14) [i686-linux]
    [2012-04-08 04:53:06] INFO  WEBrick::HTTPServer#start: pid=16702 port=3000
    
    Started GET "/assets/images/rails.png" for 127.0.0.1 at 2012-04-08 04:53:14 -0500
    Served asset /images/rails.png - 404 Not Found (38ms)
    
    ActionController::RoutingError (No route matches [GET] "/assets/images/rails.png"):
    
    Rendered /usr/local/rvm/gems/ruby-1.9.2-p318/gems/actionpack-3.1.4/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (125.2ms)
    

So What's wrong?, Am I doing something wrong?

like image 745
Israel Avatar asked Apr 08 '12 09:04

Israel


1 Answers

As far as I know all precompiled assets are moved to the assets folder.

That means the content of javascript, stylesheets AND images will be combined in localhost:3000/assets.

So if you search for localhost:3000/assets/images/rails.png it should be located in app/assets/images/images/rails.png. Therefor the 404.

To keep your directory clean I suggest to keep all your images in app/assets/images/ and point to them with asset_path('rails.png')

like image 64
Vikko Avatar answered Sep 28 '22 19:09

Vikko