【问题标题】:NoMethodError in Reservations#index undefined method `strftime' for nil:NilClassNoMethodError in Reservations#index undefined method `strftime' for nil:NilClass
【发布时间】:2015-01-03 15:07:57
【问题描述】:

我是 Rails 新手,在 Windows 上运行 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>

和预订控制器: 类 ReservationsController

 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!

【问题讨论】:

    标签: ruby-on-rails ruby nomethoderror


    【解决方案1】:

    reservation.start_time 或 reservation.end_time 都为零。

    【讨论】:

    • 对于特定条目?我应该从控制台中删除所有当前条目并刷新吗?
    • 只需添加一个if 以检查日期是否存在于您的视图中,或者您是否想要删除所有日期,在rails 控制台中执行 Reservation.where('start_time IS NULL OR end_time IS NULL') .destroy_all
    猜你喜欢
    • 2022-11-18
    • 2012-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-29
    • 1970-01-01
    • 2015-03-19
    • 2016-08-13
    相关资源
    最近更新 更多