Domanda

I learn Ruby on Rails on the book by Michael Hartl. I tried to add the dynamic generating a secret token.

secret_token.rb:

require 'securerandom'

def secure_token
  token_file = Rails.root.join('.secret')
  if File.exist?(token_file)
    # Use the existing token.
    File.read(token_file).chomp
  else
    # Generate a new token and store it in token_file.
    token = SecureRandom.hex(64)
    File.write(token_file, token)
    token
  end
end

Blog::Application.config.secret_key_base = secure_token

application.rb file:

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

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

module Blog
  class Application < Rails::Application
  end
end

But there is an error during the execution of operation:

yaroslav@yaroslavpc:~/rails/blog$ rails generate rspec:install
/home/yaroslav/rails/blog/config/application.rb:7:in `<top (required)>': undefined method `env' for Rails:Module (NoMethodError)
    from /home/yaroslav/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0/lib/rails/commands.rb:44:in `require'
    from /home/yaroslav/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0/lib/rails/commands.rb:44:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'

ROR and Ruby versions:

yaroslav@yaroslavpc:~/rails/blog$ rails --version
Rails 4.0.0
yaroslav@yaroslavpc:~/rails/blog$ ruby -v
ruby 2.0.0p247 (2013-06-27 revision 41674) [i686-linux]

A little help would be very cool.

È stato utile?

Soluzione

You are using Rails 4.0, I think Rails.env is for 3.0.

Try this:

if Rails.respond_to? :env
    Bundler.require(:default, Rails.env) if defined?(Bundler)
else
    Bundler.require(:default, RAILS_ENV) if defined?(Bundler)
end
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top