2

// This is a question for Ruby on Rails

I have a Rails Tasks that should take arguments from command line.

Looks like this :

 task update: :environment do

if (ARGV[1] == "DEBUG")
  DEBUG = true
else
  DEBUG = false
end

Now I can run the command line :

rails call:update DEBUG

and it works !

But after the task was finished I got also the message :

rails aborted!
Don't know how to build task 'DEBUG' (See the list of available tasks with `rails --tasks`)

I looking already around the community, but all what I found was quite old and seems not to be compatible with Rails 6 ! So thats the reason why I asking here.

1 Answer 1

1

I try with the code you shared above I got the same error of rake abort.

its Comming because they way you calling the rake task with the argument it assume the DEBUG is another task so it try to find and give this error.

Here is proper way of doing it I did some modification in the code to get rid of the error use the following code it will work perfectly

task :update, [:value] => [:environment] do |t, args|
  if (args[:value] == "DEBUG")
    DEBUG = true
    p "Value is True"
  else
    DEBUG = false
    p "Value is False"
  end
end

it works perfectly enter image description here

You can also put this task in the namespace and then call it from there as well

namespace :debug_task do
 # task code here what I mentioned above 
end

and then call it to form the terminal like this

rake debug_task:update["DEBUG"]

enter image description here

1
  • Thanks for fast answer, yes its works also for me ! Thanks. But is there also a way to omit the rocket operator and use symbols instead ? ([:value] => [:environment]) Commented Apr 16, 2020 at 15:41

Not the answer you're looking for? Browse other questions tagged or ask your own question.