无法在Rails 3教程中生成Pages控制器

时间:2011-10-03 00:20:30

标签: ruby-on-rails ruby

我正在阅读Michael Hartl Rails 3教程,我目前正在第3章。该教程要求我使用命令行生成一个页面控制器,其中包含对主页和联系页面的操作:“$ rails generate控制器页面主页联系人“。

这是我得到的输出:

ruby 1.9.2p290 (2011-07-09) [i386-mingw32]

C:\Users\abcd\rails_projects2\sample_app>rails generate controller Pages home
contact
C:/Users/abcd/rails_projects2/sample_app/config/application.rb:8:in `require':
 no such file to load -- sprockets/railtie (LoadError)
        from C:/Users/abcd/rails_projects2/sample_app/config/application.rb:8:
in `<top (required)>'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.9/lib/rails/comman
ds.rb:15:in `require'
        from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.9/lib/rails/comman
ds.rb:15:in `<top (required)>'
        from script/rails:6:in `require'
        from script/rails:6:in `<main>'

我的config / 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 SampleApp
  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 = :de

    # 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'
  end
end

我还发现当我尝试运行命令行“rails server”时,我收到类似的错误消息。我不知道这些信息是否有用。

谢谢!

2 个答案:

答案 0 :(得分:2)

看起来本教程使用的是Rails 3.0.9,但是您已经使用更高版本的Rails gem创建了应用程序。

版本3.1中添加了

sprockets。即使您已更新Gemfile以指定Rails 3.0.9,运行rails new sample_app时生成的代码也希望Rails 3.1 gems可用(即只需更改Gemfile就不足以更改Rails应用程序的版本。)

您可以尝试在sprockets文件中简单地评论application.rb的引用,但即使现在有效,也可能会出现其他差异,以后会导致错误。

您最好的选择可能是从头开始,但请确保您使用的是Rails 3.0.9。如果你正在使用RVM,你可以创建一个新的gemset并在其中安装3.0.9,然后使用该gemset。

或者,在创建应用程序时,在命令中指定所需的gem版本:

rails _3.0.9_ new sample_app

如果您使用第二种方法,那么在您创建应用程序之后,只需单独使用rails来发出命令(例如generate)应该没问题,因为Rails确实有一些魔力可以确定应用程序的版本并使用正确的gem版本,即使安装了更高版本 - 您也不需要一直使用rails _3.0.9_

答案 1 :(得分:0)

/ config.rb文件中,尝试取消注释“sprockets”行,如下所示:

# require "sprockets/railtie"

然后检查它是否有用......这应该可以解决问题...

但如果没有,请检查你的Gemfile,它应该是这样的:

# gem "rails", "~> 3.1.0"      # or "3.0.9"
gem "rails" , "3.0.9"
gem 'sqlite3', '1.3.3'         # or whatever DB you use

如果仍然没有帮助,请检查您安装的链轮版本: 在shell中,执行:

$ gem list | grep sprock
sprockets (2.0.0.beta.10)