开发者

How can I create a new object in cron in rails by referencing controller and passing variables?

Currently, I have a manual fo开发者_Python百科rm-submission way of creating an object which fires off stuff in the create action.

I access this form this way:

http://localhost:3000/contact_faxes/new?contact=1&fax=7&status=done

This takes me to a form, and executes stuff in the create action. As you can see, I take parameters and change them, and pass them as if it were a form submission.

But I now want to enable this same process through a cron job.

One way I did it was to type all the code back into the cron job, not using the methods new/create at all, which didn't seem like the way to go.

How would I be able to leverage the existing methods using the cron job?

Here are the new and create methods:

https://gist.github.com/746686


I would move all of that code out of the controller and into either the model or another workflow object.

In your ContactFax object, create do something like:

ContactFax < ActiveRecord::Base

  after_create :send_fax

  def send_fax
    pdf = Prawn::Document.new
    ...
    OutboundMailer.deliver_fax_email #etc
  end
end

That way, in your cron job, you can access the objects without your controller, which is what's getting in your way now.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜