简体   繁体   中英

Run a custom rake task on rails

I am trying to make a custom rake task, but I cannot seem to get it working. My code looks like this:

namespace :demotask do

  desc "display the current environment of rake"
  task :current_environment => :environment do
    puts "You are running rake task in #{Rails.env} environment"
  end

end

and is placed in a file named test.rake under lib/tasks . The Rakefile is there, but I do get this error when running the task in the console:

NameError: undefined local variable or method 'current_environment' for main:Object

I tried to restart the server as well.

You should use the namespace to call it from the terminal, and probably it's a good idea to use bundler :

bundle exec rake demotask:current_environment

Also, try to change task definition to:

task :current_environment => :environment do
...

Including => :environment will tell Rake to load the full application environment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM