Sunday 15 July 2012

ruby on rails 4 - How to use group_by date with datetime element to display on calendar by day? -



ruby on rails 4 - How to use group_by date with datetime element to display on calendar by day? -

i'm using datetime element on model reason can't show in calendar. i'm using code in controller , view this, css irrelevant think wont include that. i'm using hash it.

events controller:

def calendarpage @events = event.all @events_by_date = @events.group_by(&:edate) @date = params[:date] ? date.parse(params[:date]) : date.today end

calendarpageview:

<h1>my calendar</h1> <div id="events"> <h2 id="month"> <%= link_to "<", date: @date.prev_month %> <%= @date.strftime("%b %y") %> <%= link_to ">", date: @date.next_month %> </h2> <%= calendar @date |date| %> <%= date.day %> <% if @events_by_date[date] %><!-- populated?? --> <ul> <% @events_by_date[date].each |event| %> <li><%= link_to event.title, event%></li> <!-- doesn't show --> <% end %> </ul> <% end %> <% end %>

here's relevant database entries: table: events

id title ... edate "1" "test" ... "2014-10-06 20:38:00.000000" "2" "t2" ... "2014-10-07 20:40:00.000000"

this might useful, though don't think so: 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 mon tuesday quarta-feira th fri saturday] start_day = :sunday delegate :content_tag, to: :view def table content_tag :table, class: "calendar" header + week_rows end end def header content_tag :tr header.map { |day| content_tag :th, day }.join.html_safe end end def week_rows weeks.map |week| content_tag :tr 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) lastly = date.end_of_month.end_of_week(start_day) (first..last).to_a.in_groups_of(7) end end end

the calendar displays fine, , know controller code called. none of events displayed @ all. 6th , 7th don't displayed.

now, here's i'm thinking. followed tutorial create , i'm thinking in tutorial edate element :date element not :datetime element have it, create difference? if how right difference in variable type (the time info)? if problem have alter group_by line in event controller?

one other observation have appended code onto bottom of view page:

<% @events_by_date["7"].each |event| %> <li><%= link_to event.title, event%></li> <% end %>

and got error:

undefined method `each' nil:nilclass

this may because don't understand how hashtables work, telling me @events_by_date isn't beingness populated in controller :(

what do?

datetime ruby-on-rails-4 hashtable

No comments:

Post a Comment