Ruby on rails 保存后的Rails发送带有更新列的邮件
我遇到了一个问题,我想在rails中的after_save回调之后发送邮件 下面是我的代码片段:Ruby on rails 保存后的Rails发送带有更新列的邮件,ruby-on-rails,Ruby On Rails,我遇到了一个问题,我想在rails中的after_save回调之后发送邮件 下面是我的代码片段: class Response < ActiveRecord::Base after_create :pending_with protected def pending_with self.approver = self.mapping.user end end class ResponsesController < ApplicationController
class Response < ActiveRecord::Base
after_create :pending_with
protected
def pending_with
self.approver = self.mapping.user
end
end
class ResponsesController < ApplicationController
def create
@response = current_user.responses.new(response_params)
respond_to do |format|
if @response.save
flash[:notice] = "Response has been saved successfully."
Notification.confirm_approver(@response).deliver
format.html { redirect_to dashboard_home_url }
else
flash[:error] = "There is some problem while saving the responses. Please try again."
format.html { render action: 'new' }
end
end
end
end
class Notification < ApplicationMailer
def confirm_approver(response)
@response = response
p "============"
p @response.approver.email.inspect
end
end
当我检查数据库中的记录时,AppServer('id')成功保存。我有什么遗漏吗
回溯:
app/mailers/notification.rb:11:in `confirm_approver'
vendor/gems/ruby/1.9.1actionpack (4.1.8) lib/abstract_controller/base.rb:189:in `process_action'
vendor/gems/ruby/1.9.1actionpack (4.1.8) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
vendor/gems/ruby/1.9.1activesupport (4.1.8) lib/active_support/callbacks.rb:82:in `run_callbacks'
vendor/gems/ruby/1.9.1actionpack (4.1.8) lib/abstract_controller/callbacks.rb:19:in `process_action'
vendor/gems/ruby/1.9.1actionpack (4.1.8) lib/abstract_controller/base.rb:136:in `process'
vendor/gems/ruby/1.9.1actionview (4.1.8) lib/action_view/rendering.rb:30:in `process'
vendor/gems/ruby/1.9.1actionmailer (4.1.8) lib/action_mailer/base.rb:580:in `block in process'
vendor/gems/ruby/1.9.1activesupport (4.1.8) lib/active_support/notifications.rb:159:in `block in instrument'
vendor/gems/ruby/1.9.1activesupport (4.1.8) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
vendor/gems/ruby/1.9.1activesupport (4.1.8) lib/active_support/notifications.rb:159:in `instrument'
vendor/gems/ruby/1.9.1actionmailer (4.1.8) lib/action_mailer/base.rb:577:in `process'
vendor/gems/ruby/1.9.1actionmailer (4.1.8) lib/action_mailer/base.rb:568:in `initialize'
vendor/gems/ruby/1.9.1actionmailer (4.1.8) lib/action_mailer/base.rb:551:in `new'
vendor/gems/ruby/1.9.1actionmailer (4.1.8) lib/action_mailer/base.rb:551:in `method_missing'
app/controllers/responses_controller.rb:23:in `block in create'
vendor/gems/ruby/1.9.1actionpack (4.1.8) lib/action_controller/metal/mime_responds.rb:433:in `call'
vendor/gems/ruby/1.9.1actionpack (4.1.8) lib/action_controller/metal/mime_responds.rb:433:in `retrieve_collector_from_mimes'
vendor/gems/ruby/1.9.1actionpack (4.1.8) lib/action_controller/metal/mime_responds.rb:256:in `respond_to'
app/controllers/responses_controller.rb:19:in `create'
嗨,基于@zwippie的输入,它成功了。只是对它做了一些修改
def create
@response = current_user.responses.new(response_params)
respond_to do |format|
if @response.save && @response.reload
Notification.confirm_approver(@response).deliver
flash[:notice] = "Response has been created successfully."
else
format.html { render action: 'new' }
end
end
end
您能否提供一个回溯,以确认这在控制器中发生?错误是由于
@response
为零。您的批准人方法在哪里?当我使用@response=response时,请发布日志以解决问题,或者pry
进行检查。在confirm\u approver方法中查找(response),一切正常。但是,我不想再次找到该对象(如果我是从控制器传递的),请检查您设置和获取的实例变量中是否存在拼写错误。在电子邮件方法中,检查参数以查看是否传递了nil或所需的值。如果调用@response.reload
,在调用@response.save之后重新加载?
def create
@response = current_user.responses.new(response_params)
respond_to do |format|
if @response.save && @response.reload
Notification.confirm_approver(@response).deliver
flash[:notice] = "Response has been created successfully."
else
format.html { render action: 'new' }
end
end
end