gpt4 book ai didi

ruby-on-rails - Rspec - 未初始化的常量 RAILS_ENV

转载 作者:太空宇宙 更新时间:2023-11-03 17:47:52 25 4
gpt4 key购买 nike

我运行我的任务 crate db:restore 并显示错误:

** Execute db:restore
rake aborted!
NameError: uninitialized constant RAILS_ENV
/home/dima/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/rspec-core-2.5.2/lib/rspec/core/backward_compatibility.rb:20:in `const_missing'
/home/dima/myapp/bdsmgalaxy/lib/tasks/mysql.rake:24:in `block (2 levels) in <top (required)>'

我的任务是:

require 'yaml'

namespace :db do
def backup_prep
@directory = File.join(RAILS_ROOT, 'db', 'backup')
@db = YAML::load( File.open( File.join(RAILS_ROOT, 'config', 'database.yml') ) )[ RAILS_ENV ]
@db_params = "-u #{@db['username']} #{@db['database']}"
@db_params = "-p#{@db['password']} #{@db_params}" unless @db['password'].blank?
end

desc 'Backup database by mysqldump'
task :backup => :environment do
backup_prep
FileUtils.mkdir @directory unless File.exists?(@directory)
file = File.join( @directory, "#{RAILS_ENV}_#{DateTime.now.to_s}.sql" )
command = "mysqldump #{@db_params} | gzip > #{file}.gz" #--opt --skip-add-locks
puts "dumping to #{file}..."
# p command
exec command
end

desc "restore most recent mysqldump (from db/backup/*.sql.*) into the current environment's database."
task :restore => :environment do
unless RAILS_ENV=='development'
puts "Are you sure you want to import into #{RAILS_ENV}?! [y/N]"
return unless STDIN.gets =~ /^y/i
end
backup_prep
wildcard = File.join( @directory, ENV['FILE'] || "#{ENV['FROM']}*.sql*" )
puts file = `ls -t #{wildcard} | head -1`.chomp # default to file, or most recent ENV['FROM'] or just plain most recent
if file =~ /\.gz(ip)?$/
command = "gunzip < #{file} | mysql #{@db_params}"
else
command = "mysql #{@db_params} < #{file}"
end
p command
puts "please wait, this may take a minute or two..."
exec command
end
end

如何解决这个问题?

最佳答案

RAILS_ENV 是一个环境变量。您必须使用 ENV['RAILS_ENV']

即:

unless ENV['RAILS_ENV']=='development'

关于ruby-on-rails - Rspec - 未初始化的常量 RAILS_ENV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31346411/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com