RAILS - Passing parameters to a Rake task [duplicate]
Asked Answered
F

2

5

i need some help :( well, i need to pass 1 parameter to a rake task. And I'm not 100% sure how to do this, I've tried a lot of things, but nothing actually works. it's look like this :

 {  task :export, [:arg1]  => :environment do
      puts "Exporting..."
      Importer.export_to_csv([:arg1]).to_i
      puts "done."
    end }

and then 'export_to_csv' method spoused to get the arg when I ran in my terminal : 'rake export 1' or 'rake export [1]' I keep getting the same error-answer: 'rake aborted! NoMethodError: undefined method `id' for nil:NilClass'

which is means - he didn't recognize this input. Thank u guys ahead,

Fabron answered 4/1, 2016 at 20:22 Comment(0)
B
10

[:arg1] must be args[:arg1] (or whatever name you use as block argument). Here's the code:

task :export, [:arg1] => :environment do |t, args|
  puts "Exporting..."
  Importer.export_to_csv(args[:arg1])
  puts "done."
end

Usage:

rake export[foo1]
Blackthorn answered 4/1, 2016 at 20:35 Comment(4)
it's still dont work, I got an error : rake aborted! NameError: undefined local variable or method `args' for main:Object Thank you ahead !!!Fabron
"it doesn't work" is not useful for debugging. What error are you receiving?Blackthorn
Are you sure you copied the code I provided you? It looks like you forgot to pass the |t, args| block parameters.Blackthorn
well, now it's work !!! thank you so much!Fabron
C
13

try this, also have a look on following url. 4 Ways to Pass Arguments to a Rake Task

task :export, [:arg1] => :environment do |t, args|
  puts "Exporting..."
  Importer.export_to_csv(args[:arg1].to_i)
  puts "done."
end

and run it using

rake add\[1\]

#OR

rake 'export[1]'
Complect answered 4/1, 2016 at 20:36 Comment(1)
thank you soooo much !!! it's work!Fabron
B
10

[:arg1] must be args[:arg1] (or whatever name you use as block argument). Here's the code:

task :export, [:arg1] => :environment do |t, args|
  puts "Exporting..."
  Importer.export_to_csv(args[:arg1])
  puts "done."
end

Usage:

rake export[foo1]
Blackthorn answered 4/1, 2016 at 20:35 Comment(4)
it's still dont work, I got an error : rake aborted! NameError: undefined local variable or method `args' for main:Object Thank you ahead !!!Fabron
"it doesn't work" is not useful for debugging. What error are you receiving?Blackthorn
Are you sure you copied the code I provided you? It looks like you forgot to pass the |t, args| block parameters.Blackthorn
well, now it's work !!! thank you so much!Fabron

© 2022 - 2024 — McMap. All rights reserved.