简体   繁体   中英

Railscasts Calendar - “undefined method beginning_of_month”, Rails 4

I want to be able to print a students homework on a calendar. I followed the railscasts video (revised one) on calendars precisely but I get this error:

undefined method `beginning_of_month' for nil:NilClass

Code:

homeworks_controller.rb

... def calendar
            @homeworks = Homework.all 
            @homeworks_by_date = @homeworks.group_by(&:date)
        end...

calendar.html.erb

<%= calendar @date do |date| %>
 <%= date.day %>
 <% if @homeworks_by_date[date] %>
 <ul>
  <% @homeworks_by_date[date].each do |homework| %>
  <li><%= link_to homework.subject, homework %></li>
  <% end %>
</ul>
  <% end %>
<% end %>

calendar_helper.rb

module CalendarHelper
  def calendar(date = Date.today, &block)
    Calendar.new(self, date, block).table
  end

class Calendar < Struct.new(:view, :date, :callback)
    HEADER = %w[Sunday Monday Tuesday Wednesday Thursday Friday Saturday]
    START_DAY = :sunday

    delegate :content_tag, to: :view

    def table
      content_tag :table, class: "calendar table table-bordered table-striped" do
        header + week_rows
      end
    end

    def header
      content_tag :tr do
        HEADER.map { |day| content_tag :th, day }.join.html_safe
      end
    end

    def week_rows
      weeks.map do |week|
        content_tag :tr do
          week.map { |day| day_cell(day) }.join.html_safe
        end
      end.join.html_safe
    end

    def day_cell(day)
      content_tag :td, view.capture(day, &callback), class: day_classes(day)
    end

    def day_classes(day)
      classes = []
      classes << "today" if day == Date.today
      classes << "not-month" if day.month != date.month
      classes.empty? ? nil : classes.join(" ")
    end

    def weeks
      first = date.beginning_of_month.beginning_of_week(START_DAY)
      last = date.end_of_month.end_of_week(START_DAY)
      (first..last).to_a.in_groups_of(7)
    end
end
end

The date column in homeworks table is a string - would this be the problem? Before I put in @date on the first line of the calendar view it was showing the calendar table but would not print the links to the records on the calendar. After I put in @date I get the above error. I'm using Rails 4 and the tutorial may be somewhat out of date.

Is there something else I need to do here that i'm overlooking? Thanks.

UPDATE:

I can see the calendar now after defining @date in the controller but it still won't print the links on the calendar.

def calendar
        @homeworks = Homework.all 
        @homeworks_by_date = @homeworks.group_by(&:date)
        @date = params[:date] ? Date.parse(params[:date]) : Date.today
    end

You're probably right that the problem is that the date column is a string and not a datetime.

beginning_of_month is a Rails helper but is only available on DateTime .

So either convert the column to a datetime or convert the String to DateTime using strptime .

DateTime.strptime(date, format_string)

where format_string is a string indicating what format the date is in. This is my favorite strftime / strptime reference: http://strftime.net/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM