Ruby on rails 未定义的方法`name';对于nil:NilClass Rails mailer视图,不呈现来自控制器的变量

Ruby on rails 未定义的方法`name';对于nil:NilClass Rails mailer视图,不呈现来自控制器的变量,ruby-on-rails,ruby,ruby-on-rails-3,actionmailer,mailer,Ruby On Rails,Ruby,Ruby On Rails 3,Actionmailer,Mailer,我有一个餐厅资源,有很多预订。我正在尝试格式化创建预订后发送的电子邮件 这是我得到的错误: undefined method `name' for nil:NilClass 我的应用程序/views/reservation\u mailer/registration\u confirmation.html.erb中的代码 <p>Thanks for reserving a spot for <%= @restaurant.name %>!</p> 我不知道

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

这是我得到的错误:

undefined method `name' for nil:NilClass
我的应用程序/views/reservation\u mailer/registration\u confirmation.html.erb中的代码

<p>Thanks for reserving a spot for <%= @restaurant.name %>!</p>
我不知道为什么@restaurant不会出现。我怀疑来自reservations\u控制器的@restaurant变量没有传递给registration\u confirmation mailer方法,但我不确定。。。任何建议都非常感谢


谢谢。

您试过更改订单吗

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

控制器操作无法找到具有该ID的餐厅。请使用已知值在控制台中进行检查,然后检查您在参数
餐厅中获得的值。在呈现/传递邮件之前,查找(0)
将引发错误(ActiveRecord::RecordNotFound)。
  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
def registration_confirmation(restaurant, reservation)
  @restaurant, @reservation = restaurant, reservation
  mail(:to => reservation.email, :subject => "Reservation")
end