0

ネストされたアトリビュート フォームが更新されたら、電子メール通知を送信しようとしていますが、このエラーが発生し続けています。議会モデルがデータベース内のオブジェクトを更新する前に電子メール通知を送信しているため、nil を取得しているためだと思います。 Property.councils.email については、どんな助けでも大歓迎です。

プロパティメーラー

class PropertyMailer < ActionMailer::Base
  default from: "myemail@gmail.com"

  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

プロパティ.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

アップデート #

'Property/Build Controller'ネストされたコントローラーは、邪悪なウィザード フォーム gem を使用して、複数ステップのフォームを構築しています。

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 %>
4

2 に答える 2

0

あなたのproperty.councils呼び出しは空のセットを返します。

于 2013-08-23T15:47:52.480 に答える