【发布时间】:2023-03-27 17:49:01
【问题描述】:
我正在学习 ruby on rails,但在使用 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
但这不起作用。 我真的没有选择并寻求任何帮助。 谢谢!
【问题讨论】:
标签: ruby-on-rails callback actionmailer aasm