Rails在某些日期交付?

时间:2013-12-10 01:17:46

标签: ruby-on-rails

我正在根据futureme.org制作一个小应用程序进行练习。用户转到页面,看到表单,用电子邮件,主题,消息和交付日期填写。然后该应用程序在该日期传递他们的消息(电子邮件)。

我遇到的问题是,我不知道如何在模型中设置交货日期选项&在视图中(即。rails是否有日期选项?)。这是我的代码;

Letter.rb

class Letter < ActiveRecord::Base

      VALID_EMAIL_REGEX = /\A[\w+\-,]+@[a-z\d\-.]+\.[a-z]+\z/i
    validates_presence_of :email, presence: true, format: { with: VALID_EMAIL_REGEX },
                          uniqueness: { case_sensitive: false }

    validates_length_of :subject, presence: true, :maximum => 30

    validates_presence_of :message

    validates_presence_of :deliver_on #not sure if this is right
end

Letters_Controller.rb

class LettersController < ApplicationController

    def create
        @letter = Letter.new(letter_params)

        if @letter.save
            LetterMailer.letter_confirm(@letter).deliver
            redirect_to letters_path, :notice => "Your letter was sent!"
        else
            render "welcome/home"
        end
    end

    private

    def letter_params
        params.require(:letter).permit(:email, :subject, :message)
    end

end

lettermailer.rb

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

  def letter_confirm(letter)
    mail(to: @letter.email, subject: "Thanks from Future Self")
  end

  def letter_email(letter)
    @letter = letter
    @url = 'http://futureself.herokuapp.com'
    mail(to: @letter.email, subject: @letter.subject)
  end
end

主页表格;

<%= form_for @letter, :html => {:class => 'form-horizontal'} do |f| %>
     <% if @letter.errors.any? %>
       <div class="error_messages">
       <h3><%= pluralize(@letter.errors.count, "error")%> stopped this message from being saved</h3>

       <ul>
       <% @letter.errors.full_messages.each do |msg| %>
         <li><%= msg %></li>  
        <% end %>
      </ul>

    <% end %>  

  <div class="field">
      <%= f.label :email %><br />
      <%= f.text_field :email %>
    </div>
    <div class="field">
      <%= f.label :subject %><br />
      <%= f.text_field :subject %><br />
    </div>
    <div class="field">
      <%= f.label :message, "Message" %><br />
      <%= f.text_area :message, size: "100x10" %>
    </div>
<!-- Deliver on Option -->
    <div class="field"><%= f.submit "Submit", class: "btn btn-small btn-primary" %></div>

  <% end %> 

</body> 
</html>

任何帮助都会很棒。

2 个答案:

答案 0 :(得分:2)

你需要一个工作队列,我建议使用DelayedJob,因为它很简单(如果你使用的是ActiveRecord)https://github.com/collectiveidea/delayed_job

然后在你的LessonsController中

LetterMailer.delay(run_at: @letter.deliver_on).letter_confirm(@letter)

这假设deliver_on是一个DateTime对象。


你可能想要考虑的事情:

答案 1 :(得分:1)

验证

我喜欢使用validates_timeliness宝石。所以你可以做像

这样的事情
validates_date :deliver_on, :before => lambda { Time.zone.now + 1.year },
                            :before_message => "must be earlier one year from today",
                            :on_or_after => lambda { Time.zone.now.end_of_day },
                            :on_or_after_message => "must be after today"

表格

您可以使用jquery-ui-rails

    f.input :deliver_on, as: :string, label: "Date of delivery (dd/mm/yyyy)", input_html: {class: "jquery-ui-date", value: f.deliver_on ? l(f.deliver_on) : nil }

请不要让我开始使用RegEx来发送电子邮件。尝试谷歌搜索;)

是的,就像@mattsmith说检查延迟工作。