如何在 Rails 4-Action Mailer 视图中显示买家姓名

How to show the buyer name in Rails 4-Action Mailer Views

在我的应用程序中,买家购买演出后,买家和卖家的收件箱都会收到 e-mail 通知。 这是 user_mailer.rb:

的模型
class UserMailer < ActionMailer::Base
  default from: "example@gmail.com"

  def buyer(gig,email)
    @gig = gig
    @email = email
    mail(to: @email, subject: 'box delivery')
  end

  def seller(gig,email)
    @gig = gig
    @email = email
    mail(to: @email, subject: 'new box order')
  end
end

现在我可以在发送给买家的邮件模板中查看。

1.@gig.user.name = 它将显示卖家名称,谁拥有演出。

2.@gig.title and @gig.description = 演出的描述和标题

Question: How do I show the buyer name who bought the gig? I want to say something like "Dear buyer.name, the seller @gig.user.name(this one works) delivered your order.

千兆机型

  has_many :purchases
  has_many :buyers, through: :purchases
  has_many :sellers, through: :purchases
  belongs_to :user

用户模型

  has_many :purchases, foreign_key: 'buyer_id'
  has_many :gigs, through: :purchases, source: :buyer
  has_many :gigs, dependent: :destroy
  has_many :sales, foreign_key: 'seller_id', class_name: 'Purchase'

购买型号

class Purchase < ActiveRecord::Base
  belongs_to :gig
  belongs_to :buyer, class_name: 'User'
  belongs_to :seller, class_name: 'User'
end

千兆控制器

class GigsController < ApplicationController
  def downloadpage
    ActiveRecord::Base.transaction do
      if current_user.points >= @gig.pointsneeded 
        @purchase = current_user.purchases.create(gig: @gig, seller: @gig.user)
        if @purchase
          current_user.points -= @gig.pointsneeded
          @gig.user.points += @gig.pointsneeded
          current_user.save
          if @gig.user.save
            UserMailer.buyer(@gig,current_user).deliver
            UserMailer.seller(@gig,@gig.user.email, current_user.name).deliver
            render 'successful_download', locals:{link:@gig.boxlink}
          end
        end
      else
        redirect_to :back, notice: "You don't have enough points,upload a box and start getting them."
      end
    end
  end
end

如果您想通过电子邮件向买家发送有关订单的信息,为什么不以订单(采购)为重点?然后你可以使用

@gig = @purchase.gig
@buyer = @purchase.buyer

然后@buyer.name

这可能有效

def buyer(gig,user)
  @gig = gig
  @email = user.email
  @name = user.name
  mail(to: @email, subject: 'box delivery')
end



def seller(gig, email, name)
  @gig = gig
  @email = email
  @name = name
  mail(to: @email, subject: 'new box order')
end