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.
Edit: Have someone another tip? I tried this and not work:
controller_obj = Controller.new
controller.your_method
I got this exception:
rake aborted!
uninitialized constant Controller
Edit2: I tried:
sess = ActionController::Integration::Session.new
sess.post('/route', 'codes=3')
But I got (I have require 'action_controller/integration' in rake file) :
rake aborted!
cannot load such file -- action_controller/integration
You should move that action's behavior to a model class, then use that action with the instance of that model class. This is the standard approach.
move
controller/action
to amodel class
(Recommended). In your rake task, do the following....And still you want to call controller action then you should create an instance of your controller in your rake task and call that action with that instance(if that method is an instance method.)
Thanks @chris-finne, that was very helpful. Note that session.response.body is where you can access the results. Here's an method I'm using in a rake task:
Notice I'm memoizing the session object since it's quite slow to instantiate and I'm calling this method many times.