Rails 3.1资产无法解决

时间:2012-04-08 09:29:47

标签: ruby-on-rails-3.1 asset-pipeline

问题

我有一个Rails 3.0.4应用程序,我按照Railscast video: "Upgrading to rails 3.1"的说明升级到3.1.4。

现在我遇到了资产的问题,因为未解决,在服务器日志中发送消息如下:

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"):

这些文件位于assets目录:

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

并包含此内容:

$ 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 .
*/

我想一切都已配置完毕。在这里我放了一些文件:

  • Gemfile,包含所需的资产宝石:

    group :assets do
      gem 'sass-rails', '~> 3.1.3'
      gem 'coffee-rails', '~> 3.1.0'
      gem 'uglifier'
    end
    
  • application.rb,已启用资源:

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

    config.assets.compress = false
    config.assets.debug = true
    
  • 最后...... application.html.erb布局:

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

阶段

我现在处于开发环境中。

测试

  1. 如果我请求http://localhost:3000/assets/images/rails.png,它会抛出No route matches [GET] "/assets/images/rails.png",但文件在那里:

    $ ls app/assets/images/rails.png 
    app/assets/images/rails.png
    
  2. 然后我执行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
    

    然后,执行服务器(rails s)并再次请求图像(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)
    
  3. 所以出了什么问题?,我做错了吗?

1 个答案:

答案 0 :(得分:2)

据我所知,所有预编译资产都移动到assets文件夹。

这意味着javascript,样式表和图片的内容将合并到localhost:3000/assets

所以,如果你搜索 localhost:3000/assets/images/rails.png 它应该位于 app/assets/images/images/rails.png。 因此404.

要保持目录清洁,我建议您将所有图片保存在app / assets / images /中并使用asset_path('rails.png')

指向它们