Test Your Knowledge
Quiz
Where would you put code to which you want all of your controllers to have access?
How do the default routes decide which requests to send to your controller?
What does the
self.upmethod do in a migration?What three steps does the
createmethod combine?How do you test to find out whether a submitted field is blank?
How can you retrieve all of the values for a given field?
How can you find a set of values that match a certain condition?
How can you retrieve just the first item of a set?
Answers
Code in the
ApplicationControllerclass, stored at app/controllers/application.rb, is available to all of the controllers in the project.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.The
self.upmethod is called when Rake runs a migration forward. It usually creates tables and fields.The
createmethod creates a new object, sets its properties to those specified in the parameters, and saves it to the database.You can test to see whether something is blank using an
ifstatement and theblank?method, as in:if @name.blank? then
something to do if blankendTo retrieve all values for a given object, use
find(:all).To retrieve a set of values, use
find(:all, :conditions =>conditions).To get the first of a set, use
find(:first). You may need ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access