Rails 5 - 实例变量在电子邮件正文中不可用

Rails 5 - Instance variables not available in email body

我设置了一封电子邮件,以便在购买后立即发送,但在电子邮件的视图中没有显示变量。知道为什么吗?

user_mailer

def successful_payment(user, product)
    @user = user
    @product = product
    mail(:to => user.email,
        :subject => "Confirmation of payment #{product.name}")
end

payments_controller

def create
    @product = Product.find(params[:product_id])
    @user = current_user
    token = params[:stripeToken]
    # Create the charge on Stripe's servers - this will charge the user's card
    begin
      charge = Stripe::Charge.create(
        :amount => (@product.price * 100).to_i, # amount in cents, again
        :currency => "eur",
        :source => token,
        :description => params[:stripeEmail]
      )
      if charge.paid
        Order.create(product_id: @product.id, user_id: @user.id, total: @product.price)
        UserMailer.successful_payment(@user, @product).deliver_now
      end

模板

<table style="max-width:800px;margin:auto;padding:20px;box-shadow: 0px 1px 4px #000;margin-top:40px;background-color:#f7f7f4;">
  <tbody style="text-align:center;margin:auto;">
    <tr>
      <td><h2 style="color:#625F5F;font-family: 'Open Sans', sans-serif;">Hello <% @user.first_name %></h2></td>

不会显示用户名。

模板有误。语法是 <%= ... %> 来输出文本。 <% ... %> 只运行代码,不输出任何内容。

所以解决方案是:

<%= @user.first_name %>