0

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

しかし、それはうまくいきません。私は本当に選択肢がなく、助けを求めています。ありがとう!

更新 1: Amit Sharma に感謝します! 私はこれらの変更を行い、今取得しています

NoMethodError: undefined method `email' for nil:NilClass

changed_state_email() メソッドに渡すユーザー オブジェクトのように見えますが、理由はわかりません。ここに私のメーラーファイルもあります:

class UserMailer < ActionMailer::Base
  default from: "localhost"

  # Send email to user when hotels state change
  def changed_state_email(user, hotel_name, current_state)
    mail(to: user.email, subject: 'State of your hotel  '+hotel_name+'has been 
        changed to  '+current_state)
  end
end

puts "====#{self.inspect}" の結果は次のとおりです。

====#<Hotel id: nil, name: "CoolName", breakfast: nil, room_description: nil, price_for_room: 34, star_rating: 3, user_id: nil, address_id: nil, created_at: nil, updated_at: nil, average_rating: nil, photo_file_name: nil, photo_content_type: nil, photo_file_size: nil, photo_updated_at: nil, aasm_state: "approved">

F.====# F.====#

更新 2: ユーザー オブジェクトを返します。コンソールからの出力:

1.9.3-p551 :006 > h = Hotel.find(1)
  Hotel Load (0.4ms)  SELECT "hotels".* FROM "hotels" WHERE "hotels"."id" = ? LIMIT 1  [["id", 1]]
 => #<Hotel id: 1, name: "QWERTYUI", breakfast: nil, room_description: nil, price_for_room: 44, star_rating: 4, user_id: 2, address_id: nil, created_at: "2015-05-30 22:55:01", updated_at: "2015-05-30 22:55:01", average_rating: nil, photo_file_name: nil, photo_content_type: nil, photo_file_size: nil, photo_updated_at: nil, aasm_state: "pending"> 
1.9.3-p551 :007 > h
 => #<Hotel id: 1, name: "QWERTYUI", breakfast: nil, room_description: nil, price_for_room: 44, star_rating: 4, user_id: 2, address_id: nil, created_at: "2015-05-30 22:55:01", updated_at: "2015-05-30 22:55:01", average_rating: nil, photo_file_name: nil, photo_content_type: nil, photo_file_size: nil, photo_updated_at: nil, aasm_state: "pending"> 
1.9.3-p551 :008 > h.user
  User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 2]]
 => #<User id: 2, name: "qwerty", email: "qweqweqweqwe@qwe.com", encrypted_password: "$2a$10$FG5xXb/9wYLcdsCrfJtuDOTsslyY8p.m0qkbP4a5OEvJ...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, admin: false, created_at: "2015-05-30 22:54:14", updated_at: "2015-05-30 22:54:14", comments_count: 0, hotels_count: 1>
4

1 に答える 1