如何发送与类别相关的邮件

时间:2014-11-19 15:33:49

标签: ruby-on-rails ruby ruby-on-rails-4

所以,我有3个模型报价,类别和订阅者。

基本上,它是一个简报应用程序。订阅者可以选择一个类别,然后输入他们的电子邮件,他们将通过电子邮件接收与该类别相关的报价。

这里有两个问题,

1. The category's are stored in a constant in the category model. 
so how do I display the category type that the subscriber chose
in text format? I'd like to show in the email
something like "here's your email on <%= category.name %>" which would translate to
"here's your email on Food." 

2. I'd like to ONLY send emails about the category that the subscriber subscribed to. 
How might I accomplish this? Please provide examples. 

这是我到目前为止所做的:

Category.rb

class Category < ActiveRecord::Base
  belongs_to :quote
  belongs_to :subscriber

  CATEGORY_TYPE = {
    1 => "Food",
    2 => "Fitness",
    3 => 'Cats',
  }

end

Quote.rb

class Quote < ActiveRecord::Base
  has_many :categories
  belongs_to :category

  validates :title, presence: true

end

Subscriber.rb

class Subscriber < ActiveRecord::Base
  has_one :category

  validates :email, presence: true
end

模式

  create_table "categories", force: true do |t|
    t.string   "name"
    t.integer  "quote_id"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "subscriber_id"
    t.integer  "category_type"
  end

  create_table "quotes", force: true do |t|
    t.string   "title"
    t.text     "body"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "subscribers", force: true do |t|
    t.string   "email"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

更新subscribers_mailer.rb

  def SendMyEmail(email, category, quote, subscribers)
    @category = category
    @quote = quote
    @subscribers = subscribers
    @email = email

    mail to: email, subject: 'New Quotes'
   end
  end

然后当然:

Category.all.each do |category|
    SubscriptionMailer.SendMyEmail("email@test.com", category, category.quotes.first, category.subscribers).deliver
end

更新

There's two remaining issues here, 

    1. Categories aren't syncing to subscribers. For example when I run category.subscribers.last 
    it's nil. and category.subscribers throws an empty array. How can I sync these? I think it has
    to do with the fact that subscribers are selecting a category from the Category::CATEGORY_TYPES
    constant as seen in the view code below.

    2. I'd like to automate it so that these emails are sent to subscribers once a day. 
    How might I do this? 


view code (subscribers/new.html.erb:

    <div class="styled email-input2">
        <%= form_for @subscriber do |f| %>
        <% if @subscriber.errors.any? %>
          <div class="errorExplanation" style="color: white;">
          <h2><%= pluralize(@subscriber.errors.count, 'error') %> encountered:</h2>
          <ul>
          <% @subscriber.errors.full_messages.each do |m| %>
          <li><%= m %></li>
         <% end %>
         </ul>
         </div>
      <% end %>
      <%= f.fields_for :subscriber, @subscriber.build_category do |cat| %>
      <%= cat.select(:category_type, Category::CATEGORY_TYPE.map{|p| [p[1], p[0]]}, {prompt: 'Please select'}, {class: 'styled email-input2'}) %>
      <% end %>
    </div>

1 个答案:

答案 0 :(得分:1)

好的,首先让我们修改迁移:

create_table "categories", force: true do |t|
#DELETE QUOTE_ID AND SUBSCRIBER ID
t.string   "name"    
t.datetime "created_at"
t.datetime "updated_at"
t.integer  "category_type"
end

create_table "quotes", force: true do |t|
t.string   "title"
t.text     "body"
t.datetime "created_at"
t.datetime "updated_at"
t.references :category  #this will add integer category_id

end

create_table "subscribers", force: true do |t|
    t.string   "email"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.references :category   #this will add integer category_id

end

接下来,我们将修改模型以重新迁移迁移中的更改:

class Category < ActiveRecord::Base
    has_many :quotes
    has_many :subscribers

    CATEGORY_TYPE = {
    1 => "Food",
    2 => "Fitness",
    3 => 'Cats',
    }

end


class Quote < ActiveRecord::Base
    belongs_to :category
    validates :title, presence: true

end


class Subscriber < ActiveRecord::Base
    belongs_to :category

    validates :email, presence: true
end

现在,您可以使用以下内容获取cateogyr的订阅者:

category = Category.find(1)#use你想要的任何id category.subscribers #list所有订阅者的列表

获取类别的引用同样是直截了当的:

category = Category.find(1)
category.quotes

因此,假设您的邮件程序为要发送的电子邮件提供了类别,报价和订阅者列表

Category.all.each do |category|
    Mailer.SendMyEmail(category, category.quotes.first, category.subscribers).deliver
end

在邮件程序的“SendMyEmail”功能中,您将拥有

def SendMyEmail(category, quote, subscribers)
    #YOUR CODE FOR TYHE VIEW HERE
end