Katana24
Katana24

Reputation: 8959

Starting rails console throws error

Up until yesterday I could start a rails console using the rails c shortcut at the root of my rails application. The application is set as an API only app to serve my angular frontend. When I tried to start it last night I got the following error:

Running via Spring preloader in process 5967
/Users/Andrew/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.5/lib/action_dispatch/middleware/stack.rb:90:in `insert': can't modify frozen Array (RuntimeError)
    from /Users/Andrew/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.5/lib/action_dispatch/middleware/stack.rb:90:in `insert'
    from /Users/Andrew/sites/mine/K24/api/config/application.rb:15:in `<class:Application>'
    from /Users/Andrew/sites/mine/K24/api/config/application.rb:10:in `<module:Api>'
    from /Users/Andrew/sites/mine/K24/api/config/application.rb:9:in `<top (required)>'
    from /Users/Andrew/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `require'
    from /Users/Andrew/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `block in require'
    from /Users/Andrew/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /Users/Andrew/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `require'
    from /Users/Andrew/.rvm/gems/ruby-2.2.1/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:141:in `require_application_and_environment!'
    from /Users/Andrew/.rvm/gems/ruby-2.2.1/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:67:in `console'
    from /Users/Andrew/.rvm/gems/ruby-2.2.1/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from /Users/Andrew/.rvm/gems/ruby-2.2.1/gems/railties-4.2.5/lib/rails/commands.rb:17:in `<top (required)>'
    from /Users/Andrew/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `require'
    from /Users/Andrew/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `block in require'
    from /Users/Andrew/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /Users/Andrew/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `require'
    from /Users/Andrew/sites/mine/K24/api/bin/rails:9:in `<top (required)>'
    from /Users/Andrew/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:268:in `load'
    from /Users/Andrew/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:268:in `block in load'
    from /Users/Andrew/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /Users/Andrew/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:268:in `load'
    from /Users/Andrew/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /Users/Andrew/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from -e:1:in `<main>'

According to this error it's complaining about a problem in the application.rb file I ran a diff on the folder and found that the differences were simply the following"

  1. In application.rb I removed the comments only.
  2. In the routes file I added several new routes:

    post "update", to: "posts#update"

I tried fixing this by doing the following:

Any suggestions as to why this is happening?

EDIT

Posting the contents of the application.rb file:

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

require 'rails/all'

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

module Api
  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

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

    config.middleware.insert_before 0, "Rack::Cors", :debug => true, :logger => (-> { Rails.logger }) do
      allow do
        origins '*'

        resource '/cors',
          :headers => :any,
          :methods => [:post]

        resource '*',
          :headers => :any,
          :methods => [:get, :post, :delete, :put, :options, :head],
          :max_age => 0
      end
    end
  end
end

Upvotes: 0

Views: 429

Answers (2)

Katana24
Katana24

Reputation: 8959

My edit to barr-code's answer doesn't seem to have been approved but I have given her a +1 as it lead me to the answer below.

The actual problem was that the gem spring under the development environment was actually causing the problem. Commenting t out allows me to access the rails console - that's fixes the aforementioned problem however, it means that the app throws up another error in that it can't find a helper.

So what I'm doing at the moment is actually switching between the two as needed - not ideal but it does mean that I can continue development.

Upvotes: 0

barr-code
barr-code

Reputation: 51

I think the problem is because of this line: Bundler.require(*Rails.groups)

Before launching the console, the bundler tries to launch your gems via that line. Check your Gemfile. Have you defined any groups (test, development, etc)?

You may want to try using this instead: Bundler.require(:default, :assets, Rails.env)

Upvotes: 1

Related Questions