Rails 101 2007
Rails 101 2007
Rails 101 2007
101
Presentation slides for a five day
introductory course
by Peter Marklund
1
This work is licensed under a Creative Commons Attribution 3.0 United States License (http://
creativecommons.org/licenses/by/3.0/us).
The material was developed by Peter Marklund for a five day Ruby on Rails course in June 2007
in Sweden. Please direct any feedback to the author at [email protected] or via http://
marklunds.com.
2
Agenda
Monday Tuesday Wednesday Thursday Friday
Installation Migrations ActiveRecord Routing Exercises
Philosophy ActiveRecord, Associations, REST Working on
and MVC ActionController, Validations, and
ActionMailer your app
Start an app and ActionView Callbacks
Basics ActionView Plugins
Files,
Forms ActiveSupport
generators, and
Filters Rails
scripts 2.0
Caching
3
rails
introduction
4
Kung-Fu?
5
Installation
InstantRails - One-Click install on Windows
Locomotive - One-Click install on OS X
Better options for Mac: MacPorts and installing from
source
6
Rails background
7
Rails Elevator Pitch
8
Rails Philosophy and
Strengths
Ruby - less and more readable code, shorter development times, simple but
powerful, no compilation cycle
Almost everything in Rails is Ruby code (SQL and JavaScript are abstracted)
9
MVC Request Cycle
10
MVC Request Cycle
1. A request is made from the browser to the URL
http://localhost:3000/users/show/1.
2. A Rails server running locally on port 3000 receives the request and the
dispatcher is invoked. Rails tries to find a route (in the file config/routes.rb) to
match the URI /users/show/1. The default route :controller/:action/:id
matches the URI and the dispatcher instantiates the users controller and
invokes its show method with the :id parameter set to 1.
3. The show method in the users controller fetches the user model object from
the database with id equal to 1.
4. The show method renders its view (the file show.rhtml) which generates an
HTML page with the user info from the user model object.
5. Rails sends the generated HTML from the view back to the browser.
11
building a Demo
Application
Installing plugins with script/plugin install
Code generation with script/generate model|scaffold
Migrations
The MVC request cycle
Rails command line - script/console
Testing with rake
Routes - :controller/:action/:id
12
Directory Structure
app
controllers
helpers
models
views
layouts
config
environment.rb
routes.rb
db
database.yml
migrations
lib
log
public
script
test
vendor
plugins
rails
13
Rake
14
Useful Rake Tasks
db:migrate
db:sessions:create
doc:app
doc:rails
log:clear
rails:freeze:gems
rails:freeze:edge
rails:update
:stats
15
script/*
about
breakpointer
console
generate
plugin
runner
server
script/igoruby/clear_sessions
16
Environments
17
config/environment.rb
log level
how sessions are stored
schema format
pluralization rules
Load custom libraries that you need here
18
Debugging
raise @object.inspect
raise User.instance_methods(false).sort.join(", ")
<%= debug @object %>
script/console
The error log
script/breakpointer
19
Ruby
20
I always thought Smalltalk would beat Java. I just
didnt know it would be called Ruby when it did
- Kent Beck
21
The Ruby Language
Generic, interpreted, reflective, with garbage
collection
Optimized for people rather than computers
More powerful than Perl, more object oriented than
Python
Everything is an object. There are no primitive types.
Strong dynamic typing
22
Everything in Ruby is:
23
Ruby is Line Oriented
Statements are separated by line breaks
You can put several statements on one line if you
separate them by semicolon
For long statements you can escape the line break
with backslash
After an operator, comma, or the dot in a method
invocation you can have a line break and Ruby will
know that the statement continues on the next line
You can have line breaks in strings
24
Defining a class and
Instantiating an
Object
class Person
# Constructor - invoked by Person.new
def initialize(name)
# Instance variables start with @
@name = name
end
andreas = Person.new(Andreas)
andreas.say_hi
25
Class Inheritance
# Programmer is a Person and extends it
# with additional characteristics
class Programmer < Person
def initialize(name, favorite_ide)
super(name)
@favorite_ide = favorite_ide
end
26
Getter- and Settter
Methods
class Person
def initialize(name)
self.name = name
end
def name
@name
end
def name=(name)
@name = name
end
end
person = Person.new("Andreas")
puts person.name
person.name = "David"
puts person.name
27
attr_accessor
class Person
attr_accessor :name
def initialize(name)
self.name = name
end
end
person = Person.new("Andreas")
puts person.name
person.name = "David"
puts person.name
28
Variable/Method
Ambiguity Gotcha
class Person
attr_writer :paid
def initialize
@paid = false
end
def paid?
@paid
end
def make_payment
... code that makes payment ...
paid = true # Indicate payment is done
end
end
person = Person.new
person.make_payment
puts person.paid?
29
Methods
When invoking a method argument parenthesis are optional
30
Defining Class
Methods
class Person
def self.class_method
puts class method invoked
end
31
Singleton Classes and
Methods
# Every object has two classes: the class of which
# it is an instance, and a singleton class. Methods
# of the singleton class are called singleton methods
# and can only be invoked on that particular object.
andreas = Person.new(Andreas)
def andreas.andreas_says_hi
Andreas says hi
end
andreas.andreas_says_hi
32
Naming Conventions
MyClass
method_name, dangerous_method!,
question_method?, setter_method=
MY_CONSTANT = 3.14
local_variable = 3.14
@instance_variable
@@class_variable
$global_variable
33
Boolean expressions
All objects evaluate to true except false and nil
false and true are the only instances of FalseClass and TrueClass
34
assignment
a, b = b, a # swapping values
a = 1; b = 1
a=b=1
a += 1 # a = a + 1
a, b = [1, 2]
a = b || c
a ||= b
35
Idiom: Assignment with
Boolean Expression
# Overly verbose:
user_id = nil
if comments
if comments.first
if comments.first.user
user_id = comments.first.user.id
end
end
end
# Idiomatic:
user_id = comments && comments.first &&
comments.first.user && comments.first.user.id
36
modules
# Mixins - instead of multiple inheritance
module FullName
def full_name
"#{first_name} #{last_name}"
end
end
class Person
include FullName
end
Person.new("Peter", "Marklund").full_name
MyApp::Person.new("Peter Marklund")
37
Modules vs Classes
38
Everything is an
object
2 + 2 is equivalent to 2+(2) and 2.send(:+, 2)
2.hours.ago
2.class # => Fixnum
2.class.methods - Object.methods
andreas.capitalize
39
Constants
40
Introspection
andreas = Person.new(Andreas)
andreas.inspect
Person.instance_methods(false)
puts Kernel.methods.join(\n)
41
Arithmetic and
Conversions
2.class == Fixnum
Fixnum.superclass == Integer
Integer.superclass == Numeric
3.0.class == Float
Float.superclass == Numeric
2/3 == 0
2/3.0 # => 0.6666667
2 + 3.0 == 5.0
2.to_i + 3.0.to_f == 5.0
10000000000.class == Bignum
Bignum.superclass == Integer
42
String class
ruby.upcase + + rails.capitalize
<<-END
A here
document at #{Time.now}
END
43
array class
a = [Ruby, 99, 3.14]
a[1] == 99
a << Rails
[C, Java, Ruby] == %w{C Java Ruby}
[1, 2, 3].map { |x| x**2 }.join(, )
[1, 2, 3].select { |x| x % 2 == 0 }
[1, 2, 3].reject { |x| x < 3 }
[1, 2, 3].inject { |sum, i| sum + i }
44
hash class
45
Symbols
# Symbols start with a colon
:action.class == Symbol
:action.to_s == action
:action == action.to_sym
46
More About methods
47
Range Class
# Two dots is inclusive, i.e. 1 to 5
(1..5).each { |x| puts x**2 }
(1..3).to_a == [1, 2, 3]
48
Structs
puts rating.name
puts rating.ratings
49
if, unless and the ?
Operator
message = if count > 10
Try again
elsif tries == 3
You lose
else
Enter command
end
50
Iterators: while, until,
and for. Keywords: break
and next
while count < 100
puts count
count += 1
end
51
case
case x
when 0
when 1, 2..5
puts "Second branch"
when 6..10
puts "Third branch"
when *[11, 12]
puts Fourth branch
when String: puts Fifth branch
when /\d+\.\d+/
puts Sixth branch
when x.downcase == peter
puts Seventh branch
else
puts "Eight branch"
end
52
blocks, closures, and
proc objects
def invoke_block
puts "before block"
yield 5
puts "after block"
end
name = "Ruby"
invoke_block { |n| puts "In block with #{name}, received #{n}"}
53
blocks - usage
examples
# Iteration
[1, 2, 3].each {|item| puts item }
# Resource Management
file_contents = open(file_name) { |f| f.read }
# Callbacks
widget.on_button_press do
puts Got button press
end
54
common string
operations
.blank? == true
my_string.each_with_index { |line, i| puts #{i}: #{line} }
abc.scan(/./).each { |char| puts char }
we split words.split.join(, )
strip space .strip
sprintf(value of %s is %.2f, PI, 3.1416)
I Go Ruby[2, 2] == I Go Ruby[2..3] == Go
55
Using the dup Method
on Method Arguments
# Methods that change their receiver end with an exclamation mark by convention.
# If you need to invoke an exclamation mark method on a method argument and you want
# to avoid the object from being changed, you can duplicate the object first
# with the Object#dup method. Core classes such as String, Hash, and Array all have
# meaningful implementations of the dup method. Here is an example from Rails:
class ActiveRecord::Base
...
def attributes=(new_attributes)
return if new_attributes.nil?
attributes = new_attributes.dup # duplicate argument to avoid changing it
attributes.stringify_keys! # modify the duplicated object
multi_parameter_attributes = []
remove_attributes_protected_from_mass_assignment(attributes).each do |k, v|
k.include?("(") ? multi_parameter_attributes << [ k, v ] : send(k + "=", v)
end
assign_multiparameter_attributes(multi_parameter_attributes)
end
end
56
regular expressions
puts matches if Ruby =~ /^(ruby|python)$/i
Go\nRuby =~ /Go\s+(\w+)/m; $1 == Ruby
I Go Ruby =~ /go/i; $& == Go; $` == I ; $ == Ruby
pattern = .; Regexp.new(Regexp.escape(pattern))
I Go Ruby[/(go)/i, 1] == Go
I Go Ruby.gsub(%r{Ruby}, \0 or I go bananas)
"I Go Ruby".gsub(/ruby/i) { |lang| lang.upcase }
line = I Go Ruby
m, who, verb, what = *line.match(/^(\w+)\s+(\w+)\s+(\w+)$/)
# \s, \d, [0-9], \w - space, digit, and word character classes
# ?, *, +, {m, n}, {m,}, {m} - repetition
57
exceptions
begin
raise(ArgumentError, No file_name provided) if !file_name
content = load_blog_data(file_name)
raise Content is nil if !content
rescue BlogDataNotFound
STDERR.puts "File #{file_name} not found"
rescue BlogDataConnectError
@connect_tries ||= 1
@connect_tries += 1
retry if @connect_tries < 3
STDERR.puts "Invalid blog data in #{file_name}"
rescue Exception => exc
STDERR.puts "Error loading #{file_name}: #{exc.message}"
raise
end
58
invoking external
programs
system(ls -l)
puts $?.exitstatus if !$?.success?
puts `ls -l`
59
Ruby Scripts With RDoc
and Option Parsing
#!/usr/bin/env ruby
# == Synopsis
# This script takes photographs living locally on my desktop or laptop
# and publishes them to my homepage at http://marklunds.com.
#
# == Usage
#
# Copy config file publish-photos.yml.template to publish-photos.yml
# and edit as appropriate.
#
# ruby publish-photos [ -h | --help ] <photo_dir1> ... <photo_dirN>
opts = OptionParser.new
opts.on("-h", "--help") { RDoc::usage('usage') }
opts.on("-q", "--quiet") { Log::Logger.verbose = false }
opts.parse!(ARGV) rescue RDoc::usage('usage')
Photos::Publisher(ARGV)
60
Ruby on the Command
Line
# Query and replace
ruby -pi.bak -e "gsub(/Perl/, 'Ruby')" *.txt
# Grep
ruby -n -e "print if /Ruby/" *.txt
ruby -e "puts ARGF.grep(/Ruby/)" *.txt
61
open class definitions
and Method Aliasing
class Peter
def say_hi
puts "Hi"
end
end
class Peter
alias_method :say_hi_orig, :say_hi
def say_hi
puts "Before say hi"
say_hi_orig
puts "After say hi"
end
end
62
Core Classes are Also
Open
class Integer
def even?
(self % 2) == 0
end
end
63
method_missing: A VCR
class VCR
def initialize
@messages = []
end
def play_back_to(obj)
@messages.each do |method, args, block|
obj.send(method, *args, &block)
end
end
end
64
Using the VCR
vcr = VCR.new
vcr.gsub! /Ruby/, "Crazy"
vcr.upcase!
object = "I Go Ruby"
vcr.play_back_to(object)
puts object
65
const_missing - for
Auto Loading Classes
def Object.const_missing(name)
@looked_for ||= {}
str_name = name.to_s
raise "Class not found: #{name}" if @looked_for[str_name]
@looked_for[str_name] = 1
file = str_name.downcase
require file
klass = const_get(name)
return klass if klass
raise "Class not found: #{name}"
end
66
eval, binding
a = 1
evaluate_code(puts a, binding) # => 1
67
instance_eval
andreas = Person.new(Andreas)
name = andreas.instance_eval { @name }
68
class_eval/
module_eval
class Person
def add_method(method)
class_eval %Q{
def #{method}
puts "method #{method} invoked"
end
}
end
add_method(:say_hi)
end
person = Person.new.say_hi
69
define_method
class Array
{:second => 1, :third => 2}.each do |method,element|
define_method(method) do
self[element]
end
end
end
array = %w(A B C)
puts array.first
puts array.second
puts array.third
70
Object Space
ObjectSpace.each_object(Numeric) { |x| p x }
71
Class Reflection
# Using Class#superclass
klass = Fixnum
begin
pring klass
klass = klass.superclass
print < if klass
end while klass
# => Fixnum < Integer < Numeric < Object
# Using Class#ancestors
p Fixnum.ancestors
# => Fixnum, Integer, Precision, Numeric, Comparable, Object, Kernel
72
System Hooks:
Class#inherited
class ActiveRecord::Base
# Invoked when a new class is created that extends this
# class
def self.inherited(child)
@@subclasses[self] ||= []
@@subclasses[self] << child
end
end
73
Ruby Load path and
auto Loading in Rails
The Ruby load path is stored in $: and is used when
you require code
Models, views, controllers, and helpers under the app
dir are loaded automatically
Classes under lib are also loaded automatically
You can add load paths in config/environment.rb
Class and module names must correspond to the file
path where they are defined for auto loading to work
74
Migrations
75
Migrations
A way to evolve your database schema over time
Migrations use a database independent Ruby API
script/generate migration
Migration files are numbered in a sequence starting
with 001
Migration classes extend ActiveRecord::Migration
and have an up and a down method
rake db:migrate VERSION=X
76
Migrations: Managing
Tables and Columns
create_table, add_column, change_column,
rename_column, rename_table, add_index
Column types: binary, boolean, date, datetime,
decimal, float, integer, string, text, time,
timestamp
Column options: :null, :limit, :default
Table options: :primary_key, :id, :force, :options
Execute SQL with execute(drop table my_table)
77
Migrations: Things to
Be Aware Of
You can use ActiveRecord classes, but this is fragile as
the class definitions might change over time
Foreign keys you have to create yourself. You can use
a helper module for this.
Good practice is to backup your production data
before you run a migration
You can see the schema definition in db/schema.rb
or db/development_structure.rb if
config.active_record.schema_format = :sql
78
Two Special Columns
79
Migration Example
create_table "users", :force => true do |t|
t.column :login, :string
t.column :email, :string
t.column :crypted_password, :string, :limit => 40
t.column :salt, :string, :limit => 40
t.column :created_at, :datetime
t.column :updated_at, :datetime
t.column :remember_token, :string
t.column :remember_token_expires_at, :datetime
end
80
Lets Bring Sexy Back
# Note: this is only available in Edge Rails, *not*
# in Rails 1.2.3
81
ActiveRecord
Basics
82
Fundamentals
One database table maps to one Ruby class
Ruby classes live under app/models and extend
ActiveRecord::Base
Table names are plural and class names are singular
Database columns map to attributes, i.e. get and set
methods, in the model class
All tables have an integer primary key called id
Database tables are created with migrations
83
Overriding Naming
conventions
self.table_name = my_legacy_table
self.primary_key = my_id
self.pluralize_table_names = false
self.table_name_prefix = my_app
84
CRUD
85
Create = new + save
user = User.new user = User.new(
user.first_name = Dave :first_name => Dave,
user.last_name = Thomas :last_name => Thomas
user.new_record? # true )
user.save user.save
user.new_record? # false
user = User.create(
:first_name => Dave,
:last_name => Thomas
)
user.new_record? # false
86
Save!
user = User.new(
:first_name => Dave,
:last_name => Thomas
)
if user.save begin
# All is ok user.save!
else rescue RecordInvalid => e
# Could not save user :-( # Could not save!
end end
87
create! = new + save!
begin
user = User.create!(
:first_name => Dave,
:last_name => Thomas)
rescue RecordInvalid => e
# Could not create user...
end
88
Column/Attribute Data
Types
MySQL Ruby Class
integer Fixnum
clob, blob, text String
float, double Float
char, varchar String
datetime, time Time
89
Custom Attribute
Accessors
class Song < ActiveRecord::Base
def length=(minutes)
# self[:length] = minutes*60
write_attribute(:length, minutes * 60)
end
def length
# self[:length] / 60
read_attribute(:length) / 60
end
end
90
Default Attribute
Values
91
Boolean Attributes
92
find
93
find with :conditions
User.find(:all,
:conditions =>
[first_name = ? and created_at > ?, David, 1.year.ago])
User.find(:all,
:conditions =>
[first_name = :first_name, last_name = :last_name,
{:first_name => David, :last_name => Letterman}])
User.find(:all,
:conditions => {:first_name => Jamis, :last_name => Buck})
94
Power Find with
Additional Attributes
users = User.find(:all,
:conditions => [users.id = taggings.taggable_id and users.age > ?, 25],
:limit => 10,
:offset => 5,
:order => users.last_name,
:joins => , tagggings,
:select => count(*) as count, users.last_name,
:group => users.last_name)
95
Everything is a
Find :all
# select * from users limit 1
User.find(:first) <=> User.find(:all, :limit => 1).first
96
Like Clauses
# Like this
User.find(:all, :conditions => [name like ?, % + params[:name] + %)
97
Dynamic Finders
User.find_by_first_name Peter
User.find_all_by_last_name Hanson
User.find_by_age 20
User.find_by_last_name(Buck,
:conditions => {:country = Sweden, :age => 20..30})
User.find_by_first_name_and_last_name Andreas, Kviby
98
RecordNotFound
Exception
User.exists?(999) # => false
User.find(999) # => raises ActiveRecord::RecordNotFound
User.find_by_id(999) # => nil
User.find(:first, :conditions => {:id => 999}) # => nil
99
Find or Create
100
Update
order = Order.find(12)
order.name = Bill Gates
order.charge = 10000
order.save!
order = Order.find(13)
order.update_attributes!(
:name => Steve Jobs,
:charge => 1
)
101
update_attributes is
Syntactic Sugar
def update_attributes(attributes)
self.attributes = attributes
save
end
def update_attributes!(attributes)
self.attributes = attributes
save!
end
102
Locking
103
Destroy
# Instance method User#destroy
User.count # => 5
u = User.find(:first)
u.destroy
User.count # => 4
104
Destroy Class
Methods
def destroy(id)
id.is_a?(Array) ? id.each { |id| destroy(id) } : find(id).destroy
end
105
Delete: Does not
Instantiate Objects
# Class method User.delete
User.count # => 5
# delete from users where id = 3
User.delete 3
User.count # => 4
User.exists?(3) # => false
106
Calculations
Person.minimum(age)
Person.maximum(age)
Person.sum(age)
Person.count(:conditions => [age > ?, 25])
Person.average(age)
Person.calculate(:std, :age)
107
Executing SQL
AtiveRecord::Base.select_one|select_all|select_value|select_values
108
Virtual Attributes
# Virtual attributes are atttributes that do not correspond
# directly to database columns like normal ActiveRecord
# attributes.
class Person < ActiveRecord::Base
def full_name
first_name + + last_name
end
def full_name=(full_name)
first_name = full_name.split.first
last_name = full_name.split.last
end
end
109
Serializing Attribute
Values
class Person < ActiveRecord::Base
serialize params
end
person = Person.new
person.params = {
:height => 190,
:weight => 80,
:eye_color => blue
}
person.save
110
Composite Attributes
class Name
attr_reader :first, :initials, :last
def initialize(first, initials, last)
@first = first
@initials = initials
@last = last
end
def to_s
[ @first, @initials, @last ].compact.join(" ")
end
end
111
Transactions
Account.transaction do
account1.deposit(100)
account2.withdraw(100)
end
Account.transaction(account1, account2) do
account1.deposit(100)
account2.withdraw(100)
end
112
Chad Fowler Says
113
ActionController
Basics
114
Controllers
115
Controller
Environment
cookies[:login] = { :value => peter, :expires => 1.hour.from_now
params
response
session
116
request.env
SERVER_NAME = localhost
PATH_INFO = /
HTTP_ACCEPT_ENCODING = gzip,deflate
HTTP_USER_AGENT = Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/
2.0.0.3
SCRIPT_NAME = /
SERVER_PROTOCOL = HTTP/1.1
HTTP_CACHE_CONTROL = no-cache
HTTP_ACCEPT_LANGUAGE = en-us,en;q=0.5
HTTP_HOST = localhost:3000
REMOTE_ADDR = 127.0.0.1
SERVER_SOFTWARE = Mongrel 0.3.13.4
HTTP_KEEP_ALIVE = 300
REQUEST_PATH = /
HTTP_COOKIE = volunteer_id=ff4cc4f37c77a4efbee41e9e77a5d3d4bb619d22;
fcP=C=0&T=1176293486407&DTO=1176293486401&U=103359521886406&V=1176293592419;
_session_id=819e71f41ab4e64a111358374c3b662f
HTTP_ACCEPT_CHARSET = ISO-8859-1,utf-8;q=0.7,*;q=0.7
HTTP_VERSION = HTTP/1.1
REQUEST_URI = /
SERVER_PORT = 3000
GATEWAY_INTERFACE = CGI/1.2
HTTP_PRAGMA = no-cache
HTTP_ACCEPT = text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/
*;q=0.5
HTTP_CONNECTION = keep-alive
117
rendering a response
118
Render examples
render :text => Hello World
render :action => some_other_action
render :partial => top_menu
render :xml => xml_string
Options: :status, :layout, :content_type
send_file(/files/some_file.pdf )
119
Redirect Examples
redirect_to :back
redirect_to(/help/order_entry.html)
redirect_to :controller => blog, :action => list
120
Cookies
121
Sessions
122
Configuring Sessions
config.action_controller.session_store
= :active_record_store
rake db:sessions:create
123
The Flash
The flash is a way to set a text message to the user in
one request and then display it in the next (typically
after a redirect)
The flash is stored in the session
flash[:notice], flash[:error]
flash.now[:notice] = Welcome unless flash[:notice]
flash.keep(:notice)
124
Best Practice
125
ActionView
Basics
126
What is ActionView?
127
Where Templates Live
Templates that belong to a certain controller typically
live under app/view/controller_name, i.e. templates
for Admin::UsersController would live under app/
views/admin/users
Templates shared across controllers are put under
app/views/shared. You can render them with
render :template => shared/my_template
You can have templates shared across Rails
applications and render them with render :file =>
path/to/template
128
Template Environment
129
Three Types of
Templates
rxml - Files with Ruby code using the Builder library
to generate XML. Typically used for RSS/Atom.
rhtml - The most common type of template used for
HTML. They are HTML files with embedded Ruby
and they use the ERb library.
rjs - Ruby code with a Rails specific API that
generate JavaScript. Used for AJAX functionality.
130
Builder Template
Example: RSS
xml.instruct!
xml.rss "version" => "2.0", "xmlns:dc" => "http://purl.org/dc/elements/1.1/" do
xml.channel do
xml.title "Recent comments for #{@user.login}"
xml.link @rss_url
xml.pubDate CGI.rfc1123_date(@comments.first ? @comments.first.updated_at :
Time.now)
xml.description ""
@comments.each do |comment|
xml.item do
xml.title "Comment by #{comment.creation_user.login} #{time_ago_in_words
comment.created_at} ago"
xml.link @server_url + comment_url(comment)
xml.description h(comment.body)
xml.pubDate CGI.rfc1123_date(comment.updated_at)
xml.guid @server_url + comment_url(comment)
xml.author h(comment.creation_user.login)
end
end
end
end
131
rhtml Templates
<%= ruby code here %> - Evaluates the Ruby code
and prints the last evaluated value to the page.
<% ruby code here %> - Evaluates Ruby code
without outputting anything to the page.
Use a minus sign (i.e. <%= ... %-> and <% ... %->)
to avoid the newline after the tag to be printed to the
page.
Remember to quote especially user inputted data
with the helper h: <%= h comment.body %>.
132
Partials
Partials are templates that render a part of a page,
such as a header or footer, or a menu, or a listing of
articles
Partials help promote reuse of page elements
Partials work just like page templates (views) and run
in the same environment. They also live in the same
directory as page templates.
The filenames of partials always start with an
underscore.
133
Rendering Partials
134
Passing Variables to
Partials
Controller instance variables are available in partials
You can pass any objects into local variables in the partial with
the :locals argument: render :partial => article, :locals =>
{ :author => @author, :options => @options }
135
Partials and
Collections
<% for article in @articles %>
<%= render :partial => article, :object => article %>
<% end %>
136
Layouts
137
Determining Which
Layout to Use
If no layout is specified in the controller or render method then
Rails looks for a controller layout at app/views/layouts/
controller_name.rhtml and uses that.
138
Dynamic Layout
Selection
private
def determine_layout
user.admin? ? admin : standard
end
end
139
Passing Data to
Layouts
140
Helpers
141
text_helper.rb
truncate("Once upon a time in a world far far away", 14)
highlight('You searched for: rails', 'rails')
excerpt('This is an example', 'an', 5)
pluralize(2, 'person')
word_wrap('Once upon a time', 4)
textilize(text)
markdown(text)
simple_format(text)
auto_link(text)
strip_links(text)
sanitize(html)
strip_tags(html)
<tr class="<%= cycle("even", "odd") -%>">
142
url_helper.rb
url_for({:controller => blog}, :only_path => false)
link_to "Delete Image", { :action => "delete", :id => @image.id }, :method
=> :delete
143
Try a different
Templating System:
HAML
#content
.left.column
%h2 Welcome to our site!
%p= print_information
.right.column= render :partial => "sidebar"
<div id='content'>
<div class='left column'>
<h2>Welcome to our site!</h2>
<p>
<%= print_information %>
</p>
</div>
<div class="right column">
<%= render :partial => "sidebar" %>
</div>
</div>
144
Testing
145
Rails Testing
Landscape
Rails Ruby Tool Interface
WWW::Mechanize HTTP
146
Test::Unit:TestCase
Test::Unit is a Ruby testing library, very similar to JUnit.
Every method in the test case with a name that starts with
test_ represents a single test that gets executed by the
framework.
147
Unit Tests
148
Unit Test Example
require File.dirname(__FILE__) + '/../test_helper'
def test_should_create_user
assert_difference User.count do # Requires Edge Rails
user = create_user
assert !user.new_record?, "#{user.errors.full_messages.to_sentence}"
end
end
protected
def create_user(options = {})
User.create({
:name => "Quire",
:email => '[email protected]',
:password => 'quire',
:password_confirmation => 'quire',
:role => 'super'
}.merge(options))
end
end
149
test_helper.rb
ENV["RAILS_ENV"] = "test"
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
require 'test_help'
class Test::Unit::TestCase
# Transactional fixtures accelerate your tests by wrapping each test method
# in a transaction that's rolled back on completion
self.use_transactional_fixtures = true
# Instantiated fixtures are slow, but give you @david where otherwise you
# would need people(:david)
self.use_instantiated_fixtures = false
150
Test Data with
Fixtures
Fixtures are files that load test data into the test database that
tests can run against. Every model and database table has a
corresponding fixture file at test/fixtures/table_name.yml
The fixture command will delete from the specified tables and
then load their fixture files. The fixtures will then be available in
your tests as table_name(:fixture_name), i.e. users(:joe).
151
Fixture Example:
users.yml
quentin:
id: 1
login: quentin
email: [email protected]
salt: 7e3041ebc2fc05a40c60028e2c4901a81035d3cd
crypted_password: 00742970dc9e6319f8019fd54864d3ea740f04b1 # test
created_at: <%= 5.days.ago.to_s :db %>
aaron:
id: 2
login: aaron
email: [email protected]
salt: 7e3041ebc2fc05a40c60028e2c4901a81035d3cd
crypted_password: 00742970dc9e6319f8019fd54864d3ea740f04b1 # test
bio: Aaron is a weird guy
created_at: <%= 1.days.ago.to_s :db %>
152
Assertions
assert(actual, comment) # Asserts truth
assert_equal(expected, actual, comment)
assert_in_delta(expected_float, actual_float, delta,
message)
assert_match(pattern, string, message)
assert_nil(object, message)/assert_not_nil
assert_raise(Exception, ..., message) { block ... }
assert_difference(expressions, difference = 1, &block)
153
Functional Testing of
Controllers
154
Functional Test
Example
require File.dirname(__FILE__) + '/../test_helper'
require 'comments_controller'
def setup
@controller = CommentsController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
@request.env['HTTP_HOST'] = "localhost"
@request.session[:user] = users(:aaron)
end
def test_rss
get :rss, :id => users(:quentin)
assert_response :success
assert_select "rss > channel" do
assert_select "title", /Recent comments/
assert_select "item", 1
assert_select "item > title", Regexp.new(users(:aaron).login)
assert_select "item > description", users(:quentin).comments.first.body
end
end
155
Assertions in
Functional Tests
assert_response :success|:redirect|:missing|:error
assert_redirected_to(:controller => blog, :action =>
list)
assert_template store/index
assert_not_nil assigns(:items)
assert session[:user]
assert_not_nil flash[:notice]
156
assert_select
assert_select p.warning # <p class=warning>...</p>
assert_select p#warning # <p id=warning>...</p>
assert_select html p.warning # Ancestor chaining
assert_select html > body > p.warning # Direct parent chaining
assert_select div#cart table tr, 3 # Integer, n times
assert_select div#cart table tr, 3..5 # Range, n times
assert_select div#cart table tr, false # Not present on page
assert_select div#cart table tr td#price, $23 # Tag contents
assert_select div#cart table tr td#price, /23/ # Regexp
157
Integration Tests
Test against the Rails dispatcher and can span all
controllers
Simulate user scenarios/stories.
Can involve multiple simultaneous sessions
You make requests with the methods get/post etc.
You have access to pretty much the same
environment and assertions as in functional tests
158
Integration Test
Example
class TracerBulletTest < ActionController::IntegrationTest
def test_tracer_bullet
get("/mcm/user/login")
assert_response :success
expect_count = contacts(:adam_sandler).jobs.size
post("/mcm/contacts/search", :q => 'sandler new york')
assert_response :success
assert_n_search_results(expect_count)
get "/mcm/lists/show/#{list.id}"
assert_response :success
assert_template 'mcm/lists/show'
end
end
159
Integration Test
Example: With DSL
class TracerBulletTest < ActionController::IntegrationTest
def test_tracer_bullet
setup_test_users
carl = new_session_as(:carl)
carl.logs_in
carl.searches_for_contacts
...
end
module TestingDSL
attr_accessor :mail, :password
def logs_in
get("/mcm/user/login")
assert_response :success
def new_session_as(person)
open_session do |sess|
sess.extend(TestingDSL)
end
end
160
Running Tests
161
Stubbing and Mocking
Mock and stub objects are similar, but mock objects tend to be
more intelligent and verify that the right messages are received.
Mock classes that should be in place for all tests (static mocks)
can be put under test/mocks/test.
You may use the libraries Mocha and Stubba or FlexMock for
dynamic stubbing/mocking. The stubs/mocks that you set up
are isolated to the test.
162
Mocha and Stubba
Examples
client = Goyada::HttpClient.new({})
client.expects(:http_timeout).returns(0.01)
client.expects(:get_connection).returns(lambda { sleep 10 })
response = client.send(:https_response, "http://www.test.com", nil, nil)
assert_equal(client.send(:error_response).code, response.code)
assert_equal(Timeout::Error, response.exception.class)
163
Submitting Forms and
Clicking Links
A limitation in most controller and integration tests is
that they bypass forms and links in the views.
To be able to submit forms you may use the Form
Test Helper plugin, or alternatively Hpricot Forms.
# Form Test Helper usage example:
select_link("/user/aaron").follow
164
rcov
rcov is a Ruby library that measures code coverage of
tests. It can be used to find gaps in your test coverage.
rcov will generate a report of how many percent of
each Ruby class is covered and indicate which lines of
code are not executed by the tests.
# Installation of rcov:
gem install rcov
ruby script/plugin install http://svn.codahale.com/rails_rcov
rake test:test:rcov
165
Heckle
Heckle will mutate your code by inverting boolean
expressions and run your tests and make sure they fail
Heckle helps find missing assertions and can also find
redundancies in your code.
# Installation of Heckle:
gem install -y heckle
heckle -t test/functional/comments_controller_test.rb CommentsController create
166
AJAX and RJS Testing
When you develop AJAX functionality you write
actions generate and return JavaScript with the RJS
API.
The best way to test AJAX functionality is with a
browser testing tool like Selenium.
With the ARTS plugin you can make assertions
against the RJS code in your controller and
integration tests.
167
ARTS Plugin Usage
Example
# In a controller test...
def test_edit
xhr :get, :edit, :id => users(:aaron), :attribute => 'bio'
assert_response :success
assert_rjs :page, dom_id('bio', :edit_link), :hide
assert_rjs :replace, dom_id('bio', :div), /<form/
end
168
HTML Validation and
Link Checking
169
Using the Rails Tests
as Documentation
170
Test Case Specific
Fixtures
Sharing fixtures across all tests doesnt scale so well
and can become hard to maintain. A solution is to
use the plugin Fixture Scenarios.
[RAILS_ROOT]
+-test/
+-fixtures/
+-brand_new_user/
+-users.yml
171
BDD: From Verification
to Specification
Behaviour Driven Development (BDD) is Test Driven
Development (TDD) with a new terminology and structure
Two popular BDD tools for Rails are RSpec and test/spec.
172
RSpec Examples
require File.join(File.dirname(__FILE__), '/../spec_helper')
describe Admin::ServicesController do
include ControllerSpecHelper
fixtures :customers, :services, :users, :services_users, :audio_files, :prompts, :calls
integrate_views
it "Edit form, super user: outcall fields should be visible for outcall service" do
login(:super)
get :show, :id => services(:outcall).id
response.code.should == "200"
end
end
173
ActiveRecord
Associations
174
Three Kinds of
Relationships
class A class B Foreign keys Mapping
Any number
class Post class Category of posts maps
categories_posts.post_id
has_and_belongs_to_many :categories has_and_belongs_to_many :posts
end end categories_posts.category_id to any number
of categories
175
has_one
176
belongs_to
class LineItem < ActiveRecord::Base
belongs_to :paid_order,
:class_name => Order,
:foreign_key => order_id,
:conditions => paid_on is not null
end
li = LineItem.find(1)
puts li.product.name
177
has_many
has_many :comments, :order => "posted_on"
has_many :comments, :include => :author
has_many :people, :class_name => "Person",
:conditions => "deleted = 0", :order => "name"
has_many :tracks, :order => "position", :dependent => :destroy
has_many :comments, :dependent => :nullify
has_many :tags, :as => :taggable
has_many :subscribers, :through => :subscriptions, :source => :user
has_many :subscribers, :class_name => "Person", :finder_sql =>
'SELECT DISTINCT people.* ' +
'FROM people p, post_subscriptions ps ' +
'WHERE ps.post_id = #{id} AND ps.person_id = p.id ' +
'ORDER BY p.first_name'
178
Methods added by
has_many
Firm#clients (similar to Clients.find :all, :conditions => "firm_id = #{id}")
Firm#clients<<
Firm#clients.delete
Firm#clients=
Firm#client_ids
Firm#client_ids=
Firm#clients.clear
Firm#clients.empty? (similar to firm.clients.size == 0)
Firm#clients.count
Firm#clients.find (similar to Client.find(id, :conditions => "firm_id = #{id}"))
Firm#clients.build (similar to Client.new("firm_id" => id))
Firm#clients.create (similar to c = Client.new("firm_id" => id); c.save; c)
179
has_many example
blog = User.find(1).weblog
blog.posts.count # => 0
blog.posts << Post.new(:title => Hi, this is my first post!)
blog.posts.count # => 1
blog.posts.find(:conditions => [created_at > ?, 1.minute.ago]) = blog.posts.first
180
has_and_belongs_to_many
# Requires a join table
create_table :categories_posts, :id => false do
t.column :category_id, :integer, :null => false
t.column :post_id, :integer, :null => false
end
# Indices for performance
add_index :categories_posts, [:category_id, :post_id]
add_index :categories_posts, :post_id
181
Join Models
class Article < ActiveRecord::Base
has_many :readings
has_many :users, :through => :readings
end
class User < ActiveRecord::Base
has_many :readings
has_many :articles, :through => :readings
end
class Reading < ActiveRecord::Base
belongs_to :article
belongs_to :user
end
user = User.find(1)
article = Article.find(1)
Reading.create(
:rating => 3,
:read_at => Time.now,
:article => article,
:user => user
)
article.users.first == user
182
Join Model with
Conditions
class Article < ActiveRecord::Base
has_many :happy_users, :through => :readings,
:source => :user,
:conditions => readings.rating >= 4
end
article = Article.find(1)
article.happy_users
183
Extending
Associations
class User < ActiveRecord::Base
has_many :articles, :through => :readings do
def rated_at_or_above(rating)
find :all, :conditions => [rating >= ?, rating]
end
end
end
user = User.find(1)
good_articles = user.articles.rated_at_or_above(4)
184
Polymorphic
Associations
create_table :images, :force => true do |t|
t.column :comment, :string
t.column :file_path, :string
t.column :has_image_id, :integer
t.column :has_image_type, :string
end
185
Single Table Inheritance: Table
Definition
create_table :people, :force => true do |t|
t.column :type, :string
# common attributes
t.column :name, :string
t.column :email, :string
186
Single Table Inheritance: Class
Hierarchy
class Person < ActiveRecord::Base
end
187
Single Table Inheritance: Usage
barney.boss = wilma
barney.save!
188
Acts As List
class Parent < ActiveRecord::Base
has_many :children, :order => :position
end
189
Acts As List: Usage
parent = Parent.new
%w{ One Two Three Four}.each do |name|
parent.children.create(:name => name)
end
parent.save
def display_children(parent)
puts parent.children(true).map {|child| child.name }.join(", ")
end
parent.children[0].move_lower
display_children(parent) #=> Two, One, Three, Four
parent.children[2].move_to_top
display_children(parent) #=> Three, Two, One, Four
parent.children[2].destroy
display_children(parent) #=> Three, Two, Four
190
Acts As Tree
191
Acts As Tree: Usage
root = Category.create(:name =>"Books")
fiction = root.children.create(:name =>"Fiction")
fiction.children.create(:name =>"Mystery")
fiction.children.create(:name =>"Romance")
fiction.children.create(:name =>"ScienceFiction")
sub_category = root.children.first
192
Eager Loading: From
N+1 to 1 Query
# Joins posts, authors, comments
# in a single select
@posts = Post.find(:all,
:conditions => "posts.title like '%ruby%'",
:include => [:author, :comments])
193
Counter Cache
create_table :posts do
...
t.column comments_count, :integer
end
194
Association Callbacks
class Project
# Possible callbacks: :after_add, :before_add, :after_remove, :before_remove
has_and_belongs_to_many :developers,
:after_add => [:evaluate_velocity,
Proc.new { |p, d| p.shipping_date = Time.now}]
def evaluate_velocity(developer)
...
end
end
195
ActiveRecord
Validations
196
Validation
197
Validation Callback
Methods
class Person < ActiveRecord::Base
def validate
puts validate invoked
end
def validate_on_create
puts validate_on_create invoked
end
def validate_on_update
puts validate_on_update invoked
end
end
198
Validation Errors
class Person < ActiveRecord::Base
def validate
if Person.find_by_name(name)
errors.add(:name, is already being used)
end
199
Validation Macros
validates_acceptance_of
validate_associated
validates_confirmation_of
validates_each
validates_exclusion_of
validates_format_of
validates_inclusion_of
validates_length_of
validates_numericality_of
validates_presence_of
validates_size_of
validates_uniqueness_of
200
Validation Macros:
Usage
class User < ActiveRecord::Base
validates_presence_of :name, :email, :password
validates_format_of :name,
:with => /^\w+$/,
:message => may only contain word characters
validates_uniqueness_of :name,
:message => is already in use
validates_length_of :password,
:within => 4..40
validates_confirmation_of :password
validates_inclusion_of :role,
:in => %w(super admin user),
:message => must be super, admin, or user,
:allow_nil => true
validates_presence_of :customer_id,
:if => Proc.new { |u| %w(admin user).include?(u.role) }
validates_numericality_of :weight,
:only_integer => true,
:allow_nil => true
end
201
ActiveRecord
Callbacks
202
Callback Sequence
create update destroy
before_validation before_validation
before_validation_on_create before_validation_on_update
after_validation after_validation
before_destroy
after_validation_on_create after_validation_on_update
before_save before_save
before_create before_update
after_create after_update
after_destroy
after_save after_save
203
Three Common Ways to
Define Callbacks
# 1. Defining a method
def before_save
# encrypt password here
end
204
before_save Example
class User < ActiveRecord::Base
before_save :encrypt_password
private
def encrypt_password
return if password.blank?
if new_record?
self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{login}--")
end
self.crypted_password = encrypt(password)
end
end
205
after_save Example
class Comment < ActiveRecord::Base
belongs_to :user
def after_save
user.ferret_update
end
def after_destroy
user.ferret_update
end
end
206
Observers
class AuditObserver < ActiveRecord::Observer
observe Order, Payment, Refund
def after_save(model)
model.logger.info("Audit: #{model.class.name} #
{model.id} created")
end
end
207
after_find and
after_initialize: must
be methods
def after_find
end
def after_initialize
end
208
ActionView
Forms
209
How Forms Work
Form helpers => HTML => params =>
ActiveRecord validate + save
<% form_tag({:action => 'update', :id => @user} do %>
<%= text_field 'user', 'email' %>
<% end %>
@user = User.find(params[:id])
@user.update_attributes(params[:user])
210
form_for - Wrapping
Model Objects
<%= error_messages_for :user %>
211
Forms with Multiple
Objects: fields_for
<% form_for :company do |f| -%>
<%= f.text_field :name %>
<%= f.text_area :description %>
<fieldset>
<legend>Product</legend>
<%= error_messages_for :product %>
<% fields_for :product do |p| %>
<%= p.text_field.name %>
<% end %>
</fieldset>
212
Processing Multiple
Object Form
Submission
def create
@company = Company.new(params[:company])
@product = Product.new(params[:product])
Company.transaction do
@company.save!
@product.save!
redirect_to :action => :show, :id => @company
end
rescue ActiveRecord::RecordInvalid => e
@product.valid? # Force validation as it may not have been validated
render :action => :new
end
213
form_helper.rb
fields_for :permission, @person.permission do |fields| ...
password_field
hidden_field
file_field
214
Select Boxes
<% form_for :user do |form| %>
<%= form.select(:name,
%w{ Andy Bert Chas Dave Eric Fred }) %>
<%=
@users = User.find(:all, :order => "name")
form.collection_select(:name, @users, :id, :name)
%>
<% end %>
215
form_options_helper.rb
select("post", "category",
Post::CATEGORIES, {:include_blank => true})
select("post", "person_id",
Person.find(:all).collect {|p| [ p.name, p.id ] })
select_tag "service[users][]",
options_for_select(User.find(:all, :order => 'name').
map { |u| [u.name, u.id] }, @service.users.map(&:id)),
{:multiple => "multiple", :size => 10, :id => 'users'}
time_zone_select
country_select
option_groups_from_collection_for_select
216
date_helper.rb
distance_of_time_in_words(from_time, to_time)
time_ago_in_words(from_time)
date_select("post", "written_on",
:start_year => 1995, :use_month_numbers => true,
:discard_day => true, :include_blank => true)
datetime_select("post", "written_on")
217
Custom Form Builders
class TaggedBuilder < ActionView::Helpers::FormBuilder
# <p>
# <label for="product_description">Description</label><br/>
# <%= form.text_area 'description' %>
#</p>
def self.create_tagged_field(method_name)
define_method(method_name) do |label, *args|
@template.content_tag("p",
@template.content_tag("label" ,
label.to_s.humanize,
:for => "#{@object_name}_#{label}") +
"<br/>" + super)
end
end
field_helpers.each do |name|
create_tagged_field(name)
end
end
ActionView::Helpers::Base.default_form_builder = TaggedBuilder
218
form_tag_helper.rb -
Working with Non-
model Fields
<% form_tag('/upload', :multipart => true) do %>
...
<% end %>
219
File Upload
class Fax < ActiveRecord::Base
def fax_file=(fax_file_field)
@fax_file_field = fax_file_field
return if fax_file_field.blank? or fax_file_field.original_filename.blank?
self.fax_file_name =File.basename(fax_file_field.original_filename) if !fax_file_field.original_filename.blank?
self.fax_file_type =fax_file_field.content_type.chomp if !fax_file_field.content_type.blank?
end
def after_save
write_fax_file
end
def after_destroy
File.delete(self.fax_file_path) if File.exists?(self.fax_file_path)
end
def write_fax_file
return if @fax_file_field.blank? or @fax_file_field.original_filename.blank?
FileUtils.mkdir_p(File.dirname(self.fax_file_path)) if !File.exists?(File.dirname(self.fax_file_path))
if @fax_file_field.instance_of?(Tempfile)
FileUtils.copy(@fax_file_field.local_path, self.fax_file_path)
else
File.open(self.fax_file_path, "wb") { |f| f.write(@fax_file_field.read) }
end
@fax_file_field = nil
end
end
220
There is a Better Way:
attachment_fu
class User < ActiveRecord::Base
validates_as_attachment
end
221
activerecord_helper.rb
222
Styling Error
Messages
ActionView::Base.field_with_error_proc =
Proc.new do |html_tag, instance|
"<div class=\"fieldWithErrors\">#{html_tag}</div>"
end
223
Filters
224
Filters
Typically used to implement authentication and
authorization. Can also be used for logging,
compression, or just code reuse between actions
There are before, after, and around filters
before_filter :authenticate, :except => [:rss]
after_filter :log_access, :only => [:rss]
If a before filter returns false then the request is
aborted
225
Around Filters
class BlogController < ApplicationController
around_filter :time_an_action
end
def time_an_action
started = Time.now
yield
elapsed = Time.now - started
logger.info("#{action_name} took #{elapsed} seconds")
end
226
Filter Blocks and
Classes
around_filter do |controller, action|
started = Time.now
action.call
elapsed = Time.now - started
end
class TimingFilter
def filter(controller)
started = Time.now
yield
elapsed = Time.now - started
end
end
around_filter TimingFilter.new
227
Filter Inheritance
228
Verification
229
Caching
230
Caching
Rails provides three types of caching: page, action,
and fragment caching
Page caching creates whole html pages under public
that get served by the web server without Rails
Action caching caches only the output of an action.
Filters (i.e. authentication) still get run
You can use Memcached with plugins such as
Cached Model and Cache Fu to cache some or all of
your database queries
231
Cache Declarations
and Configuration
caches_page :public_content
expire_page :action => public_content
caches_action :premium_content
expire_action :action => premium_content, :id => 2
config.action_controller.perform_caching = true
232
Cache Storage
233
Cache Sweepers
class ArticleSweeper < ActionController::Caching::Sweeper
observe Article
def after_create(article)
expire_public_page
end
def after_update(article)
expire_article_page(article.id)
end
def after_destroy(article)
expire_public_page
expire_article_page(article.id)
end
private
def expire_public_page
expire_page(:controller => "content", :action => 'public_content')
end
def expire_article_page(article_id)
expire_action(:controller => "content",
:action => "premium_content",
:id => article_id)
end
end
234
Cache Sweeper
Declaration
class ContentController < ApplicationController
cache_sweeper :article_sweeper, :only =>
[ :create_article, :update_article, :delete_article ]
end
235
Fragment Caching
Fragment caching is used for pages where only certain parts
(fragments) should be cached whereas other parts should be
dynamic
236
Fragment Cache
Storage
Fragment caches can be stored in files, in memory
(for single server), on a DRb server, or in memcached
You configure storage with the parameter
ActionController::Base.fragment_cache_store in
config/environement.rb
See fragment_store_setting_test.rb and caching.rb in
the Rails sources for more details about cache storage
and caching in general.
237
AJAX
238
AJAX - Introduction
AJAX stands for Asynchronous JavaScript and XML
AJAX uses an XMLHttpRequest object that does
HTTP requests in the background. This avoids full
page reloads and instead update parts of the page.
This makes the application more responsive and
interactive.
Rails ships with two AJAX JavaScript libraries:
Prototype and Scriptaculous. Prototype makes
remote requests and interacts with the DOM.
Scriptaculous uses Prototype to generate visual
effects, auto completion, drag-and-drop etc.
239
What Can we Use AJAX
For?
Post something in the background and add it to a list
on the page
In-Place form editing
Autocompletion of a text field
Drag-and-drop sortable lists
Live searching
240
ActionView::Helpers::
PrototypeHelper
link_to_remote(name, options = {}, html_option = {})
periodically_call_remote(options = {})
remote_function(options)
241
ActionView::Helpers::
ScriptaculousHelper
242
ActionView::Helpers::
JavaScriptMacrosHelper
# Note: this helper module will be moved out of Rails with 2.0 and
# end up in a plugin
243
AJAX Links and Forms
link_to_remote(Destroy, :url => {:action => destroy, :id => item},
:confirm => Are you sure?
244
RJS
245
Example: Posting a
Comment: The View
# In your .rhtml view:
<% form_remote_tag
:url => {:controller => "comments", :action => "create", :id => user},
:html => { :id => "comment_form"},
:before => "$('spinner').show()",
:complete => "$('spinner').hide()" do %>
<%= text_area "comment", "body", :cols => 80 %><br/>
<%= submit_tag 'Submit' %>
<% end %>
246
Example: Posting a
Comment: The
Controller Action
def create
@user = User.find(params[:id])
@comment = @user.comments.build(params[:comment])
@comment.creation_user_id = current_user.id
if @comment.save
render :update do |page|
page.insert_html :bottom, 'comment_list', :partial => 'comment'
page.visual_effect :highlight, @comment.dom_id
page['comment_form'].reset
end
else
render :update do |page|
page.alert "Could not add comment for the following reasons:\n" +
@comment.errors.full_messages.map{|m| "* #{m}"}.join("\n") +
"\nPlease change your input and submit the form again."
end
end
end
247
Example: Deleting a
Comment
# In your .rhtml view:
<div class="comment" id="<%= comment.dom_id %>">
<p>
Hey, nice photo!
</p>
<%= link_to_remote "Delete", :url => {:controller => "comments",
:action => 'destroy', :id => comment},
:confirm => "Are you sure you want to delete your comment?",
:update => comment.dom_id %>
</div>
# The action in the controller. The innerHTML of the comment div is replaced with
# nothing and thus the comment disappears from the page.
def destroy
@comment = Comment.find(params[:id])
assert_authorized
@comment.destroy
render :text => ''
end
248
Options for Remote
Links and Forms
# Callbacks:
:before - before request is initiated and before request object is created
:after - request objects open method has not been called yet
:loading - request has not been sent yet
:loaded - request has been initiated
:interactive - response is being received
:success - response is ready and in 200 range
:failure - response is ready and is not in 200 range
:complete - response is ready
# Other options
:submit - id of a form to submit, can also be just a div with form elements
:confirm - JavaScript confirm dialog before request
:condition - JavaScript expression that must be true for request to happen
249
Server Responses to
Ajax Requests
nothing, just HTTP headers
An HTML snippet to be injected into the page
Structured data (JSON, XML, YAML, CSV etc.) to
be processed with JavaScript
JavaScript code to be executed by the browser.
Typically generated with RJS.
250
Options for Updating
the Page
If the action returns HTML you can use the :update
options which takes a DOM id where the HTML
should be inserted. You can specify different DOM
ids for success and failure: :update => {:success =>
list, :failure => error}
In conjunction with the :update options you can
specify the :position option which says where the
HTML should be inserted. Possible values
are: :before, :top, :bottom, :after
251
Prototype Basics
$A(document.getElementsByTagName(a)).first()
$H({ren:happy, stimpy:joy}).keys()
$(some_id).hide()|show() # instead of
document.getElementById(some_id)
$F(login) # The value of field input login
$$(div#footer).invoke(hide) # CSS selector
$$(a).each( function(element) { element.hide() })
252
Example: An AJAX
Shopping Cart
# In index.rhtml:
<% form_remote_tag :url => { :action => :add_to_cart, :id => product } do %>
<%= submit_tag "Add to Cart" %>
<% end %>
# The action:
def add_to_cart
product = Product.find(params[:id])
@current_item = @cart.add_product(product)
redirect_to_index unless request.xhr?
end
253
RJS Methods
# Position argument is one of :before, :top, :bottom, :after
page.insert_html :bottom todo_list, <li>#{todo.name}</li>
page.replace_html flash_notice, Todo added: #{todo_name}
page.replace flash_notice, :partial => flash, :object => todo
page.delay(3) do
page.visual_effect :fade, flash_notice
end
page.select('p.welcome b').first.hide
254
Observing Forms
<%= form_tag({:action => "search"}, {:id => 'search_form'}) %>
...
<%= observe_form('search_form',
:url => {:action => 'search_count'},
:frequency => 3,
:condition => (@model_name == 'Contact' ?
"!$('search_form').submitting && !contact_search_form_empty()" :
"!$('search_form').submitting && !outlet_search_form_empty()"),
:with => "search_form",
:loading => "$('spinner').show();",
:complete => "$('spinner').hide();") %>
<%= observe_field("show_hide_select",
:url => { :action => 'toggle_column', :item_count => item_count },
:with => "'column_name=' + value") %>
255
Example Response to
Form Change
# This is a search form where we want to display a preview of the number
# of search hits
def search_count
query = params[:search_form][/^q=(.*)/, 1]
if form_has_errors?
render :update do |page|
page.alert(@form_errors.join("\n"))
end and return
end
...
render :update do |page|
page["search_count_preview"].show
page["search_count_preview"].replace_html :partial => '/mcm/search/
search_count_preview'
page.visual_effect :highlight, "search_count_preview"
if @search_count > 0
page.mcm.set_color("search_count_preview", "green")
else
page.mcm.set_color("search_count_preview", "red")
end
end
end
256
Options for Observers
257
Dashed DOM ID Plugin
258
The Spinner Icon
module ApplicationHelper
def spinner_icon(id)
%Q{<img class="spinner" id="#{spinner_dom_id(id)}"
src="/images/spinner.gif" alt="Comment being processed"
style="display: none" />}
end
def spinner_dom_id(id)
dom_id(:spinner, id)
end
end
259
Drag and Drop
Example: The Request
Side
# This example is about dragging books in list to a shopping cart in the menu bar.
# In index.rhtml
<li class="book" id="book_<%= book.id %>">
...book info here...
</li>
<%= draggable_element("book_#{book.id}", :revert => true) %>
# In application.rhtml
<div id="shopping_cart">
<%= render :partial => "cart/cart" %>
</div>
<%= drop_receiving_element("shopping_cart", :url =>
{ :controller => "cart", :action => "add" }) %>
<% end %>
260
Drag and Drop
Example: The Response
# The action
def add
params[:id].gsub!(/book_/, "")
@book = Book.find(params[:id])
if request.xhr?
@item = @cart.add(params[:id])
flash.now[:cart_notice] = "Added <em>#{@item.book.title}</em>"
render :action => "add_with_ajax"
elsif request.post?
@item = @cart.add(params[:id])
flash[:cart_notice] = "Added <em>#{@item.book.title}</em>"
redirect_to :controller => "catalog"
else
render
end
end
# add_with_ajax.rjs:
page.replace_html "shopping_cart", :partial => "cart"
page.visual_effect :highlight, "cart_item_#{@item.book.id}", :duration => 3
page.visual_effect :fade, 'cart_notice', :duration => 3
261
Autocompletion
262
Autocompletion:
Example
# In text field view:
<%= text_field 'user', 'favorite_language' %>
<div class="auto_complete" id="user_favorite_language_auto_complete"></div>
<%= auto_complete_field :user_favorite_language,
:url=>{:action=>'autocomplete_favorite_language'}, :tokens => ',',
:frequency => 0.5,
:min_chars => 3 %>
# The action
def autocomplete_favorite_language
re = Regexp.new("^#{params[:user][:favorite_language]}", "i")
@languages= LANGUAGES.find_all do |l|
l.match re
end
render :layout=>false
end
263
In-Place-Edit
# In the view
<div id="<%= dom_id(:email, :div) %>"><%= @user.email %></div>
<%= in_place_editor dom_id(:email, :div),
:url => {:action => "set_user_email", :id => @user} %>
# In the controller
class UsersController < ApplicationController
in_place_edit_for :user, :email
...
end
# WARNINGS
# 1) in_place_edit_for does *not* use validation
# 2) in_place_editor quotes the value edited. TODO: details on this
# Options:
# :rows, :cols, :cancel_text, :save_text, :loading_text, :external_control,
# :load_text_url
264
Global AJAX Hooks
# In public/javascripts/application.js. Will show the spinner whenever
# an AJAX request is in process.
Ajax.Responders.register({
onCreate: function(){
$('spinner').show();
},
onComplete: function() {
if(Ajax.activeRequestCount == 0)
$('spinner').hide();
}
});
265
Degradability for
When There is no
JavaScript
form_remote_tag will by default fall and submit to the AJAX URL. To submit to a
different URL, you can specify :html => {:action => {:action => some_action}}
In your actions you can give different responses depending on if the request is
an AJAX request or not (using request.xhr?).
266
RJS Reuse
module ApplicationHelper
def replace_article(article)
update_page do |page|
page[:article].replace partial => :article, :locals => {:article => article}
end
end
end
def update
@article = Article.find(:first)
render :update do |page|
page << replace_article(@article)
page.highlight :article
end
end
267
Browser Tools
268
Routing
Going Beyond
:controller/:action/:id
269
Routes
Routes are rules that map URLs to the params hash
The params hash contains the controller and action
to invoke
Routes are defined in config/routes.rb and are
applied in the order that they appear
If no routes match 404 is returned
The goal is pretty, human readable, and search
engine friendly URLs
270
Anatomy of a Route
map.route_name url_pattern, params_hash
map.user_list users, :controller => users, :action => list
271
Defaults and
requirements
map.connect "blog/:year/:month/:day",
:controller => "blog",
:action => "show_date",
:requirements => { :year => /(19|20)\d\d/,
:month => /[01]?\d/,
:day => /[0-3]?\d/},
:day => nil,
:month => nil
272
splat params
map.connect *anything,
:controller => blog,
:action => unknown_request
map.connect download/*path,
:controller => file
:action => download
273
URL Generation
274
Routes in the console
rs = ActionController::Routing::Routes
puts rs.routes
rs.recognize_path /store/add_to_cart/1
rs.generate :controller => store, :id => 123
275
Testing Routes
def test_movie_route_properly_splits
opts = {:controller => "plugin", :action => "checkout", :id => "2"}
assert_routing "plugin/checkout/2", opts
end
def test_route_has_options
opts = {:controller => "plugin", :action => "show", :id => "12"}
assert_recognizes opts, "/plugins/show/12"
end
276
REST
277
REST - The Theory
REST is an alternative to SOAP and is a way to add
a web service API to your application.
Representional State Transfer (REST) is an
architecture for hypermedia system.
The state of a system is divided into resources that
are addressable with hyperlinks. All resources share a
uniform interface with well defined operations.
Connections between client and server are stateless.
REST is designed to support scalability and
decoupling.
278
Rails Implementation
of REST: CRUD
Resources are typically ActiveRecord models and
each model has a controller with seven actions: index,
create, new, show, update, edit, destroy
We are constrained to four types of operations:
Create, Read, Update, and Delete (CRUD)
The four operations correspond to the HTTP verbs
GET, POST, PUT, DELETE
In REST we strive to have associations be join
models so that they can be exposed as resources.
279
REST and Rails
building Blocks
Naming conventions for controllers and actions
281
REST Gives you Named
Routes for Free
# Named route helpers:
article_url
articles_url
new_article_url
edit_articles_url
# Old
link_to :controller => articles,
:action => destroy,:post => :true
# New
link_to articles_url(@article), :method => :delete
282
The Accept Header and
Extensions
# The respond_to method will use the HTTP Accept header or
# any suffix in the URL. So if the URL is /people/1.xml you
# will get XML back:
# GET /posts/1
# GET /posts/1.xml
def show
@post = Post.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @post }
end
end
283
Custom REST Actions
284
Nested Resources
# Nested resources are used for one-to-many relationships. Suppose we have
# Post class with has_many :comments, and a Comment class with belongs_to :post.
# The posts controller will be the parent of the comments controller. The
# comments controller needs the post model of the parent.
@post = Post.find(params[:post_id])
@comments = @post.comments.find(:all)
end
285
Scaffolding
# In Rails 1.2.X
script/generate scaffold_resource post title:string body:text published:boolean
286
Consuming REST
# You can any HTTP client to consume a REST service, such as curl or wget on the
# command line, or Net::HTTP in your Ruby scripts.
post = Post.find(1)
post.inspect
post.body = new body
post.save
287
Authentication
288
ActionMailer
289
Email Configuration
# In environment.rb or in config/environments/*. Defaults to :smtp
config.action_mailer.delivery_method = :test|:smtp|:sendmail
config.action_mailer.default_charset = iso-8859-1
290
Creating a Mailer
# Creates app/models/statistics_mailer.rb
script/generate mailer StatisticsMailer
291
The Mailer Template
# In app/views/statistics_mailer/monthly.rhtml
Regards
Peter Marklund
292
Sending Email
StatisticsMailer.deliver_monthly(user, stats)
293
Multipart Emails
# If we have several templates with the naming convention
# mail_name.content.type.rhtml then each such template
# will be added as a part in the email.
monthly.text.plain.rhtml
monthly.text.html.rhtml
monthly.text.xml.rhtml
294
Attachments
class ApplicationMailer < ActionMailer::Base
def signup_notification(recipient)
recipients recipient.email_address_with_name
subject "New account information"
from "[email protected]"
295
Receiving Email
class BulkReceiver < ActionMailer::Base
def receive(email)
return unless email.content_type == "multipart/report"
bounce = BouncedDelivery.from_email(email)
msg = Delivery.find_by_message_id(bounce.original_message_id)
msg.status_code = bounce.status_code
msg.status = 'bounced' if bounce.status =~ /^bounced/
msg.save!
end
end
class ActionMailer::Base
...
def receive(raw_email)
logger.info "Received mail:\n #{raw_email}" unless logger.nil?
mail = TMail::Mail.parse(raw_email)
mail.base64_decode
new.receive(mail)
end
296
Unit Testing Mailers
def setup
ActionMailer::Base.delivery_method = :test
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.deliveries = []
@expected = TMail::Mail.new
@expected.set_content_type "text", "plain", { "charset" => CHARSET }
@expected.mime_version = '1.0'
end
def test_monthly
user = users(:gordon)
@expected.subject = StatisticsMailer::MONTHLY_SUBJECT
@expected.from = StatisticsMailer::FROM_EMAIL
@expected_admin_url = "#{Voxway::TAX_URL}/admin/statistics"
@expected.body = ERB.new(read_fixture('monthly').join).result(binding)
@expected.date = Time.now.beginning_of_year
@expected.to = user.email
@expected.bcc = StatisticsMailer::BCC_EMAILS
assert_equal @expected.encoded.strip,
StatisticsMailer.create_monthly(user, stats, @expected.date).encoded.strip
end
297
Plugins
298
Plugins Introduction
299
Finding Plugins
300
Creating Plugins
302
The Rails Module
Inclusion Pattern
module MyPlugin
def self.included(base)
base.extend ClassMethods
base.send :include, InstanceMethods
end
module InstanceMethods
...
end
module ClassMethods
...
end
end
class ActiveRecord::Base
include MyPlugin
end
303
ActiveSupport
304
ActiveSupport
Introduction
305
to_xml, to_json
u = User.find(1)
# Those methods are also available in Struct objects
puts u.to_xml
puts u.to_json # Creates a JavaScript hash
306
Enumerations
us_states = State.find(:all)
state_lookup = us_states.index_by {|state| state.short_name}
total_orders = Order.find(:all).sum(&:value)
307
String
string = I Go Ruby
puts string.at(2)
pust string.from(5)
puts string.to(3)
puts string.first(4)
puts string.last(4)
puts string.starts_with?(I)
puts string.ends_with?(Perl)
count = Hash.new(0)
string.each_char {|ch| count[ch] += 1}
person.pluralize
people.singularize
first_name.humanize # => First Name
i go ruby.titleize # => I Go Ruby
308
Numbers
20.bytes
20.megabytes
20.seconds
20.hours
20.months
20.years
20.minutes.ago
20.weeks.from_now
20.minutes.until(2007-12-01 12:00.to_time)
309
Time and Date
310
UTF8
"".size
=> 6
"".chars.size
=> 3
"".upcase
=> ""
"".chars.upcase.inspect
=> #<ActiveSupport::Multibyte::Chars:0x33589c0 @string="">
"".chars.upcase.to_s
=> ""
311
Rails 2.0
312
Rails 2.0
No radical changes, mostly polishing
Better REST support
New breakpoint mechanism that works with Ruby
1.8.5
HTTP authentication support
HTTP performance and ActiveRecord caching
Deprecated code is removed
313
Whats New in Edge
Rails: ryandaigle.com
* RESTful URL helper
category_article_url(@category, @article) -> url_for([@category, @article])
* New database rake tasks: db:drop and db:reset
* validates_numericality_of pimped
Now takes args: :greater_than, :equal_to, :less_than, :odd, :even
* A more flexible to_xml
user.to_xml(:except => [:id, :created_at], :include => :posts)
* Object transactions are out but there is a plugin
No more Account.transaction(from, to) do ... end with object state rollback
* Code annotations: FIXME, TODO, OPTIMIZE
List annotations with rake notes
* Accessing custom helpers
By default ApplicationController now has helper :all
* Better organization of environment.rb
You can modularize initialization code and put it under config/initializers
* ActiveRecord caching
ActiveRecord::Base.cache do .. end
* New file extensions: .erb and .builder
314
Deployment
315
Hosting
Most production Rails servers run on Unix -
primarily Linux, but also FreeBSD, OpenSolaris,
Mac OS X etc. You can deploy on Windows, but its
fairly unsupported and doesnt work with Capistrano.
There is a multitude of hosting companies in the US
specialized in Rails hosting such as Engine Yard,
RailsMachine, Slicehost, Rimuhosting. In Europe
there is HostingRails.com, brightbox.co.uk etc.
Virtual Private Servers (VPS), typically on Xen, are
becoming an increasingly popular hosting strategy
316
FastCGI
317
Mongrel
318
Typical Mongrel
Architecture
There is a front-end web server that receives HTTP
requests, such as Apache, Lighttpd, or Nginx. Any
static files under the Rails public directory are served
directly from this web server without touching Rails.
There is a reverse proxy that load balances requests
to the Mongrel servers. For Apache this is
mod_proxy_balancer, for Lighttpd it is typically Pen,
or Pound.
There are one or more servers running multiple
Mongrel servers (called Mongrel clusters).
319
Apache + Mongrel:
Example Installation
Install Apache 2.2 with mod_proxy_balancer
Install MySQL 5
Install Ruby and RubyGems
Install MySQL/Ruby driver
Install Ruby gems: rake, rails, termios, capistrano,
mongrel, mongrel_cluster
There is the deprec gem that can automate all these
installation steps on Ubuntu 6.06
320
Mongrel Alternative:
LiteSpeed
LiteSpeed is a commercial web server that is
optimized for speed and is configuration compatible
with Apache
LiteSpeed has its own API for acting as a Rails
application server so Mongrel is not needed in the
picture anymore
LiteSpeed can handle load balancing and also restart
application servers that are hung
321
Capistrano
Capistrano is a Ruby tool used primarily to deploy
Rails applications to one or more production servers
Capistrano logs in to your production servers with
SSH, updates the production code and restarts the
servers automatically for you
If something goes wrong you can do a rollback
Capistrano is also an excellent generic tool for
running sysadmin related scripts on your servers
322
How to Use Capistrano
gem install -y capistrano # version 1.4.1 as of 12/7 2007
cd path_to_rails_app
cap --apply-to .
rm lib/tasks/capistrano.rake # The rake tasks are deprecated
# Edit deploy.rb
set :application, "community"
set :deploy_to, "/var/www/apps/#{application}"
set :domain, "community.marklunds.com"
set :user, "deploy"
set :repository, "svn+ssh://#{user}@#{domain}#/var/www/apps/marklunds/repos/community"
# First deploy. Here we assume database is created and mongrel cluster config is set up.
cap cold_deploy
323
Capistrano tasks
show_tasks # List all Capistrano tasks
update
update_code
symlink
deploy
update
restart
migrate
deploy_with_migrations
cleanup
diff_from_last_deploy
disable_web
enable_web
rollback
rollback_code
restart
324
Custom Capistrano
Tasks
def mongrel_cluster(command)
"mongrel_rails cluster::#{command} -C #{current_path}/config/mongrel_cluster.yml"
end
desc "Clear out old code trees. Only keep 5 latest releases around"
task :after_deploy do
cleanup
sleep 5
ping_servers
end
325
More Capistrano Tasks
desc "Run the full tests on the deployed app."
task :run_tests do
run "cd #{release_path} && RAILS_ENV=production rake && cat /dev/null > log/test.log"
end
desc "Backup production database to local file. We want backups on several production servers in case one would
crash."
task :backup_db, :roles => :app do
prod = ::ActiveRecord::Base.configurations['production']
326
Capistrano 2.0
327
MySQL and Charsets
328
Gem Dependencies
Its a good idea to have as few external library
dependencies for your Rails app as possible
rake rails:freeze:gems puts Rails under the vendor dir
You can freeze the Gem version of Rails by setting
RAILS_GEM_VERSION in environment.rb
You can make sure gems that your app depend on are
present with declarations like gem RedCloth,
3.0.4 in your environment.rb
329
Deployment Checklist
Be alerted of server errors by installing the
exception_notification plugin
330
Security
SQL Injection - always use bind variables in your SQL
conditions
331
Performance I
Use find with the :include option to avoid the N+1 query
problem
If you loop over objects with many attributes and you only need
a few of them you can specify the ones you need with the :select
option
Monitor your log file for slow or unnecessary queries. You can
use the query_analyzer plugin to get queries analyzed in the log
file. MySQL has a slow query log.
332
Performance II
Use Memcached and the Cached Model or Cache Fu plugins to
cache your database queries.
Use the Rails Analyzer gem to get response time statistics for
your actions and find the slowest ones.
333
Sound Advice
Release early in the project and release often. Just like
your application, your deployment doesnt need to be
perfect from day one. You can start simple and grow into
more sophisticated deployment strategies over time.
334
Resources
335
Recommended Books
Agile Web Development with Rails, 2nd edition, available as PDF
Programming Ruby, 2nd edition, available as PDF. Get Beginning Ruby by Peter
Cooper if you think Programming Ruby is hard to follow. The Ruby Way by Hal
Fulton is a good complement to Programming Ruby. The Ruby Cookbook is similar
and also pretty good. Enterprise Integration with Ruby is good.
If you develop AJAX UIs: Ajax on Rails by Scott Raymond and (less important) RJS
Templates for Rails by Cody Fauser
Rails Recipes by Chad Fowler. Get the Advanced Rails Recipes book as soon as it comes
out later this year. Expect great titles such as Rails Deployment from
pragmaticprogrammer.com so get on their announcement mailing list.
If you have a Java background I recommend Rails for Java Developers by Stuart
Halloway and Justin Gehtland.
Mongrel. Serving, Deploying, and Extending your Ruby Applications by Zed Shaw.
Solid introduction to Mongrel and describes how to set up Mongrel with Apache.
336
Videos and Audio
PeepCode.com - REST, AJAX, Prototype, Capistrano
podcast.rubyonrail.org
podcast.sdruby.com
rubyonrails.org/screencasts
railscasts.com
bestechvideos.com/tag/ruby-on-rails
Code Review with Jamis Buck and Marcel Molina:
mtnwestrubyconf2007.confreaks.com/session10.html
Chad Fowler at skillsmatter: skillsmatter.com/menu/479
337
Weblogs
weblog.rubyonrails.com
loudthinking.com
rubyinside.com
therailsway.com
weblog.jamisbuck.com
errtheblog.com
nubyonrails.com
planetrubyonrails.org
blog.caboo.se
338
Applications and
Frameworks
Rick Olson has written the discussion forum app
Beast, the Blog engine Memphisto, and numerous
plugins. Ricks code tends to reflect Rails best
practices.
You can download the Depot application from the
Agile Web Development with Rails book.
The Hobo framework adds an application layer and
templating language on top of Rails to allow for
faster application development.
339
Parting Words
of Advice
340
Advice
Go with the flow. Stay on the golden path of Rails.
Dont break conventions unless you really have to.
Familiarize yourself with the Rails source code and
tests.
Write tests and give TDD and BDD a serious try
Learn Ruby and master it.
Get involved in the community. Somebody has
probably come across and solved your problem
already.
341