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.up
method do in a migration?
What three steps does the create
method 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?
Code in the ApplicationController
class, 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.up
method is called when Rake runs a migration forward.
It usually creates tables and fields.
The create
method 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
if
statement and
the blank?
method, as
in:
if @name.blank? then
something to do if blank
end
To retrieve a set of values, use find(:all, :conditions =>
conditions
).
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.