Reservation中的NoMethodError #index undefined method`strftime'为零:NilClass

时间:2014-11-06 22:44:13

标签: ruby-on-rails ruby nomethoderror

我刚刚在Windows上使用rails并运行rails 4。我几天来一直在处理这个错误。我认为我之前输入的数据可能有问题,所以我尝试在控制台中创建一个新的预订,看看到底出了什么问题,但到目前为止还没有运气。

这是我的用户模型:

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

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

    has_many :reservations
end

预订模式:

class Reservation < ActiveRecord::Base

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

end

预订索引:

<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>  

显示操作:

<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>

和预订控制器:     class ReservationsController&lt; 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!

1 个答案:

答案 0 :(得分:1)

reservation.start_time或reservation.end_time都是nil。

相关问题