To Lecture Notes

IT 231 -- 7/23/12

 

Review Questions

  1. How do you create a DOS batch file?
    Ans: Create a .txt file and change the file extension to .bat.
     
  2. What does ERB mean?
    Ans: ERB means embedded Ruby (embedded within an HTML file) Place embedded Ruby code within the ERB delimiters
    ERB files have the file extension .html.erb
     
  3. What is the Rails helper function to
     

  4. You enter the following URL in a browser, but the corresponding page does not display properly. List some things that might be wrong?
    Ans:
    1. You might have saved the view or layout properly.
    2. You might not have started the server.
    3. You didn't wait long enough for the server to startup before viewing the page.
    4. You didn't convert the controller name to underscore notation before trying to use it as a URL.
    5. The controller name in the URL includes an uppercase letter.
    6. You don't have a proper end title tag.
    7. You are missing an ending ERB delimiter.

     
  5. What is the difference between these variables?
    Ans: title is a local variable only valid in the method where it is used; @title is an instance variable valid for the duration of the application. Use instance variables for embedded Ruby code placed on a webpage.
     
  6. You have already added a new controller named Info with its views and entered their content. However, you realize that you have forgotton to add the view called page1. What are the steps for adding a new view to the existing controller?
    Ans:
    1. Add a new view named in app/views/info/page1.html.erb
    2. Add a new method named page1 in app/controllers/info_controller.rb. The new page1 method might look like this:
        def page1
           @title = 'Page 1'
        end
        
    3. Add the following new route to app/config/routes.rb:
        get "info/page1"
        

     
  7. 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.
     
  8. What is wrong? Ans: 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.
     
  9. What are some possible datatypes for a Rails scaffold?
     
    Ans: :string, :text, :integer, :double, :decimal, :boolean, :date, :time, :datetime.
     
  10. Write a Rails generate scaffold statement to manage student objects with fields name, major, year, and gpa. Ans:
  11. What does rake db:migrate do?
    Ans: Uses the migration configuration file in db/migrations to create the database and ActiveRecord helper functions.
     
  12. In Rails, when do you use upper camel casing and when do you use underscore notation?
    Ans: You use upper camel casing for the project name and controller name in rails command prompt comments. Use camel casing also for names of Rails classes, such as the model name and the controller class name. You use underscore notation for pretty much everything else (e.g., controller folder name for views, controller file name, view file names, view names in rails commands, route specification, URLs).
     
  13. What is the name and location of the underlying database for a Rails project generated using a scaffold?
    Ans: db/development.sqlite3
     
  14. Which views does the Rails scaffold software create?
    Ans: index, show, new, view.
     
  15. 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.
     
  16. How can you tell if a Rails website has been created by using the generate scaffold command?
    Ans: Look in app/views. Then go into the folder with the name of the controller. If the view names are index, show, new, and edit, with another file named _form.html.erb, it is very likely that the project was created with the Rails scaffold software.
     
  17. 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.

 

Resizing an Image with MS Paint

  1. Right click on image name in MS Explorer
     
  2. Select Open with ... >> MS Paint.
     
  3. Resize Image.
     
  4. Set the horizontal and vertical boxes to a percentage less than 100 to shrink the image.
    (Reducing the side of an image by a factor of f reduces the area by about f squared.)
     
  5. Save the image.

 

Projects Created by the Rails Scaffold Software

 

Modifying the Dropdown Menus for Selecting a Date

 

Validations

 

Project 2

 

Moving Web Pages over the Internet

 

Running WEBrick on a Different Port

 

HTTP GET vs. POST

 

Representational State Transfer

 

Five Ways to Execute Ruby Code

  1. Using Interactive Ruby
     

  2. Executing Ruby commands from an .rb file
     
  3. Using Embedded Ruby (ERB)
     
  4. Running Ruby from the Rails Console Interactively
     

  5. Loading a Ruby Batch File from the Rails Console
     
    1. Enter a Rails project and create a ruby file named update.rb with JEdit
       
    2. Enter Ruby commands similar to what you entered in Section 4.
       
    3. Start the Rails Console as you did in Section 4.
       
    4. Load and run the Ruby file with
        load 'update.rb'
        

 

Project 3