我正在尝试在更新嵌套属性表单后发送电子邮件通知,但我一直遇到此错误,我认为这是因为我在委员会模型更新数据库中的对象之前发送电子邮件通知,因此得到一个 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
更新 #
'属性/构建控制器'嵌套控制器,正在使用邪恶的向导表单 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 %>