Ruby on Rails – rails generate rspec:installが実行できない

投稿者: Anonymous

こんにちわ。rails初心者です。
現在Railsチュートリアル第3章を進めています。そこで下記コマンドを実行するとエラーが出てしまいました。解決する方法をご存知の方がいらっしゃいましたらアドバイスください。お願いします。

実行コマンド

rails generate rspec:install

エラー

/Users/mofuty/rails_projects/sample_app/config/application.rb:5:in `require': cannot load such file -- active_job/railtie (LoadError)
    from /Users/mofuty/rails_projects/sample_app/config/application.rb:5:in `<top (required)>'
    from /Users/mofuty/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/railties-4.0.5/lib/rails/commands.rb:43:in `require'
    from /Users/mofuty/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/railties-4.0.5/lib/rails/commands.rb:43:in `<top (required)>'
    from bin/rails:8:in `require'
    from bin/rails:8:in `<main>'

Gemfile

source 'https://rubygems.org'
ruby '2.1.2'
#ruby-gemset=railstutorial_rails_4_0

gem 'rails', '4.0.5'

group :development, :test do
  gem 'sqlite3', '1.3.8'
  gem 'rspec-rails', '2.13.1'
end

group :test do
  gem 'selenium-webdriver', '2.35.1'
  gem 'capybara', '2.1.0'
end

gem 'sass-rails', '4.0.5'
gem 'uglifier', '2.1.1'
gem 'coffee-rails', '4.0.1'
gem 'jquery-rails', '3.0.4'
gem 'turbolinks', '1.1.1'
gem 'jbuilder', '1.0.2'

group :doc do
  gem 'sdoc', '0.3.20', require: false
end

group :production do
  gem 'pg', '0.15.1'
  gem 'rails_12factor', '0.0.2'
end

config/application.rb

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

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

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

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.

    # 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

    # Do not swallow errors in after_commit/after_rollback callbacks.
    config.active_record.raise_in_transactional_callbacks = true
  end
end

解決

試しに bundle exec rails generate rspec:install を実行してみてください。

たしかにapplication.rbがちょっと変ですね。
4.0系だとこんな感じになるはずです。

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

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

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(:default, Rails.env)

module CbMorning
  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.

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

active_jobとかは4.0系では通常出てこないので、一度このコードに置き換えてみてください。

原因はよくわかりませんが、もしかすると4.2系の実行環境で rails newしてしまったのかもしれません。

追記

4.2系でrails newしていると、今後他にも問題が出てくるかもしれません。
4.0.5で作っていくなら、バージョンを指定してrails newし直した方が早いかもです。

rails -v
Rails 4.2.0
# 4.0.5ではない場合
rails _4.0.5_ new sample_app
回答者: Anonymous

Leave a Reply

Your email address will not be published. Required fields are marked *