Test Your Knowledge

Quiz

  1. Where would you put code to which you want all of your controllers to have access?

  2. How do the default routes decide which requests to send to your controller?

  3. What does the self.up method do in a migration?

  4. What three steps does the create method combine?

  5. How do you test to find out whether a submitted field is blank?

  6. How can you retrieve all of the values for a given field?

  7. How can you find a set of values that match a certain condition?

  8. How can you retrieve just the first item of a set?

Answers

  1. Code in the ApplicationController class, stored at app/controllers/application.rb, is available to all of the controllers in the project.

  2. The default routes assume that the controller name follows the first slash within the URL, that the controller action follows the second slash, and that the ID value follows the third slash. If there’s a dot (.) after the ID, then what follows the dot is considered the format requested.

  3. The self.up method is called when Rake runs a migration forward. It usually creates tables and fields.

  4. The create method creates a new object, sets its properties to those specified in the parameters, and saves it to the database.

  5. You can test to see whether something is blank using an if statement and the blank? method, as in:

    if @name.blank? then
      something to do if blank
    end
  6. To retrieve all values for a given object, use find(:all).

  7. To retrieve a set of values, use find(:all, :conditions => conditions).

  8. To get the first of a set, use find(:first). You may need to set an :order parameter to make sure that your understanding of “first” and Rails’ understanding of “first” are the same.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset