简体   繁体   中英

Custom rake task for mountable engine

I am unable to run a custom rake task in a mountable engine I created. When you create the mountable engine, by default, there is a Rails.root/lib/tasks directory with a rake file named after the engine:

internal_management_system.rake

Inside this file, I create a simple task:

task :internal_management_system do
  puts 'hello world'
end 

Now from Rails.root directory, I try to run it:

$ rake internal_management_system
rake aborted!
Don't know how to build task 'internal_management_system'

What am I doing wrong?

If you give your task a namespace it should work. So in your internal_management_system.rake file put:

namespace :db do
 task :internal_management_system do
  puts 'hello world'
 end 
end

Then run the command:

$ rake db:internal_management_system

The solution was to run it from the dummy app root. Apparently you cannot run it from the engine root.

So in engine root, you have

/lib/tasks/hello.rake

And its contents:

task :hello do
  puts 'hello world'
end

And then go to the dummy application and run rake hello

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