在 rails 任务中没有正确设置参数?
Not setting arguments properly in rails tasks?
我很好奇为什么我的 args
变量在以下任务中总是返回为 {}
:
desc "Create an Api Key assuming one doesn't exist."
task :create_api_key, [:name] => :environment do | t, args |
if !ApiKey.find_by_application_name(args[:name])
binding.pry
if ApiKey.new(:application_name => args[:name], :api_key => SecureRandom.hex(32)).save!
puts "Your key is: " + ApiKey.find_by_application_name(args[:name]).api_key
else
puts "Could not create the api key, you might be missing an argument: Application Name."
end
else
puts "This application already contains an api key."
end
end
下面是一个运行的任务(注意binding.pry
):
$ bin/rake create_api_key "xaaron_test"
From: /Users/Adam/Documents/Rails-Projects/BlackBird/lib/tasks/create_api_key.rake @ line 4 :
1: desc "Create an Api Key assuming one doesn't exist."
2: task :create_api_key, [:name] => :environment do | t, args |
3: if !ApiKey.find_by_application_name(args[:name])
=> 4: binding.pry
5: if ApiKey.new(:application_name => args[:name], :api_key => SecureRandom.hex(32)).save!
6: puts "Your key is: " + ApiKey.find_by_application_name(args[:name]).api_key
7: else
8: puts "Could not create the api key, you might be missing an argument: Application Name."
9: end
[1] pry(main)> args
=> {}
即使我这样做 bin/rake create_api_key xaaron_test
我也会遇到同样的问题。到底是怎么回事?有什么小错误我忘了吗?
更新
我也吐了t
看看里面有什么:
pry(main)> t
=> <Rake::Task create_api_key => [environment]>
您可以将参数直接放在任务名称后的 [] 中,从而将参数传递给任务。
例如
rake create_api_key[xaaron_test]
如果使用zsh,需要对开头进行转义[
例如
rake create_api_key\[xaaron_test]
我很好奇为什么我的 args
变量在以下任务中总是返回为 {}
:
desc "Create an Api Key assuming one doesn't exist."
task :create_api_key, [:name] => :environment do | t, args |
if !ApiKey.find_by_application_name(args[:name])
binding.pry
if ApiKey.new(:application_name => args[:name], :api_key => SecureRandom.hex(32)).save!
puts "Your key is: " + ApiKey.find_by_application_name(args[:name]).api_key
else
puts "Could not create the api key, you might be missing an argument: Application Name."
end
else
puts "This application already contains an api key."
end
end
下面是一个运行的任务(注意binding.pry
):
$ bin/rake create_api_key "xaaron_test"
From: /Users/Adam/Documents/Rails-Projects/BlackBird/lib/tasks/create_api_key.rake @ line 4 :
1: desc "Create an Api Key assuming one doesn't exist."
2: task :create_api_key, [:name] => :environment do | t, args |
3: if !ApiKey.find_by_application_name(args[:name])
=> 4: binding.pry
5: if ApiKey.new(:application_name => args[:name], :api_key => SecureRandom.hex(32)).save!
6: puts "Your key is: " + ApiKey.find_by_application_name(args[:name]).api_key
7: else
8: puts "Could not create the api key, you might be missing an argument: Application Name."
9: end
[1] pry(main)> args
=> {}
即使我这样做 bin/rake create_api_key xaaron_test
我也会遇到同样的问题。到底是怎么回事?有什么小错误我忘了吗?
更新
我也吐了t
看看里面有什么:
pry(main)> t
=> <Rake::Task create_api_key => [environment]>
您可以将参数直接放在任务名称后的 [] 中,从而将参数传递给任务。
例如
rake create_api_key[xaaron_test]
如果使用zsh,需要对开头进行转义[
例如
rake create_api_key\[xaaron_test]