【问题标题】:Upgrade from rails 3.1.3 to rails 3.2.1. Error with assets从 rails 3.1.3 升级到 rails 3.2.1。资产错误
【发布时间】:2012-02-05 17:54:04
【问题描述】:

我尝试将我的应用程序从 rails 3.1.3 升级到 rails 3.2.1,但我遇到了资产问题。

我有这种错误:

ActionController::RoutingError (No route matches [GET] "/assets/logos/opera_logo.png"):
  actionpack (3.2.1) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (3.2.1) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
  railties (3.2.1) lib/rails/rack/logger.rb:26:in `call_app'
  railties (3.2.1) lib/rails/rack/logger.rb:16:in `call'
  actionpack (3.2.1) lib/action_dispatch/middleware/request_id.rb:22:in `call'
  rack (1.4.1) lib/rack/methodoverride.rb:21:in `call'
  rack (1.4.1) lib/rack/runtime.rb:17:in `call'
  activesupport (3.2.1) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
  rack (1.4.1) lib/rack/lock.rb:15:in `call'
  actionpack (3.2.1) lib/action_dispatch/middleware/static.rb:53:in `call'
  railties (3.2.1) lib/rails/engine.rb:479:in `call'
  railties (3.2.1) lib/rails/application.rb:220:in `call'
  rack (1.4.1) lib/rack/content_length.rb:14:in `call'
  railties (3.2.1) lib/rails/rack/log_tailer.rb:14:in `call'
  rack (1.4.1) lib/rack/handler/webrick.rb:59:in `service'
  /usr/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service'
  /usr/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run'
  /usr/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'

Rails 没有找到我所有的资产,但我认为它们在正确的文件夹中(例如 app/assets/images/logos/)。

我按照本指南升级我的系统:http://railscasts.com/episodes/318-upgrading-to-rails-3-2?view=asciicast。它在 rails 3.1 上运行得非常好。我没有发现任何其他改变要做。我能做什么?

这是我的 Gemfile:

source 'http://rubygems.org'

gem 'rails', '3.2.1'

# Bundle edge Rails instead:
# gem 'rails',     :git => 'git://github.com/rails/rails.git'

gem 'sqlite3'


# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails',   '~> 3.2.3'
  gem 'coffee-rails', '~> 3.2.1'
  gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails'

# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'

# Use unicorn as the web server
# gem 'unicorn'

# Deploy with Capistrano
# gem 'capistrano'

# To use debugger
gem 'ruby-debug19', :require => 'ruby-debug'

group :test do
  gem 'capybara', '1.1.2'
  gem 'rspec-rails', '2.8.1'
  gem 'autotest-rails', '4.1.1'
  gem 'spork', '0.8.5'
  gem 'factory_girl_rails', '1.4.0'
  gem 'email_spec', '1.2.1'
  gem 'cucumber-rails', '1.2.1'
  gem 'launchy', '2.0.5'
  gem 'pickle', '0.4.10'
  gem 'database_cleaner', '0.7.0'
  gem 'simplecov', '0.5.4'
  gem 'simplecov-rcov', '0.2.3'
  gem "mocha", '0.10.0'
  gem 'capybara-firebug', '0.0.10'
  gem "prawn", '0.8.4'
end

group :development do
  gem "nifty-generators", '0.4.6'
end

gem 'active_reload', '0.6.1'
gem "galetahub-simple_captcha", '0.1.3', :require => "simple_captcha"
gem 'authlogic', '3.1.0'
gem 'meta_search', '1.1.3'
gem 'naive_bayes', :git => 'git://github.com/reddavis/Naive-Bayes.git'

这是我的 development.rb :

Onopia::Application.configure do
  # Settings specified here will take precedence over those in config/application.rb

  # In the development environment your application's code is reloaded on
  # every request.  This slows down response time but is perfect for development
  # since you don't have to restart the web server when you make code changes.
  config.cache_classes = false

  # Log error messages when you accidentally call methods on nil.
  config.whiny_nils = true

  # Show full error reports and disable caching
  config.consider_all_requests_local       = true
  config.action_controller.perform_caching = false

  # Don't care if the mailer can't send
  config.action_mailer.raise_delivery_errors = false

  # Print deprecation notices to the Rails logger
  config.active_support.deprecation = :log

  # Only use best-standards-support built into browsers
  config.action_dispatch.best_standards_support = :builtin

  # Do not compress assets
  config.assets.compress = false

  # Expands the lines which load the assets
  config.assets.debug = true

  # Raise exception on mass assignment protection for Active Record models
  config.active_record.mass_assignment_sanitizer = :strict

  # Log the query plan for queries taking more than this (works
  # with SQLite, MySQL, and PostgreSQL)
  config.active_record.auto_explain_threshold_in_seconds = 0.5
end

这是我的 application.rb :

require File.expand_path('../boot', __FILE__)

# Pick the frameworks you want:
require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "sprockets/railtie"
# require "rails/test_unit/railtie"

if defined?(Bundler)
  # If you precompile assets before deploying to production, use this line
  Bundler.require(*Rails.groups(:assets => %w(development test)))
  # If you want your assets lazily compiled in production, use this line
  # Bundler.require(:default, :assets, Rails.env)
end

module Onopia
  class Application < Rails::Application
    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration should go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded.

    # Custom directories with classes and modules you want to be autoloadable.
    # config.autoload_paths += %W(#{config.root}/extras)

    # Only load the plugins named here, in the order given (default is alphabetical).
    # :all can be used as a placeholder for all plugins not explicitly named.
    # config.plugins = [ :exception_notification, :ssl_requirement, :all ]

    # Activate observers that should always be running.
    # config.active_record.observers = :cacher, :garbage_collector, :forum_observer

    # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
    # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
    # config.time_zone = 'Central Time (US & Canada)'

    # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
    # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
    config.i18n.default_locale = :fr

    # Configure the default encoding used in templates for Ruby 1.9.
    config.encoding = "utf-8"

    # Configure sensitive parameters which will be filtered from the log file.
    config.filter_parameters += [:password]

    # Enable the asset pipeline
    config.assets.enabled = true

    # Version of your assets, change this if you want to expire all your assets
    config.assets.version = '1.0'

    # Configure generators values. Many other options are available,
    # be sure to check the documentation.
    #config.generators do |g|
    #  g.test_framework :rspec, :fixture => false
    #end

    config.action_mailer.default_url_options = { :host => 'localhost:3000' }
  end
end

编辑:一个更好的例子。

我想查看这个文件:

app/assets/javascripts/application.js

当我访问这个网址时:

http://localhost:3000/assets/application.js

我有这个错误:

Started GET "/assets/application.js" for 127.0.0.1 at 2012-02-06 17:04:38 -0500
Served asset /application.js - 404 Not Found (1ms)

ActionController::RoutingError (No route matches [GET] "/assets/application.js"):
  actionpack (3.2.1) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (3.2.1) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
  railties (3.2.1) lib/rails/rack/logger.rb:26:in `call_app'
  railties (3.2.1) lib/rails/rack/logger.rb:16:in `call'
  actionpack (3.2.1) lib/action_dispatch/middleware/request_id.rb:22:in `call'
  rack (1.4.1) lib/rack/methodoverride.rb:21:in `call'
  rack (1.4.1) lib/rack/runtime.rb:17:in `call'
  activesupport (3.2.1) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
  rack (1.4.1) lib/rack/lock.rb:15:in `call'
  actionpack (3.2.1) lib/action_dispatch/middleware/static.rb:53:in `call'
  railties (3.2.1) lib/rails/engine.rb:479:in `call'
  railties (3.2.1) lib/rails/application.rb:220:in `call'
  rack (1.4.1) lib/rack/content_length.rb:14:in `call'
  railties (3.2.1) lib/rails/rack/log_tailer.rb:14:in `call'
  rack (1.4.1) lib/rack/handler/webrick.rb:59:in `service'
  /usr/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service'
  /usr/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run'
  /usr/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'

非常感谢,祝你有美好的一天。

【问题讨论】:

  • 你说资产在app/assets/images,但你是从app/assets/logos调用图片
  • 对不起,这是我的错误。我的文件在这里:app/assets/images/logos/opera_logo.png。我编辑了我的帖子。
  • 我想知道编辑帖子是否更改了 RoutingError 消息。根据您上面的日志,您引用的图像不在assets/images 文件夹中,而是在assets/logos 中。将文件的位置更改为 assets/logos 或更好地找到引用链接并将其更改为匹配 assets/images/opera_logo.png
  • 如果上述方法不起作用,请尝试检查资产生成,例如http://localhost:3000/assets/application.css。您将查看您的样式是否在assets 文件夹下生成。如果没有,请检查资产管道是否已启用。
  • 对不起,我选错了例子。这与位于此位置 app/assets/stylesheets/application.css 的 application.css 相同。我有这个错误: ActionController::RoutingError (No route matches [GET] "/assets/application.css")

标签: ruby-on-rails rails-3.1


【解决方案1】:

我遇到了一个类似的问题,我通过删除 active_reload gem 解决了这个问题。

【讨论】:

  • 为我解决了这个问题。谢谢!
  • 这是 Rails active_reload 与 Rails 3.2 不兼容。
  • 经过几个小时的搜索 - 我被发现了。非常感谢!
【解决方案2】:

显然某些 gem 依赖被破坏了,bundle exec rails server 应该可以解决这个问题。这应该在没有 sudo ... 的情况下运行,除非您通常使用具有 root 权限的应用程序(这不好)。检查您的 rails 根目录中的文件/文件夹权限,这可能已因之前滥用 sudo 命令而被破坏。在开发过程中,您几乎不需要任何 root 权限。

除此之外,similar issues 之前也出现过,答案是更新您的整个 gemset,主要是链轮和导轨。您的配置文件在我看来没问题,但我无法重现该错误。

如果这没有帮助,我会创建一个新的 rails 应用程序,使用您拥有的相同 gemset 检查资产管道,并手动交叉检查所有配置文件。

路由的调试工具也有很多种,不过还是先试试上面的吧。

【讨论】:

  • 我用 RVM 安装了一个新的 gemset,它可以工作了!非常感谢。
  • 我用bundle exec rails s 启动了rails 服务器,它可以工作。这个错误的可怕之处在于,其他资产(javascript、样式表)可以在 rails s 上正常工作,但图像不能。
  • @Giovanni 您提供的细节太少,无法理解。请开始一个新问题来解释您的情况。确保包含您的 Gemfile 和对不起作用的图像资产的调用以及完整的错误消息。
【解决方案3】:

我之前遇到过类似的事情。你是如何引用它们的?是 image_tag 失败还是 CSS 引用失败?

如果您的文件在此处/app/assets/images/logos/opera_logo.png,那么以下引用应该可以工作。

来自 html.erb 文件:&lt;%= image_tag('logos/opera_logo.png') %&gt;

来自 scss 文件:background: url(image_path("logos/opera_logo.png"))

http://guides.rubyonrails.org/asset_pipeline.html#coding-links-to-assets

【讨论】:

    【解决方案4】:

    尝试将其添加到您的 Gemfile 并重新启动服务器,如下所示:http://guides.rubyonrails.org/asset_pipeline.html#upgrading-from-old-versions-of-rails

    # Gems used only for assets and not required
    # in production environments by default.
    group :assets do
      gem 'sass-rails',   "~> 3.2.3"
      gem 'coffee-rails', "~> 3.2.1"
      gem 'uglifier'
    end
    

    【讨论】:

      【解决方案5】:

      Rails 新手,但对于我位于目录 ../app/assets/images 中的图像,例如“铁轨.png” 背景图像的正确 CSS 参考是:background: white url(rails.png) repeat-y;(显示选项)

      即使它在 images 文件夹中,使用 rails 3.2.6(我正在使用的版本),您也不要将图像放在目录的一部分。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多