当用户提交新票证时,向管理员发送电子邮件不会发送

时间:2014-11-24 00:39:50

标签: ruby-on-rails email model actionmailer

我有一种工作方法,当用户通过设置密码激活帐户时会向用户发送电子邮件但由于某种原因它不能使用我的故障单模型方法发送给角色整数为2的用户。但是,我无法使用after_create:method_name获取发送的电子邮件。

控制台中有一个错误,似乎是一个生成的错误代码,可能是ActionMailer的问题,但很难诊断我得到的所有回报。

非常感谢任何帮助!提前谢谢!

# /mailers/user_mailer.rb
#################################
def new_ticket(ticket)
  @ticket = ticket
  mails = User.where(:role => 2)
  mail(:to => 'admin@email.com', :bcc => mails, subject: "New Ticket Submitted by: #{@user.email}")
end


# /models/ticket.rb 
#################################
class Ticket < ActiveRecord::Base
  belongs_to :user
  belongs_to :category
  belongs_to :ticket_status
  has_many :comments, :dependent => :destroy

  validates_presence_of :title, presence: true
  validates_presence_of :description, presence: true
  validates_presence_of :user_id, presence: true
  validates_presence_of :category_id, presence: true
  validates_presence_of :ticket_status_id, presence: true

 def after_create
   send_ticket_notifier
   super
 end

private
 def send_ticket_notifier
   UserMailer.new_ticket(@ticket).deliver
 end

end

控制台

SQL (0.4ms)  INSERT INTO "tickets" ("category_id", "created_at", "description", "ticket_status_id", "title", "updated_at", "user_id") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id"  [["category_id", 5], ["created_at", "2014-11-24 00:25:53.892400"], ["description", "asdfasdf"], ["ticket_status_id", 8], ["title", "asdfasd"], ["updated_at", "2014-11-24 00:25:53.892400"], ["user_id", 25]]
(14.6ms)  COMMIT
Redirected to http://localhost:3000/tickets
#<ActiveModel::Errors:0x00000007230d28 @base=#<Ticket id: 30, title: "asdfasd", description: "asdfasdf", user_id: 25, category_id: 5, created_at: "2014-11-24 00:25:53", updated_at: "2014-11-24 00:25:53", ticket_status_id: 8>, @messages={}>
Completed 302 Found in 23ms (ActiveRecord: 15.6ms)

1 个答案:

答案 0 :(得分:0)

经过一些试验和错误后发现我猜得太多了。关于为什么上述工作适用于新用户注册而不是我的新机票提交的一些说明将是一个更好的答案。

以下是我使用ActionMailer工作的方式。新文件admin_mailer.rb并删除了ticket.rb中的after_create和send_ticket_notifier

# /mailers/admin_mailer.rb
######################################
def new_ticket(ticket)
  @ticket = ticket
  mail(to: 'email@email.com', subject: "New Ticket Submitted")
end


# /controllers/tickets_controller.rb
######################################
def create
  @ticket = Ticket.new(ticket_params)
  if @ticket.save
    AdminMailer.new_ticket(@ticket).deliver
    redirect_to tickets_path, :notice => "Ticket Submitted."
  else
    redirect_to tickets_path, :alert => "Unable to submit ticket."
  end
  puts @ticket.errors.inspect
end


# /views/admin_mailer/new_ticket.html.erb
######################################
<!DOCTYPE html>
<html>
  <head>
    <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
  </head>
  <body>
    <h1>New Ticket <%= @ticket.id %></h1>
    <strong><%= @ticket.user.name %></strong>
    <p>Ticket Title: <%= @ticket.title %></p>
    <p>Ticket Description: <%= @ticket.description %></p>
  </body>
</html>