nil:nilClass 错误的未定义方法"电子邮件"



我正试图在更新嵌套属性表单后发送电子邮件通知,但我一直遇到这个错误,我认为这是因为我在理事会模型更新数据库中的对象之前发送了电子邮件通知,因此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

更新#

"属性/构建控制器"嵌套控制器,我使用邪恶的向导表单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 %>

正如Marek Lipka所说,房地产委员会很可能会返回一个空散列,但这只是故事的一部分。我相信问题出在你的Property型号上,这条线是:

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

你最初的假设是正确的,我相信你是否试图在:councils关系有机会建立之前发送电子邮件。顾名思义,:reject_if方法用于在某些情况下抛出数据(我自己从未使用过它,所以想不出任何好的例子,但我相信有很多)。查看此处了解更多信息。

你绝对需要在对象持久化之前发送电子邮件吗?如果没有,也许另一种选择是使用ActiveRecord::回调方法之一,如after_commit,如:

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

您的property.councils调用返回空集。

最新更新