Ruby on rails 如何向订户发送特定电子邮件
所以,我有3个模型:报价、分类和订户 本质上,它是一个时事通讯应用程序。订阅者可以选择一个类别,然后输入他们的电子邮件地址,他们将通过电子邮件收到与该类别相关的报价 这里有两个问题Ruby on rails 如何向订户发送特定电子邮件,ruby-on-rails,ruby,email,ruby-on-rails-4,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.
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?
查看代码订阅服务器/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>
模型:
category.rb
在Rails c中,我运行:
Category.all.each do |category|
Mailer.SendMyEmail(category, category.quotes.first, category.subscribers).deliver
end
在I mailer中的sendmail函数中
def SendMyEmail(category, quote, subscribers)
#MY CODE FOR TYHE VIEW HERE
end
更新图像示例
因此,没有比官方文件更好的地方可以开始: 如果您正在寻找应该调用方法来发送电子邮件的位置,那么您可能希望在控制器中,在接收表单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,您有一个应用程序示例,使用。您必须使用ActionMailer-Reference:您能告诉我Quote和Category类中的模型字段吗?它们在原始文章Quote.rb和Category.rb中。@sonnyback您需要保存用户和类别之间的关联,然后,每当您有此类别中的新项目时,向用户发送一封电子邮件。用我的两个问题更新原始帖子。@SonnyBlack如果您有更多问题,我认为您应该打开另一个问题。
category = Category.find(1)
category.quotes
Category.all.each do |category|
Mailer.SendMyEmail(category, category.quotes.first, category.subscribers).deliver
end
def SendMyEmail(category, quote, subscribers)
#MY CODE FOR TYHE VIEW HERE
end
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