2013-08-23 41 views
0

我試圖發送一個電子郵件通知一旦嵌套的屬性窗體已更新,但我一直碰到這個錯誤,我認爲這是因爲我發送電子郵件通知之前議會模型已更新數據庫中的對象,因此得到一個零的Property.councils.email,任何幫助將不勝感激。未定義的方法`電子郵件'爲零:NilClass錯誤

地產梅勒

class PropertyMailer < ActionMailer::Base 
    default from: "[email protected]" 

    def welcome_email(property, tenants, councils) 
    @property = property 
    @tenants = tenants 
    @councils = property.councils 
    mail(:to => property.councils.first.email, subject: 'Here are your property details') 
    end 
end 

property.rb

class Property < ActiveRecord::Base 

has_many :council_histories 
    accepts_nested_attributes_for :council_histories, :reject_if => :send_email 


    has_many :councils, through: :council_histories, :foreign_key => :council_id 
    accepts_nested_attributes_for :councils 

def send_email 
    if council_histories.where(council_id_changed?) 
     PropertyMailer.welcome_email(self, tenants, councils).deliver 
    end 
    end 
end 

更新#

'屬性/編譯器'嵌套控制器,現在用邪惡的嚮導形式寶石,建立一個多步驟的形式。

class Properties::BuildController < ApplicationController 
    include Wicked::Wizard 

    steps :tenant, :meter, :council, :confirmed 

    def show 
    @property = Property.find(params[:property_id]) 
    @tenants = @property.tenants.new(params[:tenant_id]) 
    @meter = @property.build_meter 
    @property.council_histories.build do |council_history| 
    @council = council_history.build_council 
    end 
    render_wizard 
    end 


    def update 
    @property = Property.find(params[:property_id]) 
    params[:property][:status] = step.to_s 
    params[:property][:status] = 'active' if step == steps.last 
    @property.update_attributes(params[:property]) 
    render_wizard @property 
    end 
end 

表單視圖

<%= simple_form_for @property, url: wizard_path, :method => 'put' do |f| %> 


    <%= f.simple_fields_for :council_histories do |builder| %> 
     <%= builder.input :property_id, as: :hidden, input_html: { value: @property.id } %> 
     <%= builder.input :council_id, :collection => Council.all %> 

     <%= builder.submit %> 
    <% end %> 
<% end %> 
+0

你在哪裏填充議會? – usha

+0

委員會是預先定義的,我正在使用上述簡單表單爲我的原始問題更新了一個房產委員會。 – cyclopse87

回答

1

正如Marek Lipka所說,property.councils很可能會返回一個空的散列,但這只是故事的一部分。我認爲這個問題是在你的Property模式,與這條線:

has_many :council_histories 
accepts_nested_attributes_for :council_histories, :reject_if => :send_email 
                ^^ 
                This is the problem here 

您的原始假設是正確的,我相信你試圖發送電子郵件的:councils關係有機會填充之前。正如其名稱所暗示的,:reject_if方法用於在某些情況下丟棄數據(我從來不使用它自己,所以不能想到任何好的例子,但我相信有很多)。 Check here for more info

您是否絕對需要發送電子郵件之前對象被持續?如果不是這樣,也許另一種選擇將要使用的ActiveRecord::Callback方法之一,如after_commit,就像這樣:

class Property < ActiveRecord::Base 
    after_commit :send_email 
    # Remainder of model code.... 
+1

發現隊友感謝您的詳細答案,我嘗試使用事後更新回調,這似乎並沒有工作,改爲提交後,完全感謝了很多工作。 – cyclopse87

0

property.councils調用返回空集。

+0

我這麼認爲,有沒有辦法調用當前屬性。試過了議會,但我得到同樣的錯誤。 – cyclopse87

+0

@ cyclopse87我認爲我們沒有足夠的信息來回答這個問題。你提到你相信問題是由於在議會模型更新之前發送的電子郵件,所以我們可能需要查看你的控制器代碼,以便我們可以檢查事件的順序。 –

+0

我已經使用嵌套控制器更新了我的問題,它使用了多步驟寶石。需要幫助請叫我? – cyclopse87

相關問題