nil的未定义方法`name':NilClass Rails邮件程序视图不从控制器呈现变量

时间:2013-11-12 16:46:15

标签: ruby-on-rails ruby ruby-on-rails-3 actionmailer mailer

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

这是我得到的错误:

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邮件程序方法,但我不确定......任何建议真的很感激!

感谢。

1 个答案:

答案 0 :(得分:0)

您是否尝试过更改订单?

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