Reputation: 12913
I am curious as to why my args
variable is always coming back as {}
in the following task:
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
The following is a run of the task (Note the 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
=> {}
Even if I do bin/rake create_api_key xaaron_test
I get the same issue. What is going on? is there some small mistake some where I forgot about?
I also spit out t
to see what was in there:
pry(main)> t
=> <Rake::Task create_api_key => [environment]>
Upvotes: 0
Views: 27
Reputation: 25717
You pass arguments to a task by enclosing them in [] directly after the task name.
e.g.
rake create_api_key[xaaron_test]
If you use zsh, you need to escape the opening [
e.g.
rake create_api_key\[xaaron_test]
Upvotes: 1