To Lecture Notes

IT 231 -- 9/28/15

Review Questions

  1. Create a Rails scaffold project. In the upper right corner of each view, show the date and time like this (look right):

9/28/15   1:38 PM

    At the bottom of the controller, define this private method
    def get_time
      t = Time.now
      h = t.hour
      min = t.min
      s = t.sec
      m = t.mon
      d = t.day
      y = t.year
      if h == 0
        h = 12
        am_pm = "AM"
      elsif h < 12
        am_pm = "AM"
      elsif h == 12
        am_pm = "PM"
      elsif h > 12
        h = h - 12
      end
      @time_string = "%d/%d/%d %d:%02d:%02d @s" %
        [h, min, s, m, d, y % 100, am_pm]
    end
    
    At the top of the controller, enter this line:
    before_action :get_time
    
    In the layout page application.html.erb above the yield statement, enter these lines:
    <p class="r"><%= @time_string %></p>
    
    In the controller specific style file add this line:
    p.r { text-align: right }
    
  1. What is a migration?
    Ans: The Rails migrations that we have seen modify a database table: (a) create a table, (b) add a column to a table, (c) remove a column from a table, (d) drop a table entirely.
  2. How can you fix a mistake that you made in a migration?
    Ans: If you catch the mistake before you run rake db:migrate, you can simply edit the migration file to correct the mistake. If you run rake db:migrate before you catch the mistake, run rake db:rollback, fix the mistake in the migration file, and run rake db:migrate again.

Project1 Submissions

Nested Routes