如何向订阅者发送特定电子邮件

时间:2014-11-19 06:55:00

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

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

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

这里有两个问题,

1. Categories aren't syncing to subscribers because the foreign_key isn't being set. 
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. The fact that it's a model constant that's being selected
is why the foreign key isn't being set between subscriber and category, how do I set it?

2. I'd like to automate it so that these emails are sent to subscribers once a day. 
They should also be in randomized order.
How might I do this? 

查看代码(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>

Schema.rb

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

模特:

category.rb

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

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

end

quote.rb

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所有类别的订阅者(但如上所述返回nil)

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

category = Category.find(1)
category.quotes

在Rails中我运行:

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

在我邮寄的“SendMyEmail”功能中我有

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

更新图片示例

example

1 个答案:

答案 0 :(得分:1)

所以没有比开始的官方文档更好的地方:

http://guides.rubyonrails.org/action_mailer_basics.html

如果您正在寻找应该调用发送电子邮件的方法的位置,您可能希望在接收表单POST的方法中在控制器中执行此操作。看一下这个例子,取自指南:

class UsersController < ApplicationController
  # POST /users
  # POST /users.json
  def create
    @user = User.new(params[:user])

    respond_to do |format|
      if @user.save
        # Tell the UserMailer to send a welcome email after save
        UserMailer.welcome_email(@user).deliver

        format.html { redirect_to(@user, notice: 'User was successfully created.') }
        format.json { render json: @user, status: :created, location: @user }
      else
        format.html { render action: 'new' }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end
end

另外,如果你正在查看 Rails 4.2 here,那么你有一个应用示例,使用ActiveJob