Ruby on rails 未定义的方法';环境';in application.rb

Ruby on rails 未定义的方法';环境';in application.rb,ruby-on-rails,ruby,railstutorial.org,Ruby On Rails,Ruby,Railstutorial.org,我在Michael Hartl的书中学习了Ruby on Rails。我试图添加动态生成一个秘密令牌 秘密令牌。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 # Gen

我在Michael Hartl的书中学习了Ruby on Rails。我试图添加动态生成一个秘密令牌

秘密令牌。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
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
application.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
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

一点帮助会很酷。

您使用的是Rails 4.0,我认为Rails.env适用于3.0

试试这个:

if Rails.respond_to? :env
    Bundler.require(:default, Rails.env) if defined?(Bundler)
else
    Bundler.require(:default, RAILS_ENV) if defined?(Bundler)
end

谢谢。如果在
require'rails/all'
的顶部添加一个字符串,它就可以很好地工作。