2014-12-15 7 views
1

Я застрял в проблеме, в которой я хотел бы отправить почту после обратного вызова after_save в рельсах.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 
    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 

Моя проблема заключается в том, что я не получаю утверждающего. Ошибка при получении:

undefined method `approver' for nil:NilClass 

Когда я проверил запись в базе данных, apporver ('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' 
+1

Можете ли вы предоставить обратную трассировку, чтобы подтвердить это в контроллере? –

+0

Ошибка возникает, поскольку '@ response' равно нулю. также где ваш метод 'approver'? Пожалуйста, опубликуйте свой журнал, чтобы решить его или «pry», чтобы проверить –

+0

Когда я использую метод @response = Response.find (response) внутри метода confirm_approver, все работает. Но я не хочу снова искать объект (если я перехожу от контроллера) –

ответ

0

Привет на основе @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 
Смежные вопросы