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 %> 
  • one
    Take the debugger, go in order. - D-side
  • Try to start at least to return the type to the date field. - cheops
  • @cheops Obviously, if I put all changes back, then everything will work) I would like to restructure the solution so that everything works with new data. - Maxim Cherevatov
  • I would be grateful if you tell me what this line does @date = params [: session_start_date]? Date.parse (params [: session_start_date]): Date.today - Maxim Cherevatov
  • @MaximCherevatov, can describe why did you need to change the type to string (there are no questions to the name of the parameter)? The line above checks if the GET / POST parameters have session_start_date, if there is, it forms the date from this parameter, if not, it takes the current date. - cheops

1 answer 1

I solved the problem by changing the following code:

 <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 %> <ul> <% @film_sessions.each do |film_session| %> <% if film_session.session_start_date == date.to_s %> <li><%= link_to film_session.session_name, film_session %></li> <% end %> <% end %> </ul> <% end %> <% end %> </div>