私は、HABTM結合テーブルによって関連付けられたUserオブジェクトとOrgsオブジェクトを持っています。 Orgオブジェクトが更新され、Org.approved値がtrueに設定されている場合、ユーザーに電子メールを送信したいと思います。私はOrgに承認されたブール値を持っています。HABTM結合テーブル内のオブジェクトの更新時にメーラを送信する方法
私はそこにほとんどの方法を得たと思うが、私は実際に電子メールを送信するステップで助けが必要です。
はここに私のコード
class OrgMailer < ApplicationMailer
default from: '[email protected]'
def org_approved(user, org)
@user = user
@orgs = User.orgs.all
@url = 'http://example.com/login'
mail(to: @user.email, subject: 'Your listing has been approved.')
end
end
User.rbだ
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
has_and_belongs_to_many :orgs, join_table: :orgs_users
end
Org.rb
class Org < ApplicationRecord
has_and_belongs_to_many :users, join_table: :orgs_users
# after_update :send_approved_listing_email, only: [:update]
attachment :company_image
def send_approved_listing_email
OrgMailer.org_approved(i).deliver_now if org.approved === true
end
end
UPDATED:追加ORG_CONTROLLER
私のコードは以下のように編集されましたが、新しいエラーが発生しています:初期化されていない定数Org :: OrgsUser
これは、作成アクションで@ org.users < <の@user行にヒットしたときに発生します。
この行を削除すると、組織を作成できますが、正しく関連付けられていません。
org_controller.rb
class OrgsController < ApplicationController
before_action :set_org, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!
def index
@orgs = Org.all
@tags = ActsAsTaggableOn::Tag.all
end
def show
end
def new
@org = Org.new
end
def contest
end
def edit
end
def create
@user = current_user
@org = Org.new(org_params)
@org.users << @user
respond_to do |format|
if @org.save
format.html { redirect_to thankyou_path, notice: 'Your listing was successfully created. Our team will approve your listing after review.' }
format.json { render :show, status: :created, location: @org }
else
format.html { render :new }
format.json { render json: @org.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @org.update(org_params)
format.html { redirect_to @org, notice: 'Listing was successfully updated.' }
format.json { render :show, status: :ok, location: @org }
else
format.html { render :edit }
format.json { render json: @org.errors, status: :unprocessable_entity }
end
end
end
def destroy
@org.destroy
respond_to do |format|
format.html { redirect_to orgs_url, notice: 'Listing was successfully destroyed.' }
format.json { head :no_content }
end
end
def tagged
if params[:tag].present?
@orgs = Org.tagged_with(params[:tag])
else
@orgs = Org.postall
end
end
private
def set_org
@org = Org.find(params[:id])
end
def org_params
params.require(:org).permit(:twitter, :linkedin, :facebook, :name, :offer, :offercode, :url, :descrption, :category, :approved, :company_image, :tag_list => [])
end
end
私は私の管理パネルのために、アクティブな管理を使用し、任意選択ORGSを更新し、それらを承認するバッチアクションを持っています。私が欠けているのは、send_approved_listing_emailメソッドでorgを繰り返し、orgが承認されたときに各ユーザに電子メールを送る必要があるということです。
更新時には何も起こりませんので、私はこれを正しく行っていないと確信しています。私は何が欠けていますか?私はどのようにこれを書くべきですか?
'send_approved_listing_email'に引数エラーがある可能性があります。 'OrgMailer.org_approved'は'(user、org) 'を期待していますが、とにかく未定義のように見える' i'しか得られません。 – mysmallidea