Rails 3.2:nil的未定义方法`strftime':NilClass

时间:2012-05-29 23:17:09

标签: ruby-on-rails ruby ruby-on-rails-3

在实施this日历gem时出现以下错误。

NoMethodError in Users#show

Showing /Users/nelsonkeating/Desktop/ReminDeal/app/views/users/show.html.erb where line #66 raised:

undefined method `strftime' for nil:NilClass
Extracted source (around line #66):

63:   </h2>
64:                 <%= calendar_for @friends, :year => @date.year, :month => @date.month do |calendar| %>
65:                  <%= calendar.head('mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun') %>
66:                   <%= calendar.day(:day_method => :dob) do |date, friends| %>
67:                    <%= date.day %>
68:                     <ul>
69:                         <% friends.each do |friend| %>

users_controller.rb

   class UsersController < ApplicationController
   before_filter :authenticate_user!

  def index
    authorize! :index, @user, :message => 'Not authorized as an administrator.'
    @users = User.paginate(:page => params[:page])

  end

  def show
    @user = User.find(params[:id])
    @friends = @user.friends.paginate(page: params[:page])
    @date = params[:month] ? Date.parse(params[:month]) : Date.today
   end

用户/ show.html.erb

<div id="calendar">
    <h2 id="month">
    <%= link_to "<", :month => (@date.beginning_of_month-1).strftime("%Y-%m") %>
    <%=h @date.strftime("%B %Y") %>
    <%= link_to ">", :month => (@date.end_of_month+1).strftime("%Y-%m") %>
  </h2>
                <%= calendar_for @friends, :year => @date.year, :month => @date.month do |calendar| %>
                 <%= calendar.head('mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun') %>
                  <%= calendar.day(:day_method => :dob) do |date, friends| %>
                   <%= date.day %>
                    <ul>
                        <% friends.each do |friend| %>
                        <li> <%= link_to h(friend.name), friends %></li>
                     <% end %>
                    </ul>
                <% end %>
            <% end %>
</div>

friend.rb

# == Schema Information
#
# Table name: friends
#
#  id         :integer         not null, primary key
#  name       :string(255)
#  dob        :date
#  gender     :string(255)
#  user_id    :integer
#  created_at :datetime        not null
#  updated_at :datetime        not null
#  int        :string(255)
#

class Friend < ActiveRecord::Base
  attr_accessible :name, :dob, :gender, :interests, :int, :interest_ids, :date
  belongs_to :user
  has_many :person_interests, :as => :person
  has_many :interests, :through => :person_interests
  serialize :ints

1 个答案:

答案 0 :(得分:1)

您的节目动作中未定义@date