2012-05-29 62 views
1

實現this日曆寶石時出現以下錯誤。Rails 3.2:未定義方法`strftime'爲零:NilClass

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

您的演出動作中沒有定義@date

+0

我將「@date = params [:month]?Date.parse(params [:month]):Date.today」移動到show動作並得到相同的確切錯誤。 – js111

+0

它可能與我的朋友表的設置有關嗎?我在 – js111

+0

之上添加了模式您試圖顯示的用戶是否有有效的「dob」值? –

相關問題