В моем приложении после того, как покупатель купил концерт, покупатель и продавец получат электронное письмо в свой почтовый ящик, сообщая им об этом. Это модель для user_mailer.rb
:Как показать имя покупателя в Rails 4-Action Mailer Views
class UserMailer < ActionMailer::Base
default from: "[email protected]"
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
= описание и название концерта
Вопрос: Как показать имя покупателя, который купил этот концерт? Я хочу сказать, что-то вроде «Дорогой buyer.name, продавец @ gig.user.name (это работает) доставлен ваш заказ.
Gig модель
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
контроллер Gig
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
Если у вас есть система Войти на месте будет не покупатель будет current_user? – ChrisBarthol
Да, и я попробовал current_user.name, но он дает ошибку при отправке в папку «Входящие», потому что current_user недоступен для модели почтовой программы –
. Добавьте свой код контроллера. –