0

我有一个有很多预订的餐厅资源。我正在尝试格式化创建预订后发送的电子邮件。

这是我得到的错误:

undefined method `name' for nil:NilClass

我的 app/views/reservation_mailer/registration_confirmation.html.erb 中的代码

<p>Thanks for reserving a spot for <%= @restaurant.name %>!</p>

mailers 文件夹中的 reservation_mailer.rb:

class ReservationMailer < ActionMailer::Base
  default from: 'johnzw89@gmail.com'

  def registration_confirmation(restaurant, reservation)
    mail(:to => reservation.email, :subject => "Reservation")
    @restaurant = restaurant
    @reservation = reservation
  end

end

我在餐厅展示页面上有一个新的预订表格。这是reservations_controller:

  def create
    @restaurant = Restaurant.find(params[:restaurant_id])
    @reservation = @restaurant.reservations.new(params[:reservation])

    respond_to do |format|
      if @reservation.save
        ReservationMailer.registration_confirmation(@restaurant ,@reservation).deliver
        format.html { redirect_to @restaurant, notice: 'Reservation was successfully created.' }
        format.json { render json: @restaurant, status: :created, location: @restaurant }
      else
        format.html { render action: "new" }
        format.json { render json: @reservation.errors, status: :unprocessable_entity }
      end
    end
  end

我不确定为什么@restaurant 没有出现。我怀疑reservations_controller 中的@restaurant 变量没有传递给registration_confirmation 邮件方法,但我不确定......任何建议都非常感谢!

谢谢。

4

1 回答 1

0

你试过改变顺序吗?

def registration_confirmation(restaurant, reservation)
  @restaurant, @reservation = restaurant, reservation
  mail(:to => reservation.email, :subject => "Reservation")
end
于 2013-11-12T17:17:25.853 回答