我在 Rails 上学习 ruby,但在使用 aasm 回调和 actionmailer 时遇到了麻烦。我有一个酒店模型。这是一个代码:
class Hotel < ActiveRecord::Base
include AASM
scope :approved_hotels, -> { where(aasm_state: "approved") }
has_many :comments
belongs_to :user, :counter_cache => true
has_many :ratings
belongs_to :address
aasm do
state :pending, initial: true
state :approved
state :rejected
event :approve, :after => :send_email do
transitions from: :pending, to: :approved
end
event :reject, :after => :send_email do
transitions from: :pending, to: :rejected
end
end
def send_email
end
end
如您所见,当他添加的酒店状态发生更改时,用户必须收到电子邮件。这是我写的内容,但它不是解决方案,因为每次管理员更新酒店的“待定”状态时,用户都会收到电子邮件。
class HotelsController < ApplicationController
before_filter :authenticate_user!, except: [:index, :show, :top5hotels]
def update
@hotel = Hotel.find(params[:id])
if @hotel.aasm_state == "pending"
@hotel.aasm_state = params[:state]
UserMailer.changed_state_email(current_user, @hotel.name,
@hotel.aasm_state).deliver
end
if @hotel.update_attributes!(params[:hotel])
redirect_to admin_hotel_path(@hotel), notice: "Hotel was successfully updated."
else
render "edit"
end
end
end
所以我想我需要使用回调,但我不知道如何调用
UserMailer.changed_state_email(current_user, @hotel.name,
@hotel.aasm_state).deliver
从模型。我试过了
UserMailer.changed_state_email(User.find(:id), Hotel.find(:name),
Hotel.find(aasm_state)).deliver
但这不起作用。我真的没有选择并寻求任何帮助。谢谢!