8

问题

我有一个 Rails 3.0.4 应用程序,我按照Railscast 视频中的说明升级到 3.1.4 :“升级到 rails 3.1” 。

现在我遇到了资产问题,因为它们没有解决,在服务器日志中给出如下消息:

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)
    

那么怎么了?, 难道我做错了什么?

4

1 回答 1

2

据我所知,所有预编译的资产都已移至资产文件夹。

这意味着 javascript、样式表和图像的内容将组合在localhost:3000/assets.

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

为了保持目录干净,我建议将所有图像保存在 app/assets/images/ 中,并使用asset_path('rails.png')

于 2012-08-14T12:50:48.257 回答