-2

new on rails and using windows for now,, i have web page that user insert name, date and email. depending on his date input i want to send him email(if @dop.date=date.now-7.days then send email ). how could i implement it?? i want the system get user dates and check it if user insert date, and the date has been for 7 days then send him email...


class UserMailer < ActionMailer::Base
  default from: "from@example.com"

  def welcome_email(dop)
    @dop = dop
    @url  = "http://example.com/login"
    mail(:to => dop.mail, :subject => "Welcome to My Awesome Site")
  end
end

 def create
    @dop = Dop.new(params[:dop])

    respond_to do |format|
      if @dop.save
        UserMailer.welcome_email(@dop).deliver
        format.html { redirect_to @dop, notice: 'Dop was successfully created.' }
        format.json { render json: @dop, status: :created, location: @dop }
      else
        format.html { render action: "new" }
        format.json { render json: @dop.errors, status: :unprocessable_entity }
      end
    end
  end

here is my model: class Dop < ActiveRecord::Base attr_accessible :date,:mail,:name validates_presence_of:name validates_presence_of:mail validates_uniqueness_of:mail

end


  • There are already many questions on SO about background tasks in Rails. Search or Google 'rails background tasks' – railsdog Jun 26 '12 at 11:28
  • 1
    You need to run a periodic task outside your rails application that sends out the e-mails. You could make it a custom Rake task that you launch periodically for example using a windows service: http://stackoverflow.com/questions/163497/running-a-ruby-program-as-a-windows-service . Then you run it once per day to send out all the e-mails for that day. – Casper Jun 26 '12 at 12:03
  • This is also related, although not precisely the solution you need, but it will teach you things which will be useful: http://railscasts.com/episodes/127-rake-in-background – Casper Jun 26 '12 at 12:05
  • i want to use "if" statement to do it?? – Abdulrahman Al Samadi Jun 26 '12 at 13:01
  • such like that:if @dop.date=date.now-7.days then send email?? – Abdulrahman Al Samadi Jun 26 '12 at 13:02

1 Answers1

1

Use cron to schedule a repeating rake task. If you're using heroku, you can get cron as an add-on. But first, of course, you need to write the rake task -- for tips:

http://railscasts.com/episodes/66-custom-rake-tasks/

http://jasonseifer.com/2010/04/06/rake-tutorial

Long and the short - rake is a file that allows you to define various tasks and establish dependencies among those tasks. It's perfect for administrative/cleanup tools, or, in your case, something outside the actual execution of your application.

danieltahara
  • 4,743
  • 3
  • 18
  • 20
  • i want to use if statement??? what should i do??? dont want to use cron – Abdulrahman Al Samadi Jun 26 '12 at 14:49
  • Yes. The 'If' is the key to understanding and conquering the Universe. If Einstein could, he would use Ruby's 'if' statement. For the love of God please understand that you need some kind of time-based job scheduler to do it properly. Not the all-migthy 'if' statement, put in somewhere in the Rails controller or model. – socjopata Jun 26 '12 at 15:12
  • the user insert the date, based on his input on date i want to send him from the date which he entered+7days an email?? that is all the problem?? – Abdulrahman Al Samadi Jun 27 '12 at 08:59