nil的未定义方法`strftime`:chrome&中的NilClass野生动物园,但不是火狐

时间:2014-12-22 23:17:49

标签: ruby-on-rails ruby

我在我的index.html中为nil:NilClass接收未定义的方法strftime。由于某种原因,这个错误存在于chrome和safari中,但不存在于firefox中。任何帮助,将不胜感激。我的模型,控制器和视图中有以下内容:

user_meetup.rb

def self.request(start_date, start_time, location, description)
  transaction do            
    start_time = [start_date, start_time].join(' ').to_time unless start_time.blank?

    meetup1 = UserMeetup.create!(start_time: start_time, location: location, description: description)
    meetup2 = UserMeetup.create!(start_time: start_time, location: location, description: description)
  end
end

def time
  start_time.strftime("%l:%M %P")
end

def date
  start_time.strftime("%B %e,  %Y")
end

user_meetups_controller.rb:

class UserMeetupsController < ApplicationController
  before_filter :authenticate_user!
  respond_to :html, :js

  def index
    @user_meetups = current_user.user_meetups
  end

在我看来,我有以下内容,meetup.date和meetup.time是我得到NoM​​ethodError:nil的未定义方法strftime:NilClass:

<% if current_user.user_meetups.each do |meetup| %>
  <% if meetup.accepted? %>
    <div class="accepted">
      <span class="start-date">scheduled for <%= meetup.date  %> at <%= meetup.time %> at <%= meetup.location %></span>

2 个答案:

答案 0 :(得分:0)

您需要在视图代码中使用@user_meetups来访问此数据,Rails始终将实例变量从控制器传递到视图。

小心!

答案 1 :(得分:0)

以下这行错误

 def self.request(start_date,...) do

    start_time = [start_date, start_time].join(' ').to_time unless start_time.blank? # <= This line
    ...
  end

当start_time为nil""时,它将不会被分配。

由于变量 start_time为空,您可能正在寻找:         start_time = [start_date,start_time] .join(&#39;&#39;)。to_time

通常,我会避免使用属性名称(此处使用[start_date, ...])来表示局部变量(用作start_date =