To Lecture Notes

IT 231 -- 9/25/12

 

Review Questions

  1. What is a resource in a scaffold-based project? What is a resource-based route?
    Ans: A resource is an item that the controller is managing. The name of a resource is usually (always for us) the same as the name of the model. A resource based route is created in the file config/routes.rb with this statement
  2. How do you undo the actions executed by
  3. What does CRUD mean?
    Ans: It is an acronym for the usual database operations: Create, Read, Update, and Destroy.
     
  4. Write a hyperlink with text "To Students Index", whose target is the index view of the GroceryItems controller:
  5. What is the seven layer model?
    Ans: It is a system for organizing network software into seven layers, where each layer is responsable for different aspects of moving data over the internet. The names of the seven layers are Layer 7: Application, Layer 6: Presentation, Layer 5: Session, Layer 4: Transport, Layer 3: Network, Layer 2: Data Link, Layer 1: Physical. An acronym for remembering these layers is All people seem to need data processing.
     
  6. What are some important HTTP commands?
    Ans: GET, HEAD, POST, PUT, DELETE.
     
  7. Explain the difference between the HTTP GET and HTTP POST commands.
    Ans: GET requests the server to send a webpage to the client. POST submits a webpage back to the server. GET can send information to the server by adding this information to the end of the URL.
     
  8. What does REST mean for Rails?
    Ans: Representational State Transfer. In more detail, REST for Rails means these three things:
    1. Client-server architecture.
    2. Neither the client or the server remembers the information on previously transmitted web pages. (To retain information from previous pages, it must be stored in a database table.)
    3. CRUD operations are implemented with the HTTP GET, POST, PUT, and DELETE commands.

 

Six Ways to Execute Ruby Code

  1. Using Interactive Ruby
     
    In a Command Prompt Window, type irb for Interactive Ruby. Then enter the Ruby statements as shown:
    Enter ^C to exit irb.
     
    The items to print in a print statement are listed sequentially, separated by commas. To go to a new line, an explicit new line character \n must be used.
     
    Although normally a string can use either single or double quotes, for a \n to be interpreted as a new line character, double quotes must be used.
     
    In single quotes, \n is interpreted as a backslash and a letter n. Only in double quotes is \n interpreted as a new line character.
     
    The Ruby method puts prints a single string. If multiple items are to be displayed with puts, they must be concatenated together in a single string using the concatenation operator +.
  2. Executing Ruby commands from an .rb file
     
    Use JEdit to create a Ruby file named ex1.rb that contains the following Ruby statements:
    Save these commands and exit the Command Line Editor.
     
    From a Command Prompt window, enter rb ex1.rb. You should get output as shown here:
  3. Using Embedded Ruby (ERB)
     
    Create a Rails project named TestRuby.
     
    Create a controller named ex1 with one page named index.
     
    Add the following Ruby statements to the controller index method:
    Replace the HTML code in index.html.erb with this source code:
  4. Running Ruby from the Rails Console Interactively
     
    Enter a rails project created by the scaffold software, for example the Census project (Example 8).
    Now you can enter Ruby commands as you did with irb. The advantage of this context is that all of the classes defined by the scaffold software are available.
     
    Now enter the following commands. Now shut down the console with ^C and start the server and check the database with your browser to see whether the changes actually made it to the database.
  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'
        
  6. Populating a Ruby Batch File using a Seed File
     
    1. Create a scaffold-based Rails project with model name GroceryItem and fields code (integer), description (text), price (float), and on_sale (boolean).
       
    2. Copy and paste the Ruby code in this seed code at the bottom of the seed file db/seeds.rb.
       
    3. Run the seed file at the command prompt with
        rake db:seed
        

 

Ruby Details