0

我已经成功解决了问题 nil 不能被强制转换为 Fixnum 但我得到了另一个 :(

没有给出块(产量)

我知道问题出在 index.html 第 8 行,但我不知道如何解决

模块日历助手

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

class Calendar < Struct.new(:view, :date, :callback)

    HEADER = %w[Monday Mardi Mercredi jeudi Vendredi Samedi Dimanche]
    START_DAY = :monday

    delegate :content_tag, to: :view

    def table
        content_tag :table, class: "calendar" 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 << "notmonth" 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

结尾

控制器 calendars_controller.rb

def index
    @content_calendars = Calendar.all
    @content_calendars_by_dates = @content_calendars.group_by(&:published_on)
    @date = params[:date] ? Date.parse(params[:date]) : Date.today
  end


  def show
    @calendar = Calendar.find(params[:id])
  end

索引.html

 1     <%= calendar @date do |date| %>
 2       <%= date.day %>
 3  
 4    <!--# <%= debug(params) if Rails.env.development? %>
 5      --><% if @content_calendars_by_dates[date] %>
 6         <ul>
 7           
 8           <% @content_calendars_by_dates[date].each do |article| %>
 9                <li><%= link_to calendar.event, article %></li>
 10          <% end %>
 11         
 12         </ul>
 13       <% end %>
 14     <% end %>
4

1 回答 1

0

您可能想查看 Ryans 日历应用程序的想法https://github.com/railscasts/213-calendars-revised

于 2013-05-09T14:58:47.240 回答