I have a restaurant resource which has many reservations. I am trying to format the email that is sent after a reservation is created.
This is the error I get:
undefined method `name' for nil:NilClass
Code in my app/views/reservation_mailer/registration_confirmation.html.erb
<p>Thanks for reserving a spot for <%= @restaurant.name %>!</p>
The reservation_mailer.rb in the mailers folder:
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
I have a new reservations form in the restaurant show page. Here is the 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
I am not sure why @restaurant is not coming up. I suspect that the @restaurant variable from the reservations_controller is not passed to the registration_confirmation mailer method, but I am not sure... Any suggestions REALLY appreciated!
Thanks.
Have you tried changing order?
def registration_confirmation(restaurant, reservation)
@restaurant, @reservation = restaurant, reservation
mail(:to => reservation.email, :subject => "Reservation")
end