我正在尝试设置Mandrill API,以便在用户点击按钮时发送电子邮件,但我似乎无法将其发送。电子邮件将从控制台发送完全正常,所以我知道这不是模板的问题(设计电子邮件也发送查找)。
我认为这与我在控制器中如何设置它有关,但我找不到任何帮助,我应该把它放在哪里。
以下是控制器的代码:
def attending
@event = Event.find(params[:id])
type = params[:type]
if type == "attending" && @event.space != 0
current_user.attending << @event
@event.space = @event.space - 1
@event.save
AdminMailer.new_attending(@event.user, current_user)
redirect_to :back, notice: "You've joined the group. Your number will be sent to #{@event.user.name}"
else type == "unattending"
current_user.attending.delete(@event)
redirect_to :back, notice: "You've removed yourself from the group"
end
end
这是admin_mailer.rb
class AdminMailer < ActionMailer::Base
require 'mandrill'
def mandrill_client
@mandrill_client ||= Mandrill::API.new MANDRILL_API_KEY
end
def new_attending(creator, user)
template_name = "new-attending"
template_content = []
message = {
to: [{email: creator.email, name: creator.name}],
subject: "Someone wants to go riding with you!",
merge_vars: [
{rcpt: creator.email,
vars: [
{name: "CREATOR_NAME", content: creator.name},
{name: "USER_NAME", content: user.name},
{name: "USER_NUMBER", content: user.number}
]}
]
}
mandrill_client.messages.send_template template_name, template_content, message
end
end
以下是他们在view.html.erb中点击的链接,该链接应该发送电子邮件:
<td><% if event.user != current_user && event.space != 0 && user_signed_in? %>
<% unless event.attendees.include?(current_user) %>
<%= link_to "Join", attending_event_path(event, type: "attending"), class: "btn btn-primary btn-xs", method: :put %>
<% end %>
<% end %></td>
任何帮助,找出它不发送的原因都会很棒!正如我所说,当我输入时,它在控制台中起作用:
AdminMailer.new_attending(@event, @user)
答案 0 :(得分:0)
请替换以下代码。
AdminMailer.new_attending(@event.user, current_user)
与
AdminMailer.new_attending(@event.user, current_user).deliver
我希望这会奏效。