0

Would like to find a way to show Klass.name, Programme.name, Topics.name and Timeslots.starts_at in the calendar. Currently only managed to show "Topic" instead of topics.name (see link) and Timeslots.starts_at. Attempted other methods but keep getting No Method Error. Appreciate suggestions on how to resolve this.

https://i.stack.imgur.com/sfHWX.jpg

Here is the views of the calendar.

app/views/timeslots/month.html.erb/_month_views.html.erb

<div id="timeslots">
  <h2 id="month">
    <%= link_to "<", date: @date.prev_month %>
    <%= @date.strftime("%B %Y") %>
    <%= link_to ">", date: @date.next_month %>
  </h2>
  <%= calendar @date do |date| %>
    <%= date.day %>
    <% if @timeslots_by_date[date] %>
      <ul>
        <% @timeslots_by_date[date].each do |timeslot| %>
          <li><%= link_to timeslot.topics.name %>) <%= link_to timeslot.starts_at %></li>          
        <% end %>
      </ul>
    <% end %>
  <% end %>
</div>
<br/>

app/controllers/timeslots_controller.rb

  def month
    @timeslots = Timeslot.all
    @timeslots_by_date = @timeslots.group_by {|i| i.starts_at.to_date}
    @date = params[:date] ? Date.parse(params[:date]) : Date.today 
  end  

Routes are

Rails.application.routes.draw do
  root to: 'posts#index'

  get '/register', to: 'users#new'
  get '/login', to: 'sessions#new'
  post '/login', to: 'sessions#create'
  get '/logout', to: 'sessions#destroy'

  get '/weekly_view', to: 'timeslots#week'
  get '/monthly_view', to: 'timeslots#month'

  resources :posts
  resources :users
  resources :categories

  resources :programmes do
    resources :topics    
  end

  resources :academies do
    resources :centres do
      resources :rooms
    end
  end

  resources :intakes do
    resources :klasses
  end

  resources :timeslots

end

The models are all has_many :through associations with belongs_to declared in all the 3rd models.

class Klass < ActiveRecord::Base

  belongs_to :intake
  has_many :user_klasses
  has_many :users, through: :user_klasses
  has_many :klass_programmes
  has_many :programmes, through: :klass_programmes
  has_many :klass_timeslots
  has_many :timeslots, through: :klass_timeslots  
  has_many :klass_centres
  has_many :centres, through: :klass_centres
  has_many :klass_rooms
  has_many :rooms, through: :klass_rooms  
  has_many :klass_topics
  has_many :topics, through: :klass_topics

end

class Programme < ActiveRecord::Base

  has_many :topics
  has_many :user_programmes
  has_many :users, through: :user_programmes
  has_many :programme_timeslots
  has_many :timeslots, through: :programme_timeslots
  has_many :programme_intakes
  has_many :intakes, through: :programme_intakes  
  has_many :klass_programmes
  has_many :klasses, through: :klass_programmes   

end

class Topic < ActiveRecord::Base
  belongs_to :programme
  has_many :user_topics
  has_many :users, through: :user_topics
  has_many :topic_intakes
  has_many :intakes, through: :topic_intakes
  has_many :topic_timeslots
  has_many :timeslots, through: :topic_timeslots
  has_many :klass_topics
  has_many :klasses, through: :klass_topics

end

class Timeslot < ActiveRecord::Base
  has_many :programme_timeslots
  has_many :programmes, through: :programme_timeslots
  has_many :room_timeslots
  has_many :rooms, through: :room_timeslots
  has_many :user_timeslots
  has_many :users, through: :user_timeslots
  has_many :topic_timeslots
  has_many :topics, through: :topic_timeslots
  has_many :centre_timeslots
  has_many :centres, through: :centre_timeslots  
  has_many :klass_timeslots
  has_many :klass, through: :klass_timeslots     

end

The calendar helper module is as below

app/helpers/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 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
chickensmitten
  • 477
  • 6
  • 16
  • 1
    I don't see full error message with a backtrace anywhere in your question. This kind of error can be caused by every single dot, I am not gona guess which one. – BroiSatse Nov 13 '14 at 12:44
  • Thanks for taking a look. Yes, there is no error message with the existing code. Perhaps the more accurate question is how do I change the code in the controller to be able to show the desired params in views. – chickensmitten Nov 13 '14 at 12:54

2 Answers2

1

topics is a collection, same goes for all the other relations on the model. You'd have to iterate over them to display them.

Kind of hard to offer more help without a proper strack trace, more direct question.

HTH,

Novae
  • 1,071
  • 8
  • 17
  • Noted, thanks for the observation. How can I provide the stack trace of the existing codes without raising an exception? Is it done through showing the server output by refreshing the calendar page? – chickensmitten Nov 13 '14 at 13:24
  • Yes, if you run the server locally you can use 'tail -n500 log/dev.log' to get the last 500 lines from the dev log in the console, alternately you can export it to a file: 'tail -n500 log/dev > out.part.log'. Or as you mentioned looking at the running server you can scroll through the output and extract the stracktrace there. – Novae Nov 13 '14 at 13:48
  • So, following http://stackoverflow.com/questions/11122233/get-current-stack-trace-in-ruby-without-raising-an-exception I put "puts caller" into timeslots_controller.rb def month ... end. Refreshed the page and I believe, it generated the stack trace as shown in gist. https://gist.github.com/chickensmitten/9d5b6a3efed946f6b4ab – chickensmitten Nov 14 '14 at 01:20
  • Managed to solve the problem, thanks for your inputs. Although it did not directly solve the issue, it helped me learn a lot. – chickensmitten Nov 15 '14 at 04:18
0

After some trying, I believe the issue is with the lack of fundamental understanding of Ruby on my part. Below is the code to solve the problem.

app/views/timeslots/month.html.erb/_month_views.html.erb

#skipped for brevity

        <% @timeslots_by_date[date].each do |timeslot| %>
          <li><%= link_to timeslot.starts_at %></li>
          <% timeslot.klass.each do |topic| %>
            <li><%= link_to topic.name %></li>
          <% end %>          
        <% end %>

#skipped for brevity

With it, now the timetable will show the Klass.name. The rest are ignored as the calendar will become unwieldy.

chickensmitten
  • 477
  • 6
  • 16