Guest User

Untitled

a guest
Mar 6th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. ## calendars_controller.rb
  2.  
  3. class CalendarsController < ApplicationController
  4.  
  5. # GET /ui_prototype
  6. def index
  7. @dates = dates_of_month today.year, today.month
  8. end
  9.  
  10. protected
  11.  
  12. # Return an array of days from sunday to saturday of the given month
  13. # and year, includes previous and next months / years if needed
  14. def dates_of_month(year, month)
  15. first = Date.new(year, month, 1)
  16. last = Date.new(year, month + 1, 1) - 1
  17. start_date = first - first.wday
  18. end_date = last + (6 - last.wday)
  19.  
  20. dates = Array.new
  21. start_date.upto(end_date) do |d|
  22. dates.push(d)
  23. end
  24. return dates
  25. end
  26.  
  27. end
  28.  
  29. ## calendars_helper.rb
  30.  
  31. module CalendarsHelper
  32.  
  33. def date_dom_id(date)
  34. return "#{date.year}_#{date.month}_#{date.day}"
  35. end
  36.  
  37. def date_dom_class(date, week)
  38. return "calendar-datecell-week-5-day-6" if week == 5 and date.wday == 6
  39. return "calendar-datecell-week-5" if week == 5
  40. return "calendar-datecell-day-6" if date.wday == 6
  41. return "calendar-datecell"
  42. end
  43.  
  44. end
  45.  
  46. ## index.rhtml
  47.  
  48. <div id="calendar_container">
  49. <%
  50. week = 1
  51. @dates.each do |d|
  52. %>
  53. <div id="<%= date_dom_id(d) %>" class="<%= date_dom_class(d, week) %>">
  54. <div id="<%= "day_#{date_dom_id(d)}" %>" class="calendar-day"><%= d.day -%></div>
  55. </div>
  56. <%
  57. end
  58. %>
  59. </div>
  60.  
  61. ## error message
  62.  
  63. NoMethodError in Calendars#index
  64.  
  65. Showing app/views/calendars/index.rhtml where line #4 raised:
  66.  
  67. You have a nil object when you didn't expect it!
  68. You might have expected an instance of Array.
  69. The error occurred while evaluating nil.each
Add Comment
Please, Sign In to add comment