开发者

How can I call a controller action from a rake task?

开发者 https://www.devze.com 2022-12-23 19:22 出处:网络
I have a controller action that generates a number of excel reports, this takes about 10 minutes to do. Sometimes I call it from my webapp, which is why it is an action.

I have a controller action that generates a number of excel reports, this takes about 10 minutes to do. Sometimes I call it from my webapp, which is why it is an action.

But I also want to create a开发者_运维问答 rake task to run this, so I can schedule it to automatically run once a night.

Any way to do this?


Can you handle the report generation from your models? Models should be doing most of the work anyway and can be accessed from Rake tasks:

task :reports => :environment do
  ...
  # Do stuff with your models.
end


I think you'll have to move your code into your model. Since it's bad to put knowledge about output rendering in models, I'd suggest putting all the business logic and data manipulation in the model, but then put the rendering code in your rake task. That would make the rake task analogous to the controller used on the web - maintaining separation of concerns.

You can look at ActionView::Base and work from there to figure out how to trigger rendering of templates.

0

精彩评论

暂无评论...
验证码 换一张
取 消