Search code examples
ruby-on-railsrubynomethoderror

NoMethodError in Reservations#index undefined method `strftime' for nil:NilClass


I'm new to rails and running rails 4 on Windows. I've been dealing with this error for a few days now. I think something may be wrong with the previous data I entered so I tried creating a new reservation in the console to see what's going wrong, but no luck so far.

This is my User model:

class User < ActiveRecord::Base
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable

    attr_accessible :name, :email, :password, :remember_me

    has_many :reservations
end

Reservation model:

class Reservation < ActiveRecord::Base

    attr_accessible :user_id, :user_name, :start_time, :end_time, :project, :which_room

end

Reservation index:

<tbody>
  <% @reservations.each do |reservation| %>
    <tr>
      <td><%= current_user.name %></td> 
      <td><%= reservation.project %></td>
      <td><%= reservation.start_time.strftime("%I:%M %p")%></td>
      <td><%= reservation.end_time.strftime("%I:%M %p")%></td>
      <td><%= reservation.which_room %></td>
      <td><%= link_to 'Edit', edit_reservation_path(reservation) %></td>
      <td><%= link_to 'Destroy', reservation, method: :delete, data: { confirm: 'Are you sure?' } %></td>
    </tr>
  <% end %>
 </tbody>
  </table>
</div>  

Show action:

<p>
<strong>User:</strong>
<%= @user.name %>
</p>

<p>
<strong>Project:</strong>
<%= @reservation.project %>
</p>

<p>
<strong>Start time:</strong>
<%= @reservation.start_time %>
</p>

<p>
<strong>End time:</strong>
<%= @reservation.end_time %>
</p>

<p>
 <strong>Room number:</strong>
 <%= @reservation.which_room %>
</p>

And Reservations controller: class ReservationsController < ApplicationController before_action :set_reservation, only: [:show, :edit, :update, :destroy]

 def index
  @reservations = Reservation.all.order(which_room: :asc, start_time: :desc)
 end

 def show
 end

 def new
  @reservation = Reservation.new
end

def edit
end

def create
  @reservation = Reservation.new(reservation_params)

respond_to do |format|
  if @reservation.save
    format.html { redirect_to @reservation, notice: 'Reservation was successfully created.' }
    format.json { render action: 'show', status: :created, location: @reservation }
   else
      format.html { render action: 'new' }
      format.json { render json: @reservation.errors, status: :unprocessable_entity }
    end
  end
end

def update
  respond_to do |format|
    if @reservation.update(reservation_params)
      format.html { redirect_to @reservation, notice: 'Reservation was successfully updated.' }
      format.json { head :no_content }
   else
    format.html { render action: 'edit' }
    format.json { render json: @reservation.errors, status: :unprocessable_entity }
   end
  end
end

def destroy
  @reservation.destroy
  respond_to do |format|
    format.html { redirect_to reservations_url }
   format.json { head :no_content }
  end
end

private 
  def set_reservation
   @reservation = Reservation.find(params[:id])
 end

 def reservation_params
   params.require(:reservation).permit(:project, :start_time, :end_time, :which_room)
 end
end

TIA!


Solution

  • Either reservation.start_time or reservation.end_time is nil.