[英]Getting Actionmailer to send an email
I have asked a question similar to this and received some great help, it has clarified some understanding and concepts.This is the first time using actionmailer so I am trying to get a better understanding 我问了一个与此类似的问题并获得了很大的帮助,它澄清了一些理解和概念。这是第一次使用actionmailer,因此我试图更好地理解
What i am trying to achieve is for an email to be to sent to a user who clicks a remind me button. 我要达到的目的是将一封电子邮件发送给单击“提醒我”按钮的用户。 It is a simple library app where a user can check out, check in and request a reminder when a book is checked back in..I'm just struggling on this last part of logic, once I see it working Im sure I will understand it better 这是一个简单的图书馆应用程序,用户可以在其中签出,签到并在提回书时要求提醒。.我只是在逻辑的最后一部分上苦苦挣扎,一旦我看到它可以正常工作,我肯定会理解更好
So when the check_out status changes for a book there are various posts to the book model 因此,当某本书的check_out状态更改时,该图书模型会有各种帖子
Check out
<%= form_for @book do |f| %>
<%= f.label :checked_out, "Check Book Out?" %>
<%= f.check_box :checked_out, {}, true %>
<%= f.hidden_field :user_id, :value => current_user.id %>
<%= f.hidden_field :checked_out, :value => true %>
<%= f.submit 'Checkout' %>
<% end %>
Check In
<%= form_for @book do |f| %>
<%= f.label :checked_out, "Check Book Back In" %>
<%= f.check_box :checked_out, {checked: false}, false %>
<%= f.hidden_field :user_id, :value => nil %>
<%= f.hidden_field :checked_out, :value => false %>
<%= f.submit 'Check In' %>
<% end %>
Register Interest
<%= form_for @book do |f| %>
<%= f.label :remind_me, "let Me know when book back in" %>
<%= f.check_box :remind_me, {checked: false}, false %>
<%= f.hidden_field :remind_me, :value => current_user.id %>
<%= f.submit 'Remind Me' %>
<% end %>
In my book model I have this logic to recognise that when a book is checked back in after a remind me request has been made an email is sent notifying the user that the book is back in the library 在我的书本模型中,我具有这种逻辑,可以识别出在发出“提醒我”请求后重新签入书本时,会发送一封电子邮件,通知用户该书本已归还图书馆。
after_save :reminder_mailer
def reminder_mailer
if !self.checked_out && self.changes[:user_id] && self.user.nil?
ReminderMailer.remind_email(@user).deliver
end
and my action mailer 和我的行动邮件
class ReminderMailer < ActionMailer::Base
default from: "my email address"
def remind_email(user)
@user = user
mail(:to => user.email, :subject => "Library Clerk")
end
end
As you can probably see I am struggling with what to pass as the users email who made the remind me request. 正如您可能看到的那样,当用户发送提醒我请求的电子邮件时,我正在为传递的内容而苦苦挣扎。 When the remind me request is made I post the users_id to the column remind_me. 发出提醒我的请求后,我将users_id张贴到alert_me列中。 I have a seperate user model (devise).. 我有一个单独的用户模型(设计)。
Is part of my problem being that when the users_id is posted to the remind me column it is then just a regular integer and not linked to the users_id? 我的问题的一部分是,当将users_id发布到“提醒我”列时,它只是一个常规整数而没有链接到users_id吗?
Any advice appreciated 任何建议表示赞赏
You could create a Reminder
model (and table in DB) that keeps track of reminders that users have set 您可以创建一个Reminder
模型(和数据库中的表)来跟踪用户设置的提醒
class Reminder < ActiveRecord::Base
belongs_to :user
belongs_to :book
end
class Book < ActiveRecord::Base
has_many :reminders
end
Show a button beside a book, make sure to add @reminder = Reminder.new
to the action that renders the following form. 在书旁边显示一个按钮,确保将@reminder = Reminder.new
添加到呈现以下表单的操作中。
<%= form_for @reminder do |f| %>
<%= f.label :remind_me, "let Me know when book back in" %>
<%= f.hidden_field :book_id, :value => @book.id %>
<%= f.submit 'Remind Me' %>
<% end %>
You will have a reminders controller, (add resources :reminders, only: [:create]
to routes.rb) 您将拥有一个提醒控制器,(将resources :reminders, only: [:create]
添加resources :reminders, only: [:create]
到route.rb)
class RemindersController < ApplicationController
def create
@book = Book.find(params[:reminder][:book_id])
Reminder.create(user: current_user, book_id: @book.id)
redirect_to @book
end
end
Then in the after_save
of your book model, you would send emails to all users that have reminders for this book 然后,在您的图书模型的after_save
中,您将向所有提醒该书的用户发送电子邮件
def reminder_mailer
if !self.checked_out && self.changes[:user_id] && self.user.nil?
# get all users waiting for a remind on this book
@users = self.reminders.map(&:user)
@users.each do |user|
ReminderMailer.remind_email(user).deliver
end
# remove reminders as users have been reminded
self.reminders.destroy_all
end
end
Notice: Make sure that you add a validation that no user will request reminder to the same book. 注意:请确保您添加的验证是没有用户会向同一本书请求提醒。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.