Prev | Current Page 420 | Next

Brad Ediger

"Advanced Rails"

The HTML tags are all closed for us,
but so is the each iterator we opened. This style of code has a learning curve, but it
has less duplication overall than comparable template systems such as ERb.
The stable version of Haml can be installed as a Rails plugin from http://svn.
hamptoncatlin.com/haml/tags/stable. As with the other template language plugins, it
registers a template handler, so all you need to do is install the plugin and start writing
views with an extension of .haml.
Incorporating Rails Components
As Rails is built up of many modular components, these components can be used
individually just as they can be used as a framework. Here we will see how the pieces
that make up Rails can be used in other Ruby code. We will walk through two modular
components of Rails, ActiveRecord and ActionMailer, and see how to use them
in standalone applications.
ActiveRecord
ActiveRecord is perhaps the easiest component to decouple from the rest of Rails, as
it fulfills a purpose (object-relational mapping) that can be used in many different
places. The basic procedure for loading ActiveRecord is simple; just define the connection,
and then create the classes that inherit from ActiveRecord::Base:
require 'rubygems'
require 'active_record'
ActiveRecord::Base.establish_connection(
# connection hash
)
class Something < ActiveRecord::Base # DB table: somethings
end
The establish_connection function takes a hash of parameters needed to set up the
connection.


Pages:
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432