2012-09-19 6 views
7

Tôi đang cố chuyển đối số cho tác vụ Rake và truy cập mô hình của tôi như đã đề cập trong" How to pass command line arguments to a rake task "và" Do rails rake tasks provide access to ActiveRecord models? ".bị hủy bỏ! undefined method `map 'for: name: Symbol

Tuy nhiên thêm => :environment để mã này:

require 'rake' 

namespace :genre do 
    desc "Add a genre" 
    task :add, :genre_id, :name => :environment do |t, args| 
     puts args.name 
    end 
end 

Cung cấp cho tôi lỗi này khi tôi chạy nhiệm vụ:

rake genre:add['1','Abc'] 
rake aborted! 
undefined method `map' for :name:Symbol 

(See full trace by running task with --trace) 

Nếu tôi xóa => :environment nó hoạt động tốt.


UPDATE:

TRACE:

rake --trace genre:add['1','Abc'] 
rake aborted! 
undefined method `map' for :name:Symbol 
/Users/user/.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/task.rb:254:in `set_arg_names' 
/Users/user/.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/task_manager.rb:29:in `define_task' 
/Users/user/.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/task.rb:310:in `define_task' 
/Users/user/.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/dsl_definition.rb:32:in `task' 
/private/var/www/html/someapp.com/subdomains/someapp.heroku.com/someapp/lib/tasks/genre.rake:5:in `block in <top (required)>' 
/Users/user/.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/task_manager.rb:207:in `in_namespace' 
/Users/user/.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/dsl_definition.rb:102:in `namespace' 
/private/var/www/html/someapp.com/subdomains/someapp.heroku.com/someapp/lib/tasks/genre.rake:3:in `<top (required)>' 
/Users/user/.rvm/gems/ruby-1.9.2-p318/gems/railties-3.2.3/lib/rails/engine.rb:425:in `load' 
/Users/user/.rvm/gems/ruby-1.9.2-p318/gems/railties-3.2.3/lib/rails/engine.rb:425:in `block in load_tasks' 
/Users/user/.rvm/gems/ruby-1.9.2-p318/gems/railties-3.2.3/lib/rails/engine.rb:425:in `each' 
/Users/user/.rvm/gems/ruby-1.9.2-p318/gems/railties-3.2.3/lib/rails/engine.rb:425:in `load_tasks' 
/Users/user/.rvm/gems/ruby-1.9.2-p318/gems/railties-3.2.3/lib/rails/application.rb:145:in `load_tasks' 
/Users/user/.rvm/gems/ruby-1.9.2-p318/gems/railties-3.2.3/lib/rails/railtie/configurable.rb:30:in `method_missing' 
/private/var/www/html/someapp.com/subdomains/someapp.heroku.com/someapp/Rakefile:7:in `<top (required)>' 
/Users/user/.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/rake_module.rb:25:in `load' 
/Users/user/.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/rake_module.rb:25:in `load_rakefile' 
/Users/user/.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/application.rb:501:in `raw_load_rakefile' 
/Users/user/.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/application.rb:82:in `block in load_rakefile' 
/Users/user/.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling' 
/Users/user/.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/application.rb:81:in `load_rakefile' 
/Users/user/.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/application.rb:65:in `block in run' 
/Users/user/.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling' 
/Users/user/.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/application.rb:63:in `run' 
/Users/user/.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/bin/rake:33:in `<top (required)>' 
/Users/user/.rvm/gems/ruby-1.9.2-p318/bin/rake:19:in `load' 
/Users/user/.rvm/gems/ruby-1.9.2-p318/bin/rake:19:in `<main>' 
+0

bạn có thể cung cấp dấu vết hay không. –

Trả lời

20

Hình như cú pháp đã tắt. Hãy thử:

task :add, [:genre_id, :name] => :environment do |t, args| 
+2

Đầu tiên: hoạt động đẹp mắt. Thứ hai đưa ra: CẢNH BÁO: 'nhiệm vụ: t, arg,: needs => [deps]' không được chấp nhận. Thay vào đó, hãy sử dụng 'tác vụ: t, [args] => [deps]'. – Snowcrash

+0

không sử dụng: nhu cầu ... chỉ cần sử dụng tác vụ: foo, [: arg1,: arg2] => [: bar] – nterry