Topic: Exam table help

Since my exams are looming I thought I'd make a timetable with Ruby on Rails to make an exam 'realisation' table. I've got the main structure off adding exams and I've styled them etc. even put in how long is left to each exam. However I want to put at the top out of the loop how long until the first exam, I then added a method to the controller being:

  def days_left
    first_exam = Subject.find(:first, :order => "day ASC")
    from_time = Time.now
    distance_of_time_in_words(from_time, first_exam, include_seconds = false)
  end

Is this the right way to go?
I got this far but have no idea how to execute it in the list view. Can someone push me in the right direction?

Thanks
Matt.

Matt
| My blog smile |

Re: Exam table help

lol - exams doesn't mean make something in Rails, its means revise wink

Although normally you would do something like:

#controller
time_left = Exam.find(:first, :order => "day ASC").days_left

#model
def days_left
    distance_of_time_in_words(Time.now, date_of_exam_field, include_seconds = false)
end


(that might not work, its more a concept than working code)

Last edited by shadow (2007-04-03 07:34:24)

Re: Exam table help

Couple of questions: what is the controller method called? And, how do I execute all this in the list view?

Last edited by Matt (2007-04-04 11:57:30)

Matt
| My blog smile |

Re: Exam table help

Ok I worked it out in one line of code, <%= distance_of_time_in_words(Time.now, Subject.find(:first, :order => "day ASC").day, include_seconds = false) %>

But I want to learn properly how to do it with MVC, which is the essence of Ruby, can anyone explain shadow's code for me?

Thanks
Matt

Matt
| My blog smile |

Re: Exam table help

I would use distance_of_time_in_words() in the view itself:

# controller
def list
   @exam = Subject.find(:first, :order => "day ASC")
end

# view
<table>
<tr>
<th>Exam Name</th>
<th>Exam Date</th>
</tr>
<tr>
<td><%= @exam.name %></td>
<td><%= distance_of_time_in_words(Time.now, @exam.days_left, include_seconds = FALSE %></td>
</tr>
<% end %>
</table>

Last edited by Michael Boutros (2007-04-06 20:48:17)