I want to run a Rake task that asks the user for input.
I know that I can supply input on the command line, but I want to ask the user if they are sure they want to proceed with a particular action in case they mistyped one of the values supplied to the Rake task.
Go to Websites & Domains and click Ruby. After gems installation you can try to run a Rake task by clicking Run rake task. In the opened dialog, you can provide some parameters and click OK - this will be equivalent to running the rake utility with the specified parameters in the command line.
We can also write our custom Rake tasks in Rails environment by creating files with . rake extension in ./lib/tasks. It's a common practice after cloning a repository for the first time, to run ./bin/setup, in order to automatically fetch all the libraries, create db, seed data etc.
rake extension and are placed in Rails. root/lib/tasks . You can create these custom rake tasks with the bin/rails generate task command. If your need to interact with your application models, perform database queries and so on, your task should depend on the environment task, which will load your application code.
Something like this might work
task :action do STDOUT.puts "I'm acting!" end task :check do STDOUT.puts "Are you sure? (y/n)" input = STDIN.gets.strip if input == 'y' Rake::Task["action"].reenable Rake::Task["action"].invoke else STDOUT.puts "So sorry for the confusion" end end
Task reenabling and invoking from How to run Rake tasks from within Rake tasks?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With