Hello, I recently made a lesson calendar for my site. I have little idea how it works, so after the changes (namely, I changed the name of the date field to session_start_date and the data type from date to string), it stopped working. More precisely, the events in the corresponding dates are no longer displayed. Please tell me what the error is.
Calendar output to page
<h1>Calendar</h1> <div id="film_sessions"> <h2 id="month"> <%= link_to "<", session_start_date: @date.prev_month %> <%= @date.strftime("%B %Y") %> <%= link_to ">", session_start_date: @date.next_month %> </h2> <%= calendar @date do |date| %> <%= date.day %> <% if @film_sessions_by_date[date] %> <ul> <% @film_sessions_by_date[date].each do |film_session| %> <li><%= link_to film_session.session_name, film_session_path %></li> <% end %> </ul> <% end %> <% end %> </div> Method for it
def calendar @film_sessions = FilmSession.all @film_sessions_by_date = @film_sessions.group_by(&:session_start_date) @date = params[:session_start_date] ? Date.parse(params[:session_start_date]) : Date.today end And the formation of the calendar itself
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" 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 end Update
I found out that the problem is in this block.
<% if @film_sessions_by_date[date] %> <ul> <% @film_sessions_by_date[date].each do |film_session| %> <li><%= link_to film_session.session_name, film_session_path %></li> <% end %> </ul> <% end %> That is, the code is executed in the else
<% if @film_sessions_by_date[date] %> <ul> <% @film_sessions_by_date[date].each do |film_session| %> <li><%= link_to film_session.session_name, film_session %></li> <% end %> </ul> <% else %> <h1>Hello</h1> <% end %>