2013-11-28 4 views
-1

저는 Michael Hartl이 저서에서 Ruby on Rails를 배웁니다. 나는 동적 토큰을 생성하는 것을 추가하려고 시도했다.application.rb에서 정의되지 않은 메소드 'env'

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 파일 :

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 

그러나 오류가 작업의 실행 중에있다 :

[email protected]:~/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 Ruby versio ns :

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

약간의 도움이 매우 좋습니다.

답변

1

레일즈 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 
+0

덕분에 많이. 'require 'rails/all'의 맨 위에 문자열을 추가하면 아주 잘 작동합니다. – Shapoval

관련 문제