从rake任务调用控制器 [英] Call controller from rake task

查看:156
本文介绍了从rake任务调用控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从rake任务调用控制器动作。我的问题是什么方法最好准备http请求?感谢所有提示。

I would like to call controller action from rake task. My question is what method is the best to prepare http request? Thanks for all tips.

编辑:
请别人提示?我试过这个并没有用:

Have someone another tip? I tried this and not work:

controller_obj = Controller.new
controller.your_method

我遇到了这个例外:

rake aborted!
uninitialized constant Controller

Edit2:
我试过:

I tried:

sess = ActionController::Integration::Session.new
sess.post('/route', 'codes=3')

但我得到了(我在rake文件中要求'action_controller / integration'):

But I got (I have require 'action_controller/integration' in rake file) :

rake aborted!
cannot load such file -- action_controller/integration


推荐答案

task :use_controller_method_in_rake => :environment do
  session = ActionDispatch::Integration::Session.new(Rails.application)
  session.get "/foo"
  session.post "/bar", {my_post_params: 'foobar'}
end

这篇关于从rake任务调用控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆