本文介绍了在控制器中运行 rake 任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在我的控制器中运行一个 rake 任务.有没有办法做到这一点?
I'd like to run a rake task in my controller. Is there any way to do this?
推荐答案
我同意 ddfreynee,但如果你知道你需要什么代码可以看起来像:
I agree with ddfreynee, but in case you know what you need code can look like:
require 'rake'
Rake::Task.clear # necessary to avoid tasks being loaded several times in dev mode
Sample::Application.load_tasks # providing your application name is 'sample'
class RakeController < ApplicationController
def run
Rake::Task[params[:task]].reenable # in case you're going to invoke the same task second time.
Rake::Task[params[:task]].invoke
end
end
您可以改为在初始化程序中要求 'rake' 和 .load_tasks.
You can require 'rake' and .load_tasks in an initializer instead.
这篇关于在控制器中运行 rake 任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!