hi im just wondering if why the session cant find my reservation upon refresh and used it in a other controller
this is my applicationcontroller
helper :all # include all helpers, all the time
private
def current_reservation
@reservation ||= Reservation.find(session[:reservation_id]) if session[:reservation_id]
#last assigned value will be returned as default.
end
def create_reservation_session(id)
session[:reservation_id] = id
end
def destroy_reservation_session
session[:reservation_id] = nil
end
and im trying to us it here
def new
@book_reservation = BookReservation.new
end
def create
@reservation = current_reservation
@book_reservation=@reservation.build_book_reservation(params[:book_reservation])
if @book_reservation.save
#If success set session
create_reservation_session(@reservation.id)
#redirect_to root_url, :notice => "Successfully created book reservation."
else
render :action => 'new'
end
end
it raises undefined methodbuild_book_reservation’ for nil:NilClass` error
model/book_reservation.rb
belongs_to :reservation
model/reservation.rb
has_one :book_reservation
Cause of the error is you never set session but you tried to access it.In your application controller use
In your controller
Then whenever you need you can use current_reservation.