You can find it in
the app/controllers/application.rb file, and it??™s useful to change this file to add things to all
controllers.
There isn??™t much to tell about controllers. Except for specifying which view should be rendered,
you can also provide data to the view. This is best done by setting instance variables to
the values you want the view to have. With some trickery, Rails copies all instance variables
from the current controller to the view that should be rendered. So, you can give a view some
much-needed data by doing it like this:
class FooController < ApplicationController
def index
@products = Product.find :all
@title = "All the Products in the World"
end
end
Another useful thing you can do with controllers is to use filters. You can specify that
some code should be executed before, after, or around every action. This allows you to achieve
authentication and authorization quite easily, but you can also add encryption, compression,
or many other things to your application in this way. Filters will be covered later in the book,
when we look at implementing authentication for Rails applications.
CHAPTER 3 ?– INTRODUCTION TO RAILS 24
Views
The view is the part that generates HTML (or XML, or JavaScript, or anything else really). In
Rails, the standard way of doing this templating is called ERb, and the standard file extension
is RHTML. There are also a few other ways of doing this, but ERb is the method I??™ll use in this
book.
Pages:
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83