To Lecture Notes

IT 231 -- 9/20/12

 

Review Questions

  1. What is wrong?
    Ans: The word new should not be here and there should not be a space between p and 1 because actions cannot contain spaces. The scaffold software will think that the actions are index, p, and 1, but 1 is not a legal name for an action. The controller name is also missing between "controller" and "index".
     
  2. What are the names of the database tables generated by the following models in a scaffold-based application?
  3. What is wrong?
    Ans: The word new should be omitted; the words scaffold and generate should be reversed. Optionally, you can abbreviate generate to g. The model name People should be singular (Person), int should be string, text should be string and lower case because the gender is only one letter. Even if you want to use an integer, spell out the datatype as "integer", not "int". class is a reserved word, so use another word like course instead, string should be lower case.
     
  4. What are the names of the following items created by a Rails scaffold-based project using the model GroceryItem?
    1. resources  i Ans: grocery_items
    2. database table   Ans: grocery_items
    3. controller file   grocery_item_controller
    4. folder containing views   grocery_item
    5. URL of index view   http://localhost:3000/grocery_items

  5. What does
    do?
     
    Ans: It runs the latest migration file, which may create the database table, or modify the items in the database table.
     
  6. What is the name and location of the underlying database for a Rails project generated using a scaffold?
    Ans: db/development.sqlite3
     
  7. Which views does the Rails scaffold software create?
    Ans: index, show, new, edit.
     
  8. Explain the difference between yield and render.
    Ans: yield embeds a view within a layout page; render inserts code from another file into a view. A file containing code to be inserted with render must be preceded by a leading underscore character. For example, <%= render 'page' %> will insert the contents of the file _page.html.erb.
     
  9. What is the filename containg the code that the following render command inserts?
    Ans: _page.html.erb
     
  10. What does the Rails database console do for you? How can you invoke it?
    Ans: Use the Rails database console to issue SQL commands. Invoke the database console with
  11. How do you determine the controller and action to use to display A Rails view by looking at the Rails project?
    Ans: Look in app/views. You will find a folder named layouts and another folder. The other folder is the controller name. Go into this folder the .html.erb files in this folder correspond to the names of the views.
     
  12. How does a view know (a) if it is supposed to use a layout page and (b) which layout page to use?
    Ans: (a) A view indicates that it wants to use a layout page if there is either a file named <controller_name>.html.erb or application.html.erb in app/views/layouts. (b) If both files are present, the one with the controller name is used. Of course, the layout page must contain the ERB statement
    for the layout page to work.
     
  13. Create a scaffold-based project named Login with fields username, password, and expiration_date. Then do the following:
     
    1. Change the years displayed in the year dropdown box for specifying :expiration_date to start at 2012 and end at 2016.
      Ans: This answer is shown for the MovieReviewSite Example. Change Line 24 of _form.html.erb from
        <%= f.date_select :movie_release_date %>
        
      to
        <%= f.date_select :movie_release_date,
           :start_year => 1990, :end_year => 2012 %>
        

    2.  
    3. Write validations to do the following:
       
      1. The field :owner must be nonempty. Ans:
          validates :username, :presence => true
          

         
      2. The field :password must have a minimum length of 8 and maximum length of 20.Ans:
          validates :password, :length => { :in => 8..20 }
          

 

Helper Methods for URLs Generated by Scaffold Software

 

Project 2

 

Moving Web Pages over the Internet

 

Running WEBrick on a Different Port

 

HTTP GET vs. POST

 

Representational State Transfer