简体   繁体   中英

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

Currently, I have a manual form-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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM