-1

I'd like to develop an app for schedule. Each user create their own schedule.

I'd like to display the data as followings;

schedule title (user name)

  day1(mm/dd,yyyy)
    09:00 Math
    11:00 Science
    Room name A

  day2(mm/dd,yyyy)
    10:00 Physics 
    13:00 Music
    Room name B

How can I display course information in the event model?

e.g. title (Math, Science), time (from, to)...

The following error appeared when I try to display the page.

NoMethodError (undefined method `event' for #<Room:0x007fb2149a1ac8>
Did you mean?  events
           events=):
  app/views/schedules/_schedule.html.erb:6:in `block in _app_views_schedules__schedule_html_erb___2809280481749604534_70201410929300'
  app/views/schedules/_schedule.html.erb:4:in `_app_views_schedules__schedule_html_erb___2809280481749604534_70201410929300'
  app/views/users/show.html.erb:11:in `_app_views_users_show_html_erb__1629176981125983348_70201561746560'

It would be appreciated if you could give me any suggestion.

user.rb

class User < ActiveRecord::Base
  has_many :schedlues
  has_many :rooms, through: :schedules
  ...

schedule.rb

class Schedule < ActiveRecord::Base
  belongs_to :user
  has_many :rooms
  ...

room.rb

class Room < ActiveRecord::Base
  belongs_to :schedlue
  has_many :events

event.rb

class Event < ActiveRecord::Base
  belongs_to :room

(Although I think 'room has_many events' is improper, I don't have any idea.)

schema

create_table "users", force: :cascade do |t|
    t.string   "name"
    t.string   "email"
    ...
end

create_table "schedules", force: :cascade do |t|
    t.string   "title"
    t.integer  "user_id"
    ...
end

create_table "rooms", force: :cascade do |t|
    t.date     "date"
    t.string   "room_name"
    t.integer  "schedule_id"
    ...
end

create_table "events", force: :cascade do |t|
  t.time     "from"
  t.time     "to"
  t.string   "title"
    ...
end

users_controller.rb

class UsersController < ApplicationController
  def show
    @user = User.find(params[:id])
    @schedules = @user.schedules.paginate(page: params[:page])
  end
  ...

show.html.erb

    <% if @user.schedules.any? %>
      <ol class="schedules">
        <%= render @schedules %>
      </ol>
      <%= will_paginate @schedules %>
    <% end %>

_schedule.html.erb

<li>
  <p>
  <% schedule.room.each do |a| %>
    <p><b>Day <%= a.day %></b></p>
    <% a.event.each do |e| %>  #error occur
      <% if e.title.any? %>
        <%= e.title %>
      <% end %>
    <% end %>
    <p>room: <%= a.room_name %></p>
  <% end %>
  </p>
</li>
SamuraiBlue
  • 851
  • 2
  • 17
  • 44

1 Answers1

0

As the error message states, Room does not have a method event. The reason is that in your model, you have has_many :events, which gives you an Association and not a single event. So try:

room.events.first 

or something like

room.events.each do |event|
  puts event
end

This should fix the error you're getting now.

EDIT I forgot to look at your template. The following line is wrong:

<% a.event.each do |e| %>

The solution is, as mentioned above:

<% a.events.each do |e| %>

It is by the way interesting that you didn't get an error on schedule.room. That is wrong as well, it must be schedule.rooms.

jdno
  • 4,104
  • 1
  • 22
  • 27
  • Thank you for your answer, @jdno. It works after using '<% a.events.each do |e| %>'. I also removed '<% if e.title.any? %>' because of error. – SamuraiBlue Mar 12 '16 at 18:48