Merge docrails along with the new guides and guides generation code

This commit is contained in:
Pratik Naik 2009-02-06 01:57:02 +00:00
parent bccd2c54b2
commit 96d610553e
108 changed files with 18543 additions and 9 deletions

1
.gitignore vendored
View File

@ -14,6 +14,7 @@ railties/pkg
railties/test/500.html
railties/doc/guides/html/images
railties/doc/guides/html/stylesheets
railties/guides/output
*.rbc
*.swp
*.swo

View File

@ -1090,6 +1090,22 @@ module ActiveRecord
# but it in fact generates a join table name of "paper_boxes_papers". Be aware of this caveat, and use the
# custom <tt>:join_table</tt> option if you need to.
#
# The join table should not have a primary key or a model associated with it. You must manually generate the
# join table with a migration such as this:
#
# class CreateDevelopersProjectsJoinTable < ActiveRecord::Migration
# def self.up
# create_table :developers_projects, :id => false do |t|
# t.integer :developer_id
# t.integer :project_id
# end
# end
#
# def self.down
# drop_table :developers_projects
# end
# end
#
# Deprecated: Any additional fields added to the join table will be placed as attributes when pulling records out through
# +has_and_belongs_to_many+ associations. Records returned from join tables with additional attributes will be marked as
# readonly (because we can't save changes to the additional attributes). It's strongly recommended that you upgrade any

View File

@ -244,6 +244,11 @@ def copy_with_rewritten_ruby_path(src_file, dest_file)
end
end
desc 'Generate guides (for authors), use ONLY=foo to process just "foo.textile"'
task :guides do
ruby "guides/rails_guides.rb"
end
# Generate documentation ------------------------------------------------------------------

View File

@ -0,0 +1,188 @@
/* Unobtrustive Code Highlighter By Dan Webb 11/2005
Version: 0.4
Usage:
Add a script tag for this script and any stylesets you need to use
to the page in question, add correct class names to CODE elements,
define CSS styles for elements. That's it!
Known to work on:
IE 5.5+ PC
Firefox/Mozilla PC/Mac
Opera 7.23 + PC
Safari 2
Known to degrade gracefully on:
IE5.0 PC
Note: IE5.0 fails due to the use of lookahead in some stylesets. To avoid script errors
in older browsers use expressions that use lookahead in string format when defining stylesets.
This script is inspired by star-light by entirely cunning Dean Edwards
http://dean.edwards.name/star-light/.
*/
// replace callback support for safari.
if ("a".replace(/a/, function() {return "b"}) != "b") (function(){
var default_replace = String.prototype.replace;
String.prototype.replace = function(search,replace){
// replace is not function
if(typeof replace != "function"){
return default_replace.apply(this,arguments)
}
var str = "" + this;
var callback = replace;
// search string is not RegExp
if(!(search instanceof RegExp)){
var idx = str.indexOf(search);
return (
idx == -1 ? str :
default_replace.apply(str,[search,callback(search, idx, str)])
)
}
var reg = search;
var result = [];
var lastidx = reg.lastIndex;
var re;
while((re = reg.exec(str)) != null){
var idx = re.index;
var args = re.concat(idx, str);
result.push(
str.slice(lastidx,idx),
callback.apply(null,args).toString()
);
if(!reg.global){
lastidx += RegExp.lastMatch.length;
break
}else{
lastidx = reg.lastIndex;
}
}
result.push(str.slice(lastidx));
return result.join("")
}
})();
var CodeHighlighter = { styleSets : new Array };
CodeHighlighter.addStyle = function(name, rules) {
// using push test to disallow older browsers from adding styleSets
if ([].push) this.styleSets.push({
name : name,
rules : rules,
ignoreCase : arguments[2] || false
})
function setEvent() {
// set highlighter to run on load (use LowPro if present)
if (typeof Event != 'undefined' && typeof Event.onReady == 'function')
return Event.onReady(CodeHighlighter.init.bind(CodeHighlighter));
var old = window.onload;
if (typeof window.onload != 'function') {
window.onload = function() { CodeHighlighter.init() };
} else {
window.onload = function() {
old();
CodeHighlighter.init();
}
}
}
// only set the event when the first style is added
if (this.styleSets.length==1) setEvent();
}
CodeHighlighter.init = function() {
if (!document.getElementsByTagName) return;
if ("a".replace(/a/, function() {return "b"}) != "b") return; // throw out Safari versions that don't support replace function
// throw out older browsers
var codeEls = document.getElementsByTagName("CODE");
// collect array of all pre elements
codeEls.filter = function(f) {
var a = new Array;
for (var i = 0; i < this.length; i++) if (f(this[i])) a[a.length] = this[i];
return a;
}
var rules = new Array;
rules.toString = function() {
// joins regexes into one big parallel regex
var exps = new Array;
for (var i = 0; i < this.length; i++) exps.push(this[i].exp);
return exps.join("|");
}
function addRule(className, rule) {
// add a replace rule
var exp = (typeof rule.exp != "string")?String(rule.exp).substr(1, String(rule.exp).length-2):rule.exp;
// converts regex rules to strings and chops of the slashes
rules.push({
className : className,
exp : "(" + exp + ")",
length : (exp.match(/(^|[^\\])\([^?]/g) || "").length + 1, // number of subexps in rule
replacement : rule.replacement || null
});
}
function parse(text, ignoreCase) {
// main text parsing and replacement
return text.replace(new RegExp(rules, (ignoreCase)?"gi":"g"), function() {
var i = 0, j = 1, rule;
while (rule = rules[i++]) {
if (arguments[j]) {
// if no custom replacement defined do the simple replacement
if (!rule.replacement) return "<span class=\"" + rule.className + "\">" + arguments[0] + "</span>";
else {
// replace $0 with the className then do normal replaces
var str = rule.replacement.replace("$0", rule.className);
for (var k = 1; k <= rule.length - 1; k++) str = str.replace("$" + k, arguments[j + k]);
return str;
}
} else j+= rule.length;
}
});
}
function highlightCode(styleSet) {
// clear rules array
var parsed, clsRx = new RegExp("(\\s|^)" + styleSet.name + "(\\s|$)");
rules.length = 0;
// get stylable elements by filtering out all code elements without the correct className
var stylableEls = codeEls.filter(function(item) { return clsRx.test(item.className) });
// add style rules to parser
for (var className in styleSet.rules) addRule(className, styleSet.rules[className]);
// replace for all elements
for (var i = 0; i < stylableEls.length; i++) {
// EVIL hack to fix IE whitespace badness if it's inside a <pre>
if (/MSIE/.test(navigator.appVersion) && stylableEls[i].parentNode.nodeName == 'PRE') {
stylableEls[i] = stylableEls[i].parentNode;
parsed = stylableEls[i].innerHTML.replace(/(<code[^>]*>)([^<]*)<\/code>/i, function() {
return arguments[1] + parse(arguments[2], styleSet.ignoreCase) + "</code>"
});
parsed = parsed.replace(/\n( *)/g, function() {
var spaces = "";
for (var i = 0; i < arguments[1].length; i++) spaces+= "&nbsp;";
return "\n" + spaces;
});
parsed = parsed.replace(/\t/g, "&nbsp;&nbsp;&nbsp;&nbsp;");
parsed = parsed.replace(/\n(<\/\w+>)?/g, "<br />$1").replace(/<br \/>[\n\r\s]*<br \/>/g, "<p><br></p>");
} else parsed = parse(stylableEls[i].innerHTML, styleSet.ignoreCase);
stylableEls[i].innerHTML = parsed;
}
}
// run highlighter on all stylesets
for (var i=0; i < this.styleSets.length; i++) {
highlightCode(this.styleSets[i]);
}
}

View File

@ -0,0 +1,8 @@
function guideMenu(){
if (document.getElementById('guides').style.display == "none") {
document.getElementById('guides').style.display = "block";
} else {
document.getElementById('guides').style.display = "none";
}
}

View File

@ -0,0 +1,90 @@
CodeHighlighter.addStyle("css", {
comment : {
exp : /\/\*[^*]*\*+([^\/][^*]*\*+)*\//
},
keywords : {
exp : /@\w[\w\s]*/
},
selectors : {
exp : "([\\w-:\\[.#][^{};>]*)(?={)"
},
properties : {
exp : "([\\w-]+)(?=\\s*:)"
},
units : {
exp : /([0-9])(em|en|px|%|pt)\b/,
replacement : "$1<span class=\"$0\">$2</span>"
},
urls : {
exp : /url\([^\)]*\)/
}
});
CodeHighlighter.addStyle("ruby",{
comment : {
exp : /#[^\n]+/
},
brackets : {
exp : /\(|\)/
},
string : {
exp : /'[^']*'|"[^"]*"/
},
keywords : {
exp : /\b(do|end|self|class|def|if|module|yield|then|else|for|until|unless|while|elsif|case|when|break|retry|redo|rescue|require|raise)\b/
},
/* Added by Shelly Fisher (shelly@agileevolved.com) */
symbol : {
exp : /([^:])(:[A-Za-z0-9_!?]+)/
},
ivar : {
exp : /\@[A-Za-z0-9_!?]+/
}
});
CodeHighlighter.addStyle("html", {
comment : {
exp: /&lt;!\s*(--([^-]|[\r\n]|-[^-])*--\s*)&gt;/
},
tag : {
exp: /(&lt;\/?)([a-zA-Z1-9]+\s?)/,
replacement: "$1<span class=\"$0\">$2</span>"
},
string : {
exp : /'[^']*'|"[^"]*"/
},
attribute : {
exp: /\b([a-zA-Z-:]+)(=)/,
replacement: "<span class=\"$0\">$1</span>$2"
},
doctype : {
exp: /&lt;!DOCTYPE([^&]|&[^g]|&g[^t])*&gt;/
}
});
CodeHighlighter.addStyle("javascript",{
comment : {
exp : /(\/\/[^\n]*(\n|$))|(\/\*[^*]*\*+([^\/][^*]*\*+)*\/)/
},
brackets : {
exp : /\(|\)/
},
string : {
exp : /'[^']*'|"[^"]*"/
},
keywords : {
exp : /\b(arguments|break|case|continue|default|delete|do|else|false|for|function|if|in|instanceof|new|null|return|switch|this|true|typeof|var|void|while|with)\b/
},
global : {
exp : /\b(toString|valueOf|window|element|prototype|constructor|document|escape|unescape|parseInt|parseFloat|setTimeout|clearTimeout|setInterval|clearInterval|NaN|isNaN|Infinity)\b/
}
});
CodeHighlighter.addStyle("yaml", {
keyword : {
exp : /\/\*[^*]*\*+([^\/][^*]*\*+)*\//
},
value : {
exp : /@\w[\w\s]*/
},
});

View File

@ -0,0 +1,436 @@
/* Guides.rubyonrails.org */
/* Main.css */
/* Created January 30, 2009 */
/* Modified January 31, 2009
--------------------------------------- */
/* General
--------------------------------------- */
.left {float: left; margin-right: 1em;}
.right {float: right; margin-left: 1em;}
.small {font-size: smaller;}
.large {font-size: larger;}
.hide {display: none;}
li ul, li ol { margin:0 1.5em; }
ul, ol { margin: 0 1.5em 1.5em 1.5em; }
ul { list-style-type: disc; }
ol { list-style-type: decimal; }
dl { margin: 0 0 1.5em 0; }
dl dt { font-weight: bold; }
dd { margin-left: 1.5em;}
pre,code { margin: 1.5em 0; white-space: pre; }
pre,code,tt { font: 1em 'andale mono', 'lucida console', monospace; line-height: 1.5; }
abbr, acronym { border-bottom: 1px dotted #666; }
address { margin: 0 0 1.5em; font-style: italic; }
del { color:#666; }
blockquote { margin: 1.5em; color: #666; font-style: italic; }
strong { font-weight: bold; }
em, dfn { font-style: italic; }
dfn { font-weight: bold; }
sup, sub { line-height: 0; }
p {margin: 0 0 1.5em;}
label { font-weight: bold; }
fieldset { padding:1.4em; margin: 0 0 1.5em 0; border: 1px solid #ccc; }
legend { font-weight: bold; font-size:1.2em; }
input.text, input.title,
textarea, select {
margin:0.5em 0;
border:1px solid #bbb;
}
table {
margin: 1em 0;
border: 1px solid #ddd;
background: #f4f4f4;
border-spacing: 0;
}
table th, table td {
padding: 0.25em;
border-right: 1px dotted #e0e0e0;
border-bottom: 1px dotted #e0e0e0;
}
table th:last-child, table td:last-child {
border-right: none;
}
table th {
border-bottom: 1px solid #ddd;
background: #f0f0f0;
font-weight: bold;
}
table td {
}
table tt {
padding: 0.1em;
}
/* Structure and Layout
--------------------------------------- */
body {
text-align: center;
font-family: Helvetica, Arial, sans-serif;
font-size: 87.5%;
line-height: 1.5em;
background: #222;
color: #999;
}
.wrapper {
text-align: left;
margin: 0 auto;
width: 69em;
}
#topNav {
padding: 1em 0;
color: #565656;
}
#header {
background: #c52f24 url(../../images/header_tile.gif) repeat-x;
color: #FFF;
padding: 1.5em 0;
position: relative;
z-index: 99;
}
#feature {
background: #d5e9f6 url(../../images/feature_tile.gif) repeat-x;
color: #333;
padding: 0.5em 0 1.5em;
}
#container {
background: #FFF;
color: #333;
padding: 0.5em 0 1.5em 0;
}
#mainCol {
width: 45em;
margin-left: 2em;
}
#subCol {
position: absolute;
z-index: 0;
top: 0;
right: 0;
background: #FFF;
padding: 1em 1.5em 1em 1.25em;
width: 17em;
font-size: 0.9285em;
line-height: 1.3846em;
}
#extraCol {display: none;}
#footer {
padding: 2em 0;
background: url(../../images/footer_tile.gif) repeat-x;
}
#footer .wrapper {
padding-left: 2em;
width: 67em;
}
#header .wrapper, #topNav .wrapper, #feature .wrapper {padding-left: 1em; width: 68em;}
#feature .wrapper {width: 45em; padding-right: 23em; position: relative; z-index: 0;}
/* Links
--------------------------------------- */
a, a:link, a:visited {
color: #ee3f3f;
text-decoration: underline;
}
#mainCol a, #subCol a {color: #980905;}
/* Navigation
--------------------------------------- */
.nav {margin: 0; padding: 0;}
.nav li {display: inline; list-style: none;}
#header .nav {
float: right;
margin-top: 1.5em;
font-size: 1.2857em;
}
#header .nav li {margin: 0 0 0 0.5em;}
#header .nav a {color: #FFF; text-decoration: none;}
#header .nav a:hover {text-decoration: underline;}
#header .nav .index {
padding: 0.5em 1.5em;
border-radius: 1em;
-webkit-border-radius: 1em;
-moz-border-radius: 1em;
background: #980905;
position: relative;
}
#header .nav .index a {
background: #980905 url(../../images/nav_arrow.gif) no-repeat right top;
padding-right: 1em;
position: relative;
z-index: 15;
padding-bottom: 0.125em;
}
#header .nav .index:hover a, #header .nav .index a:hover {background-position: right -81px;}
#guides {
width: 27em;
display: block;
background: #980905;
border-radius: 1em;
-webkit-border-radius: 1em;
-moz-border-radius: 1em;
-webkit-box-shadow: 0.25em 0.25em 1em rgba(0,0,0,0.25);
-moz-box-shadow: rgba(0,0,0,0.25) 0.25em 0.25em 1em;
color: #f1938c;
padding: 1.5em 2em;
position: absolute;
z-index: 10;
top: -0.25em;
right: 0;
padding-top: 2em;
}
#guides dt, #guides dd {
font-weight: normal;
font-size: 0.722em;
margin: 0;
padding: 0;
}
#guides dt {padding:0; margin: 0.5em 0 0;}
#guides a {color: #FFF; background: none !important;}
#guides .L, #guides .R {float: left; width: 50%; margin: 0; padding: 0;}
#guides .R {float: right;}
#guides hr {
display: block;
border: none;
height: 1px;
color: #f1938c;
background: #f1938c;
}
/* Headings
--------------------------------------- */
h1 {
font-size: 2.5em;
line-height: 1em;
margin: 0.6em 0 .2em;
font-weight: bold;
}
h2 {
font-size: 2.1428em;
line-height: 1em;
margin: 0.7em 0 .2333em;
font-weight: bold;
}
h3 {
font-size: 1.7142em;
line-height: 1.286em;
margin: 0.875em 0 0.2916em;
font-weight: bold;
}
h4 {
font-size: 1.2857em;
line-height: 1.2em;
margin: 1.6667em 0 .3887em;
font-weight: bold;
}
h5 {
font-size: 1em;
line-height: 1.5em;
margin: 1em 0 .5em;
font-weight: bold;
}
h6 {
font-size: 1em;
line-height: 1.5em;
margin: 1em 0 .5em;
font-weight: normal;
}
/* Content
--------------------------------------- */
.pic {
margin: 0 2em 2em 0;
}
#topNav strong {color: #999; margin-right: 0.5em;}
#topNav strong a {color: #FFF;}
#header h1 {
float: left;
background: url(../../images/ruby_guides_logo.gif) no-repeat;
width: 492px;
text-indent: -9999em;
margin: 0;
padding: 0;
}
#header h1 a {
text-decoration: none;
display: block;
height: 77px;
}
#feature p {
font-size: 1.2857em;
margin-bottom: 0.75em;
}
#feature ul {margin-left: 0;}
#feature ul li {
list-style: none;
background: url(../../images/check_bullet.gif) no-repeat left 0.5em;
padding: 0.5em 1.75em 0.5em 1.75em;
font-size: 1.1428em;
font-weight: bold;
}
#mainCol dd, #subCol dd {
padding: 0.25em 0 1em;
border-bottom: 1px solid #CCC;
margin-bottom: 1em;
margin-left: 0;
padding-left: 28px;
}
#mainCol dt, #subCol dt {
font-size: 1.2857em;
padding: 0.125em 0 0.25em 28px;
margin-bottom: 0;
background: url(../../images/book_icon.gif) no-repeat left top;
}
#mainCol dd.ticket, #subCol dd.ticket {
background: #fff9d8 url(../../images/tab_yellow.gif) no-repeat left top;
border: none;
padding: 1.25em 1em 1.25em 48px;
margin-left: 0;
margin-top: 0.25em;
}
#mainCol dd.warning, #subCol dd.warning {
background: #f9d9d8 url(../../images/tab_red.gif) no-repeat left top;
border: none;
padding: 1.25em 1.25em 1.25em 48px;
margin-left: 0;
margin-top: 0.25em;
}
#subCol .chapters {color: #980905;}
#subCol .chapters a {font-weight: bold;}
#subCol .chapters ul a {font-weight: normal;}
#subCol .chapters li {margin-bottom: 0.75em;}
#subCol h3.chapter {margin-top: 0.25em;}
#subCol h3.chapter img {vertical-align: text-bottom;}
#subCol .chapters ul {margin-left: 0; margin-top: 0.5em;}
#subCol .chapters ul li {
list-style: none;
padding: 0 0 0 1em;
background: url(../../images/bullet.gif) no-repeat left 0.45em;
margin-left: 0;
font-size: 1em;
font-weight: normal;
}
tt {
background: #EEE;
border: 1px solid #CCC;
padding: 0.25em 0.5em;
font-family: monaco, "Bitstream Vera Sans Mono", "Courier New", courier, monospace;
}
code, pre {
font-family: monaco, "Bitstream Vera Sans Mono", "Courier New", courier, monospace;
background: #EEE url(../../images/tab_grey.gif) no-repeat left top;
border: none;
padding: 0.25em 1em 0.5em 48px;
margin-left: 0;
margin-top: 0.25em;
display: block;
}
.note {
background: #fff9d8 url(../../images/tab_note.gif) no-repeat left top;
border: none;
padding: 1em 1em 0.25em 48px;
margin-left: 0;
margin-top: 0.25em;
}
.info {
background: #d5e9f6 url(../../images/tab_info.gif) no-repeat left top;
border: none;
padding: 1em 1em 0.25em 48px;
margin-left: 0;
margin-top: 0.25em;
}
.warning {
background: #f9d9d8 url(../../images/tab_red.gif) no-repeat left top;
border: none;
padding: 1em 1em 0.25em 48px;
margin-left: 0;
margin-top: 0.25em;
}
.warning tt, .note tt, .info tt {border:none; background: none; padding: 0;}
em.highlight {
background: #fffcdb;
padding: 0 0.25em;
}
#mainCol ul li {
list-style:none;
background: url(../../images/grey_bullet.gif) no-repeat left 0.5em;
padding-left: 1em;
margin-left: 0;
}
/* Clearing
--------------------------------------- */
.clearfix:after {
content: ".";
display: block;
height: 0;
clear: both;
visibility: hidden;
}
.clearfix {display: inline-block;}
* html .clearfix {height: 1%;}
.clearfix {display: block;}
.clear { clear:both; }

View File

@ -0,0 +1,52 @@
/* Guides.rubyonrails.org */
/* Print.css */
/* Created January 30, 2009 */
/* Modified January 31, 2009
--------------------------------------- */
body, .wrapper, .note, .info, code, #topNav, .L, .R, #frame, #container, #header, #navigation, #footer, #feature, #mainCol, #subCol, #extraCol, .content {position: static; text-align: left; text-indent: 0; background: White; color: Black; border-color: Black; width: auto; height: auto; display: block; float: none; min-height: 0; margin: 0; padding: 0;}
body {
background: #FFF;
font-size: 10pt !important;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
line-height: 1.5;
color: #000;
padding: 0 3%;
}
.hide, .nav {
display: none !important;
}
a:link, a:visited {
background: transparent;
font-weight: bold;
text-decoration: underline;
}
hr {
background:#ccc;
color:#ccc;
width:100%;
height:2px;
margin:2em 0;
padding:0;
border:none;
}
h1,h2,h3,h4,h5,h6 { font-family: "Helvetica Neue", Arial, "Lucida Grande", sans-serif; }
code { font:.9em "Courier New", Monaco, Courier, monospace; }
img { float:left; margin:1.5em 1.5em 1.5em 0; }
a img { border:none; }
blockquote {
margin:1.5em;
padding:1em;
font-style:italic;
font-size:.9em;
}
.small { font-size: .9em; }
.large { font-size: 1.1em; }

View File

@ -0,0 +1,43 @@
/* Guides.rubyonrails.org */
/* Reset.css */
/* Created January 30, 2009
--------------------------------------- */
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, font, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td {
margin: 0;
padding: 0;
border: 0;
outline: 0;
font-size: 100%;
background: transparent;
}
body {line-height: 1; color: black; background: white;}
a img {border:none;}
ins {text-decoration: none;}
del {text-decoration: line-through;}
:focus {
-moz-outline:0;
outline:0;
outline-offset:0;
}
/* tables still need 'cellspacing="0"' in the markup */
table {border-collapse: collapse; border-spacing: 0;}
caption, th, td {text-align: left; font-weight: normal;}
blockquote, q {quotes: none;}
blockquote:before, blockquote:after,
q:before, q:after {
content: '';
content: none;
}

View File

@ -0,0 +1,13 @@
/* Guides.rubyonrails.org */
/* Style.css */
/* Created January 30, 2009
--------------------------------------- */
/*
---------------------------------------
Import advanced style sheet
---------------------------------------
*/
@import url("reset.css");
@import url("main.css");

View File

@ -0,0 +1,31 @@
.html .tag {
color : green;
}
.html .doctype {
color: #708090;
}
.erb .tag {
color : green;
}
.erb .doctype {
color: #708090;
}
.ruby .keywords {
color : red;
}
.ruby .ivar {
color : blue;
}
.ruby .comment {
color: #708090;
}
.ruby .symbol {
color: green;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 337 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 60 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 628 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 384 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 613 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 41 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 43 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 44 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 45 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 62 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 38 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 98 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 38 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 90 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 882 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 44 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 32 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 34 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 34 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 32 KiB

View File

@ -0,0 +1,5 @@
Replaced the plain DocBook XSL admonition icons with Jimmac's DocBook
icons (http://jimmac.musichall.cz/ikony.php3). I dropped transparency
from the Jimmac icons to get round MS IE and FOP PNG incompatibilies.
Stuart Rackham

Binary file not shown.

After

Width:  |  Height:  |  Size: 329 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 361 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 565 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 617 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 623 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 411 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 640 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 353 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 350 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 345 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 348 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 355 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 344 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 357 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 357 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 427 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 83 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 75 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 47 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

View File

@ -0,0 +1,30 @@
pwd = File.dirname(__FILE__)
$: << pwd
$: << File.join(pwd, "../../activesupport/lib")
$: << File.join(pwd, "../../actionpack/lib")
require "action_view"
# Require rubygems after loading Action View
require 'rubygems'
begin
gem 'RedCloth', '= 4.1.1'# Need exactly 4.1.1
rescue Gem::LoadError
$stderr.puts %(Missing the RedCloth 4.1.1 gem.\nPlease `gem install -v=4.1.1 RedCloth` to generate the guides.)
exit 1
end
require 'redcloth'
module RailsGuides
autoload :Generator, "rails_guides/generator"
autoload :Indexer, "rails_guides/indexer"
autoload :Helpers, "rails_guides/helpers"
autoload :TextileExtensions, "rails_guides/textile_extensions"
end
RedCloth.send(:include, RailsGuides::TextileExtensions)
if $0 == __FILE__
RailsGuides::Generator.new.generate
end

View File

@ -0,0 +1,112 @@
module RailsGuides
class Generator
attr_reader :output, :view_path, :view, :guides_dir
def initialize(output = nil)
@guides_dir = File.join(File.dirname(__FILE__), '..')
@output = output || File.join(@guides_dir, "output")
unless ENV["ONLY"]
FileUtils.rm_r(@output) if File.directory?(@output)
FileUtils.mkdir(@output)
end
@view_path = File.join(@guides_dir, "source")
end
def generate
guides = Dir.entries(view_path).find_all {|g| g =~ /textile$/ }
if ENV["ONLY"]
only = ENV["ONLY"].split(",").map{|x| x.strip }.map {|o| "#{o}.textile" }
guides = guides.find_all {|g| only.include?(g) }
puts "GENERATING ONLY #{guides.inspect}"
end
guides.each do |guide|
guide =~ /(.*?)(\.erb)?\.textile/
name = $1
puts "Generating #{name}"
file = File.join(output, "#{name}.html")
File.open(file, 'w') do |f|
@view = ActionView::Base.new(view_path)
@view.extend(Helpers)
if guide =~ /\.erb\.textile/
# Generate the erb pages with textile formatting - e.g. index/authors
result = view.render(:layout => 'layout', :file => guide)
f.write textile(result)
else
body = File.read(File.join(view_path, guide))
body = set_header_section(body, @view)
body = set_index(body, @view)
result = view.render(:layout => 'layout', :text => textile(body))
f.write result
end
end
end
# Copy images and css files to html directory
FileUtils.cp_r File.join(guides_dir, 'images'), File.join(output, 'images')
FileUtils.cp_r File.join(guides_dir, 'files'), File.join(output, 'files')
end
def set_header_section(body, view)
new_body = body.gsub(/(.*?)endprologue\./m, '').strip
header = $1
header =~ /h2\.(.*)/
page_title = $1.strip
header = textile(header)
view.content_for(:page_title) { page_title }
view.content_for(:header_section) { header }
new_body
end
def set_index(body, view)
index = <<-INDEX
<div id="subCol">
<h3 class="chapter"><img src="images/chapters_icon.gif" alt="" /> Chapters</h3>
<ol class="chapters">
INDEX
i = Indexer.new(body)
i.index
# Set index for 2 levels
i.level_hash.each do |key, value|
bookmark = '#' + key.gsub(/[^a-z0-9\-_\+]+/i, '').underscore.dasherize
link = view.content_tag(:a, :href => bookmark) { key }
children = value.keys.map do |k|
bm = '#' + k.gsub(/[^a-z0-9\-_\+]+/i, '').underscore.dasherize
l = view.content_tag(:a, :href => bm) { k }
view.content_tag(:li, l)
end
children_ul = view.content_tag(:ul, children)
index << view.content_tag(:li, link + children_ul)
end
index << '</ol>'
index << '</div>'
view.content_for(:index_section) { index }
i.result
end
def textile(body)
t = RedCloth.new(body)
t.hard_breaks = false
t.to_html(:notestuff, :plusplus, :code, :tip)
end
end
end

View File

@ -0,0 +1,34 @@
module RailsGuides
module Helpers
def guide(name, url, options = {}, &block)
link = content_tag(:a, :href => url) { name }
result = content_tag(:dt, link)
if ticket = options[:ticket]
result << content_tag(:dd, lh(ticket), :class => 'ticket')
end
result << content_tag(:dd, capture(&block))
concat(result)
end
def lh(id, label = "Lighthouse Ticket")
url = "http://rails.lighthouseapp.com/projects/16213/tickets/#{id}"
content_tag(:a, label, :href => url)
end
def author(name, nick, image = 'credits_pic_blank.gif', &block)
image = "images/#{image}"
result = content_tag(:img, nil, :src => image, :class => 'left pic', :alt => name)
result << content_tag(:h3, name)
result << content_tag(:p, capture(&block))
concat content_tag(:div, result, :class => 'clearfix', :id => nick)
end
def code(&block)
c = capture(&block)
content_tag(:code, c)
end
end
end

View File

@ -0,0 +1,51 @@
module RailsGuides
class Indexer
attr_reader :body, :result, :level_hash
def initialize(body)
@body = body
@result = @body.dup
end
def index
@level_hash = process(body)
end
private
def process(string, current_level= 3, counters = [1])
s = StringScanner.new(string)
level_hash = ActiveSupport::OrderedHash.new
while !s.eos?
s.match?(/\h[0-9]\..*$/)
if matched = s.matched
matched =~ /\h([0-9])\.(.*)$/
level, title = $1.to_i, $2
if level < current_level
# This is needed. Go figure.
return level_hash
elsif level == current_level
index = counters.join(".")
bookmark = '#' + title.gsub(/[^a-z0-9\-_\+]+/i, '').underscore.dasherize
raise "Parsing Fail" unless @result.sub!(matched, "h#{level}(#{bookmark}). #{index}#{title}")
# Recurse
counters << 1
level_hash[title.strip] = process(s.post_match, current_level + 1, counters)
counters.pop
# Increment the current level
last = counters.pop
counters << last + 1
end
end
s.getch
end
level_hash
end
end
end

View File

@ -0,0 +1,38 @@
module RailsGuides
module TextileExtensions
def notestuff(body)
body.gsub!(/^(IMPORTANT|CAUTION|WARNING|NOTE|INFO)(?:\.|\:)(.*)$/) do |m|
css_class = $1.downcase
css_class = 'warning' if ['caution', 'important'].include?(css_class)
result = "<div class='#{css_class}'><p>"
result << $2.strip
result << '</p></div>'
result
end
end
def tip(body)
body.gsub!(/^(TIP)\:(.*)$/) do |m|
result = "<div class='info'><p>"
result << $2.strip
result << '</p></div>'
result
end
end
def plusplus(body)
body.gsub!(/\+(.*?)\+/) do |m|
"<notextile><tt>#{$1}</tt></notextile>"
end
end
def code(body)
body.gsub!(/\<(yaml|shell|ruby|erb|html|sql)\>(.*?)\<\/\1\>/m) do |m|
es = ERB::Util.h($2)
css_class = ['erb', 'shell'].include?($1) ? 'html' : $1
"<notextile><code class='#{css_class}'>#{es}\n</code></notextile>"
end
end
end
end

View File

@ -0,0 +1,422 @@
h2. Ruby on Rails 2.2 Release Notes
Rails 2.2 delivers a number of new and improved features. This list covers the major upgrades, but doesn't include every little bug fix and change. If you want to see everything, check out the "list of commits":http://github.com/rails/rails/commits/master in the main Rails repository on GitHub.
Along with Rails, 2.2 marks the launch of the "Ruby on Rails Guides":http://guides.rubyonrails.org/, the first results of the ongoing "Rails Guides hackfest":http://hackfest.rubyonrails.org/guide. This site will deliver high-quality documentation of the major features of Rails.
endprologue.
h3. Infrastructure
Rails 2.2 is a significant release for the infrastructure that keeps Rails humming along and connected to the rest of the world.
h4. Internationalization
Rails 2.2 supplies an easy system for internationalization (or i18n, for those of you tired of typing).
* Lead Contributors: Rails i18 Team
* More information :
** "Official Rails i18 website":http://rails-i18n.org
** "Finally. Ruby on Rails gets internationalized":http://www.artweb-design.de/2008/7/18/finally-ruby-on-rails-gets-internationalized
** "Localizing Rails : Demo application":http://i18n-demo.phusion.nl
h4. Compatibility with Ruby 1.9 and JRuby
Along with thread safety, a lot of work has been done to make Rails work well with JRuby and the upcoming Ruby 1.9. With Ruby 1.9 being a moving target, running edge Rails on edge Ruby is still a hit-or-miss proposition, but Rails is ready to make the transition to Ruby 1.9 when the latter is released.
h3. Documentation
The internal documentation of Rails, in the form of code comments, has been improved in numerous places. In addition, the "Ruby on Rails Guides":http://guides.rubyonrails.org/ project is the definitive source for information on major Rails components. In its first official release, the Guides page includes:
* "Getting Started with Rails":http://guides.rubyonrails.org/getting_started_with_rails.html
* "Rails Database Migrations":http://guides.rubyonrails.org/migrations.html
* "Active Record Associations":http://guides.rubyonrails.org/association_basics.html
* "Active Record Finders":http://guides.rubyonrails.org/finders.html
* "Layouts and Rendering in Rails":http://guides.rubyonrails.org/layouts_and_rendering.html
* "Action View Form Helpers":http://guides.rubyonrails.org/form_helpers.html
* "Rails Routing from the Outside In":http://guides.rubyonrails.org/routing_outside_in.html
* "Basics of Action Controller":http://guides.rubyonrails.org/actioncontroller_basics.html
* "Rails Caching":http://guides.rubyonrails.org/caching_with_rails.html
* "Testing Rails Applications":http://guides.rubyonrails.org/testing_rails_applications.html
* "Securing Rails Applications":http://guides.rubyonrails.org/security.html
* "Debugging Rails Applications":http://guides.rubyonrails.org/debugging_rails_applications.html
* "Benchmarking and Profiling Rails Applications":http://guides.rubyonrails.org/benchmarking_and_profiling.html
* "The Basics of Creating Rails Plugins":http://guides.rubyonrails.org/creating_plugins.html
All told, the Guides provide tens of thousands of words of guidance for beginning and intermediate Rails developers.
If you want to generate these guides locally, inside your application:
<ruby>
rake doc:guides
</ruby>
This will put the guides inside +RAILS_ROOT/doc/guides+ and you may start surfing straight away by opening +RAILS_ROOT/doc/guides/index.html+ in your favourite browser.
* Lead Contributors: "Rails Documentation Team":http://guides.rails.info/credits.html
* Major contributions from "Xavier Noria":http://advogato.org/person/fxn/diary.html and "Hongli Lai":http://izumi.plan99.net/blog/.
* More information:
** "Rails Guides hackfest":http://hackfest.rubyonrails.org/guide
** "Help improve Rails documentation on Git branch":http://weblog.rubyonrails.org/2008/5/2/help-improve-rails-documentation-on-git-branch
h3. Better integration with HTTP : Out of the box ETag support
Supporting the etag and last modified timestamp in HTTP headers means that Rails can now send back an empty response if it gets a request for a resource that hasn't been modified lately. This allows you to check whether a response needs to be sent at all.
<ruby>
class ArticlesController < ApplicationController
def show_with_respond_to_block
@article = Article.find(params[:id])
# If the request sends headers that differs from the options provided to stale?, then
# the request is indeed stale and the respond_to block is triggered (and the options
# to the stale? call is set on the response).
#
# If the request headers match, then the request is fresh and the respond_to block is
# not triggered. Instead the default render will occur, which will check the last-modified
# and etag headers and conclude that it only needs to send a "304 Not Modified" instead
# of rendering the template.
if stale?(:last_modified => @article.published_at.utc, :etag => @article)
respond_to do |wants|
# normal response processing
end
end
end
def show_with_implied_render
@article = Article.find(params[:id])
# Sets the response headers and checks them against the request, if the request is stale
# (i.e. no match of either etag or last-modified), then the default render of the template happens.
# If the request is fresh, then the default render will return a "304 Not Modified"
# instead of rendering the template.
fresh_when(:last_modified => @article.published_at.utc, :etag => @article)
end
end
</ruby>
h3. Thread Safety
The work done to make Rails thread-safe is rolling out in Rails 2.2. Depending on your web server infrastructure, this means you can handle more requests with fewer copies of Rails in memory, leading to better server performance and higher utilization of multiple cores.
To enable multithreaded dispatching in production mode of your application, add the following line in your +config/environments/production.rb+:
<ruby>
config.threadsafe!
</ruby>
* More information :
** "Thread safety for your Rails":http://m.onkey.org/2008/10/23/thread-safety-for-your-rails
** "Thread safety project announcement":http://weblog.rubyonrails.org/2008/8/16/josh-peek-officially-joins-the-rails-core
** "Q/A: What Thread-safe Rails Means":http://blog.headius.com/2008/08/qa-what-thread-safe-rails-means.html
h3. Active Record
There are two big additions to talk about here: transactional migrations and pooled database transactions. There's also a new (and cleaner) syntax for join table conditions, as well as a number of smaller improvements.
h4. Transactional Migrations
Historically, multiple-step Rails migrations have been a source of trouble. If something went wrong during a migration, everything before the error changed the database and everything after the error wasn't applied. Also, the migration version was stored as having been executed, which means that it couldn't be simply rerun by +rake db:migrate:redo+ after you fix the problem. Transactional migrations change this by wrapping migration steps in a DDL transaction, so that if any of them fail, the entire migration is undone. In Rails 2.2, transactional migrations are supported on PostgreSQL out of the box. The code is extensible to other database types in the future - and IBM has already extended it to support the DB2 adapter.
* Lead Contributor: "Adam Wiggins":http://adam.blog.heroku.com/
* More information:
** "DDL Transactions":http://adam.blog.heroku.com/past/2008/9/3/ddl_transactions/
** "A major milestone for DB2 on Rails":http://db2onrails.com/2008/11/08/a-major-milestone-for-db2-on-rails/
h4. Connection Pooling
Connection pooling lets Rails distribute database requests across a pool of database connections that will grow to a maximum size (by default 5, but you can add a +pool+ key to your +database.yml+ to adjust this). This helps remove bottlenecks in applications that support many concurrent users. There's also a +wait_timeout+ that defaults to 5 seconds before giving up. +ActiveRecord::Base.connection_pool+ gives you direct access to the pool if you need it.
<ruby>
development:
adapter: mysql
username: root
database: sample_development
pool: 10
wait_timeout: 10
</ruby>
* Lead Contributor: "Nick Sieger":http://blog.nicksieger.com/
* More information:
** "What's New in Edge Rails: Connection Pools":http://ryandaigle.com/articles/2008/9/7/what-s-new-in-edge-rails-connection-pools
h4. Hashes for Join Table Conditions
You can now specify conditions on join tables using a hash. This is a big help if you need to query across complex joins.
<ruby>
class Photo < ActiveRecord::Base
belongs_to :product
end
class Product < ActiveRecord::Base
has_many :photos
end
# Get all products with copyright-free photos:
Product.all(:joins => :photos, :conditions => { :photos => { :copyright => false }})
</ruby>
* More information:
** "What's New in Edge Rails: Easy Join Table Conditions":http://ryandaigle.com/articles/2008/7/7/what-s-new-in-edge-rails-easy-join-table-conditions
h4. New Dynamic Finders
Two new sets of methods have been added to Active Record's dynamic finders family.
h5. +find_last_by_<attribute>+
The +find_last_by_<attribute>+ method is equivalent to +Model.last(:conditions => {:attribute => value})+
<ruby>
# Get the last user who signed up from London
User.find_last_by_city('London')
</ruby>
* Lead Contributor: "Emilio Tagua":http://www.workingwithrails.com/person/9147-emilio-tagua
h5. +find_by_<attribute>!+
The new bang! version of +find_by_<attribute>!+ is equivalent to +Model.first(:conditions => {:attribute => value}) || raise ActiveRecord::RecordNotFound+ Instead of returning +nil+ if it can't find a matching record, this method will raise an exception if it cannot find a match.
<ruby>
# Raise ActiveRecord::RecordNotFound exception if 'Moby' hasn't signed up yet!
User.find_by_name!('Moby')
</ruby>
* Lead Contributor: "Josh Susser":http://blog.hasmanythrough.com
h4. Associations Respect Private/Protected Scope
Active Record association proxies now respect the scope of methods on the proxied object. Previously (given User has_one :account) +@user.account.private_method+ would call the private method on the associated Account object. That fails in Rails 2.2; if you need this functionality, you should use +@user.account.send(:private_method)+ (or make the method public instead of private or protected). Please note that if you're overriding +method_missing+, you should also override +respond_to+ to match the behavior in order for associations to function normally.
* Lead Contributor: Adam Milligan
* More information:
** "Rails 2.2 Change: Private Methods on Association Proxies are Private":http://afreshcup.com/2008/10/24/rails-22-change-private-methods-on-association-proxies-are-private/
h4. Other ActiveRecord Changes
* +rake db:migrate:redo+ now accepts an optional VERSION to target that specific migration to redo
* Set +config.active_record.timestamped_migrations = false+ to have migrations with numeric prefix instead of UTC timestamp.
* Counter cache columns (for associations declared with +:counter_cache => true+) do not need to be initialized to zero any longer.
* +ActiveRecord::Base.human_name+ for an internationalization-aware humane translation of model names
h3. Action Controller
On the controller side, there are several changes that will help tidy up your routes. There are also some internal changes in the routing engine to lower memory usage on complex applications.
h4. Shallow Route Nesting
Shallow route nesting provides a solution to the well-known difficulty of using deeply-nested resources. With shallow nesting, you need only supply enough information to uniquely identify the resource that you want to work with.
<ruby>
map.resources :publishers, :shallow => true do |publisher|
publisher.resources :magazines do |magazine|
magazine.resources :photos
end
end
</ruby>
This will enable recognition of (among others) these routes:
<ruby>
/publishers/1 ==> publisher_path(1)
/publishers/1/magazines ==> publisher_magazines_path(1)
/magazines/2 ==> magazine_path(2)
/magazines/2/photos ==> magazines_photos_path(2)
/photos/3 ==> photo_path(3)
</ruby>
* Lead Contributor: "S. Brent Faulkner":http://www.unwwwired.net/
* More information:
** "Rails Routing from the Outside In":http://guides.rails.info/routing/routing_outside_in.html#_nested_resources
** "What's New in Edge Rails: Shallow Routes":http://ryandaigle.com/articles/2008/9/7/what-s-new-in-edge-rails-shallow-routes
h4. Method Arrays for Member or Collection Routes
You can now supply an array of methods for new member or collection routes. This removes the annoyance of having to define a route as accepting any verb as soon as you need it to handle more than one. With Rails 2.2, this is a legitimate route declaration:
<ruby>
map.resources :photos, :collection => { :search => [:get, :post] }
</ruby>
* Lead Contributor: "Brennan Dunn":http://brennandunn.com/
h4. Resources With Specific Actions
By default, when you use +map.resources+ to create a route, Rails generates routes for seven default actions (index, show, create, new, edit, update, and destroy). But each of these routes takes up memory in your application, and causes Rails to generate additional routing logic. Now you can use the +:only+ and +:except+ options to fine-tune the routes that Rails will generate for resources. You can supply a single action, an array of actions, or the special +:all+ or +:none+ options. These options are inherited by nested resources.
<ruby>
map.resources :photos, :only => [:index, :show]
map.resources :products, :except => :destroy
</ruby>
* Lead Contributor: "Tom Stuart":http://experthuman.com/
h4. Other Action Controller Changes
* You can now easily "show a custom error page":http://m.onkey.org/2008/7/20/rescue-from-dispatching for exceptions raised while routing a request.
* The HTTP Accept header is disabled by default now. You should prefer the use of formatted URLs (such as +/customers/1.xml+) to indicate the format that you want. If you need the Accept headers, you can turn them back on with +config.action_controller.use_accept_header = true+.
* Benchmarking numbers are now reported in milliseconds rather than tiny fractions of seconds
* Rails now supports HTTP-only cookies (and uses them for sessions), which help mitigate some cross-site scripting risks in newer browsers.
* +redirect_to+ now fully supports URI schemes (so, for example, you can redirect to a svn+ssh: URI).
* +render+ now supports a +:js+ option to render plain vanilla javascript with the right mime type.
* Request forgery protection has been tightened up to apply to HTML-formatted content requests only.
* Polymorphic URLs behave more sensibly if a passed parameter is nil. For example, calling +polymorphic_path([@project, @date, @area])+ with a nil date will give you +project_area_path+.
h3. Action View
* +javascript_include_tag+ and +stylesheet_link_tag+ support a new +:recursive+ option to be used along with +:all+, so that you can load an entire tree of files with a single line of code.
* The included Prototype javascript library has been upgraded to version 1.6.0.3.
* +RJS#page.reload+ to reload the browser's current location via javascript
* The +atom_feed+ helper now takes an +:instruct+ option to let you insert XML processing instructions.
h3. Action Mailer
Action Mailer now supports mailer layouts. You can make your HTML emails as pretty as your in-browser views by supplying an appropriately-named layout - for example, the +CustomerMailer+ class expects to use +layouts/customer_mailer.html.erb+.
* More information:
** "What's New in Edge Rails: Mailer Layouts":http://ryandaigle.com/articles/2008/9/7/what-s-new-in-edge-rails-mailer-layouts
Action Mailer now offers built-in support for GMail's SMTP servers, by turning on STARTTLS automatically. This requires Ruby 1.8.7 to be installed.
h3. Active Support
Active Support now offers built-in memoization for Rails applications, the +each_with_object+ method, prefix support on delegates, and various other new utility methods.
h4. Memoization
Memoization is a pattern of initializing a method once and then stashing its value away for repeat use. You've probably used this pattern in your own applications:
<ruby>
def full_name
@full_name ||= "#{first_name} #{last_name}"
end
</ruby>
Memoization lets you handle this task in a declarative fashion:
<ruby>
extend ActiveSupport::Memoizable
def full_name
"#{first_name} #{last_name}"
end
memoize :full_name
</ruby>
Other features of memoization include +unmemoize+, +unmemoize_all+, and +memoize_all+ to turn memoization on or off.
* Lead Contributor: "Josh Peek":http://joshpeek.com/
* More information:
** "What's New in Edge Rails: Easy Memoization":http://ryandaigle.com/articles/2008/7/16/what-s-new-in-edge-rails-memoization
** "Memo-what? A Guide to Memoization":http://www.railway.at/articles/2008/09/20/a-guide-to-memoization
h4. each_with_object
The +each_with_object+ method provides an alternative to +inject+, using a method backported from Ruby 1.9. It iterates over a collection, passing the current element and the memo into the block.
<ruby>
%w(foo bar).each_with_object({}) { |str, hsh| hsh[str] = str.upcase } #=> {'foo' => 'FOO', 'bar' => 'BAR'}
</ruby>
Lead Contributor: "Adam Keys":http://therealadam.com/
h4. Delegates With Prefixes
If you delegate behavior from one class to another, you can now specify a prefix that will be used to identify the delegated methods. For example:
<ruby>
class Vendor < ActiveRecord::Base
has_one :account
delegate :email, :password, :to => :account, :prefix => true
end
</ruby>
This will produce delegated methods +vendor#account_email+ and +vendor#account_password+. You can also specify a custom prefix:
<ruby>
class Vendor < ActiveRecord::Base
has_one :account
delegate :email, :password, :to => :account, :prefix => :owner
end
</ruby>
This will produce delegated methods +vendor#owner_email+ and +vendor#owner_password+.
Lead Contributor: "Daniel Schierbeck":http://workingwithrails.com/person/5830-daniel-schierbeck
h4. Other Active Support Changes
* Extensive updates to +ActiveSupport::Multibyte+, including Ruby 1.9 compatibility fixes.
* The addition of +ActiveSupport::Rescuable+ allows any class to mix in the +rescue_from+ syntax.
* +past?+, +today?+ and +future?+ for +Date+ and +Time+ classes to facilitate date/time comparisons.
* +Array#second+ through +Array#fifth+ as aliases for +Array#[1]+ through +Array#[4]+
* +Enumerable#many?+ to encapsulate +collection.size > 1+
* +Inflector#parameterize+ produces a URL-ready version of its input, for use in +to_param+.
* +Time#advance+ recognizes fractional days and weeks, so you can do +1.7.weeks.ago+, +1.5.hours.since+, and so on.
* The included TzInfo library has been upgraded to version 0.3.12.
* +ActiveSuport::StringInquirer+ gives you a pretty way to test for equality in strings: +ActiveSupport::StringInquirer.new("abc").abc? => true+
h3. Railties
In Railties (the core code of Rails itself) the biggest changes are in the +config.gems+ mechanism.
h4. config.gems
To avoid deployment issues and make Rails applications more self-contained, it's possible to place copies of all of the gems that your Rails application requires in +/vendor/gems+. This capability first appeared in Rails 2.1, but it's much more flexible and robust in Rails 2.2, handling complicated dependencies between gems. Gem management in Rails includes these commands:
* +config.gem _gem_name_+ in your +config/environment.rb+ file
* +rake gems+ to list all configured gems, as well as whether they (and their dependencies) are installed, frozen, or framework (framework gems are those loaded by Rails before the gem dependency code is executed; such gems cannot be frozen)
* +rake gems:install+ to install missing gems to the computer
* +rake gems:unpack+ to place a copy of the required gems into +/vendor/gems+
* +rake gems:unpack:dependencies+ to get copies of the required gems and their dependencies into +/vendor/gems+
* +rake gems:build+ to build any missing native extensions
* +rake gems:refresh_specs+ to bring vendored gems created with Rails 2.1 into alignment with the Rails 2.2 way of storing them
You can unpack or install a single gem by specifying +GEM=_gem_name_+ on the command line.
* Lead Contributor: "Matt Jones":http://github.com/al2o3cr
* More information:
** "What's New in Edge Rails: Gem Dependencies":http://ryandaigle.com/articles/2008/4/1/what-s-new-in-edge-rails-gem-dependencies
** "Rails 2.1.2 and 2.2RC1: Update Your RubyGems":http://afreshcup.com/2008/10/25/rails-212-and-22rc1-update-your-rubygems/
** "Detailed discussion on Lighthouse":http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/1128
h4. Other Railties Changes
* If you're a fan of the "Thin":http://code.macournoyer.com/thin/ web server, you'll be happy to know that +script/server+ now supports Thin directly.
* +script/plugin install <plugin> -r <revision>+ now works with git-based as well as svn-based plugins.
* +script/console+ now supports a +--debugger+ option
* Instructions for setting up a continuous integration server to build Rails itself are included in the Rails source
* +rake notes:custom ANNOTATION=MYFLAG+ lets you list out custom annotations.
* Wrapped +Rails.env+ in +StringInquirer+ so you can do +Rails.env.development?+
* To eliminate deprecation warnings and properly handle gem dependencies, Rails now requires rubygems 1.3.1 or higher.
h3. Deprecated
A few pieces of older code are deprecated in this release:
* +Rails::SecretKeyGenerator+ has been replaced by +ActiveSupport::SecureRandom+
* +render_component+ is deprecated. There's a "render_components plugin":http://github.com/rails/render_component/tree/master available if you need this functionality.
* Implicit local assignments when rendering partials has been deprecated.
<ruby>
def partial_with_implicit_local_assignment
@customer = Customer.new("Marcel")
render :partial => "customer"
end
</ruby>
Previously the above code made available a local variable called +customer+ inside the partial 'customer'. You should explicitly pass all the variables via :locals hash now.
* +country_select+ has been removed. See the "deprecation page":http://www.rubyonrails.org/deprecation/list-of-countries for more information and a plugin replacement.
* +ActiveRecord::Base.allow_concurrency+ no longer has any effect.
* +ActiveRecord::Errors.default_error_messages+ has been deprecated in favor of +I18n.translate('activerecord.errors.messages')+
* The +%s+ and +%d+ interpolation syntax for internationalization is deprecated.
* +String#chars+ has been deprecated in favor of +String#mb_chars+.
* Durations of fractional months or fractional years are deprecated. Use Ruby's core +Date+ and +Time+ class arithmetic instead.
* +Request#relative_url_root+ is deprecated. Use +ActionController::Base.relative_url_root+ instead.
h3. Credits
Release notes compiled by "Mike Gunderloy":http://afreshcup.com

View File

@ -0,0 +1,501 @@
h2. Ruby on Rails 2.3 Release Notes
Rails 2.3 delivers a variety of new and improved features, including pervasive Rack integration, refreshed support for Rails Engines, nested transactions for Active Record, dynamic and default scopes, unified rendering, more efficient routing, application templates, and quiet backtraces. This list covers the major upgrades, but doesn't include every little bug fix and change. If you want to see everything, check out the "list of commits":http://github.com/rails/rails/commits/master in the main Rails repository on GitHub or review the +CHANGELOG+ files for the individual Rails components.
endprologue.
h3. Application Architecture
There are two major changes in the architecture of Rails applications: complete integration of the "Rack":http://rack.rubyforge.org/ modular web server interface, and renewed support for Rails Engines.
h4. Rack Integration
Rails has now broken with its CGI past, and uses Rack everywhere. This required and resulted in a tremendous number of internal changes (but if you use CGI, don't worry; Rails now supports CGI through a proxy interface.) Still, this is a major change to Rails internals. After upgrading to 2.3, you should test on your local environment and your production environment. Some things to test:
* Sessions
* Cookies
* File uploads
* JSON/XML APIs
Here's a summary of the rack-related changes:
* +script/server+ has been switched to use Rack, which means it supports any Rack compatible server. +script/server+ will also pick up a rackup configuration file if one exists. By default, it will look for a +config.ru+ file, but you can override this with the +-c+ switch.
* The FCGI handler goes through Rack
* +ActionController::Dispatcher+ maintains its own default middleware stack. Middlewares can be injected in, reordered, and removed. The stack is compiled into a chain on boot. You can configure the middleware stack in +environment.rb+
* The +rake middleware+ task has been added to inspect the middleware stack. This is useful for debugging the order of the middleware stack.
* The integration test runner has been modified to execute the entire middleware and application stack. This makes integration tests perfect for testing Rack middleware.
* +ActionController::CGIHandler+ is a backwards compatible CGI wrapper around Rack. The +CGIHandler+ is meant to take an old CGI object and convert its environment information into a Rack compatible form.
* +CgiRequest+ and +CgiResponse+ have been removed
* Session stores are now lazy loaded. If you never access the session object during a request, it will never attempt to load the session data (parse the cookie, load the data from memcache, or lookup an Active Record object).
* +CGI::Session::CookieStore+ has been replaced by +ActionController::Session::CookieStore+
* +CGI::Session::MemCacheStore+ has been replaced by +ActionController::Session::MemCacheStore+
* +CGI::Session::ActiveRecordStore+ has been replaced by +ActiveRecord::SessionStore+
* You can still change your session store with +ActionController::Base.session_store = :active_record_store+
* Default sessions options are still set with +ActionController::Base.session = { :key => "..." }+
* The mutex that normally wraps your entire request has been moved into middleware, +ActionController::Lock+
* +ActionController::AbstractRequest+ and +ActionController::Request+ have been unified. The new +ActionController::Request+ inherits from +Rack::Request+. This affects access to +response.headers['type']+ in test requests. Use +response.content_type+ instead.
* +ActiveRecord::QueryCache+ middleware is automatically inserted onto the middleware stack if +ActiveRecord+ has been loaded. This middleware sets up and flushes the per-request Active Record query cache.
* The Rails router and controller classes follow the Rack spec. You can call a controller directly with +SomeController.call(env)+. The router stores the routing parameters in +rack.routing_args+.
* +ActionController::Request+ inherits from +Rack::Request+
* Instead of +config.action_controller.session = { :session_key => 'foo', ...+ use +config.action_controller.session = { :key => 'foo', ...+
* Using the +ParamsParser+ middleware preprocesses any XML, JSON, or YAML requests so they can be read normally with any +Rack::Request+ object after it.
h4. Renewed Support for Rails Engines
After some versions without an upgrade, Rails 2.3 offers some new features for Rails Engines (Rails applications that can be embedded within other applications). First, routing files in engines are automatically loaded and reloaded now, just like your +routes.rb+ file (this also applies to routing files in other plugins). Second, if your plugin has an app folder, then app/[models|controllers|helpers] will automatically be added to the Rails load path. Engines also support adding view paths now.
h3. Documentation
The "Ruby on Rails guides":http://guides.rubyonrails.org/ project has published several additional guides for Rails 2.3. In addition, a "separate site":http://guides.rails.info/ maintains updated copies of the Guides for Edge Rails. Other documentation efforts include a relaunch of the "Rails wiki":http://newwiki.rubyonrails.org/ and early planning for a Rails Book.
* More Information: "Rails Documentation Projects":http://weblog.rubyonrails.org/2009/1/15/rails-documentation-projects
h3. Active Record
Active Record gets quite a number of new features and bug fixes in Rails 2.3. The highlights include nested attributes, nested transactions, dynamic scopes, and default scopes.
h4. Nested Attributes
Active Record can now update the attributes on nested models directly, provided you tell it to do so:
<ruby>
class Book < ActiveRecord::Base
has_one :author
has_many :pages
accepts_nested_attributes_for :author, :pages
end
</ruby>
Turning on nested attributes enables a number of things: automatic (and atomic) saving of a record together with its associated children, child-aware validations, and support for nested forms (discussed later).
* Lead Contributor: "Eloy Duran":http://www.superalloy.nl/blog/
* More Information: "Nested Model Forms":http://weblog.rubyonrails.org/2009/1/26/nested-model-forms
h4. Nested Transactions
Active Record now supports nested transactions, a much-requested feature. Now you can write code like this:
<ruby>
User.transaction do
User.create(:username => 'Admin')
User.transaction(:requires_new => true) do
User.create(:username => 'Regular')
raise ActiveRecord::Rollback
end
end
User.find(:all) # => Returns only Admin
</ruby>
Nested transactions let you roll back an inner transaction without affecting the state of the outer transaction. If you want a transaction to be nested, you must explicitly add the +:requires_new+ option; otherwise, a nested transaction simply becomes part of the parent transaction (as it does currently on Rails 2.2). Under the covers, nested transactions are "using savepoints":http://rails.lighthouseapp.com/projects/8994/tickets/383, so they're supported even on databases that don't have true nested transactions. There is also a bit of magic going on to make these transactions play well with transactional fixtures during testing.
* Lead Contributors: "Jonathan Viney":http://www.workingwithrails.com/person/4985-jonathan-viney and "Hongli Lai":http://izumi.plan99.net/blog/
h4. Dynamic Scopes
You know about dynamic finders in Rails (which allow you to concoct methods like +find_by_color_and_flavor+ on the fly) and named scopes (which allow you to encapsulate reusable query conditions into friendly names like +currently_active+). Well, now you can have dynamic scope methods. The idea is to put together syntax that allows filtering on the fly _and_ method chaining. For example:
<ruby>
Order.scoped_by_customer_id(12)
Order.scoped_by_customer_id(12).find(:all,
:conditions => "status = 'open'")
Order.scoped_by_customer_id(12).scoped_by_status("open")
</ruby>
There's nothing to define to use dynamic scopes: they just work.
* Lead Contributor: "Yaroslav Markin":http://evilmartians.com/
* More Information: "What's New in Edge Rails: Dynamic Scope Methods":http://ryandaigle.com/articles/2008/12/29/what-s-new-in-edge-rails-dynamic-scope-methods.
h4. Default Scopes
Rails 2.3 will introduce the notion of _default scopes_ similar to named scopes, but applying to all named scopes or find methods within the model. For example, you can write +default_scope :order => 'name ASC'+ and any time you retrieve records from that model they'll come out sorted by name (unless you override the option, of course).
* Lead Contributor: Paweł Kondzior
* More Information: "What's New in Edge Rails: Default Scoping":http://ryandaigle.com/articles/2008/11/18/what-s-new-in-edge-rails-default-scoping
h4. Multiple Conditions for Callbacks
When using Active Record callbacks, you can now combine +:if+ and +:unless+ options on the same callback, and supply multiple conditions as an array:
<ruby>
before_save :update_credit_rating, :if => :active,
:unless => [:admin, :cash_only]
</ruby>
* Lead Contributor: L. Caviola
h4. Find with having
Rails now has a +:having+ option on find (as well as on +has_many+ and +has_and_belongs_to_many+ associations) for filtering records in grouped finds. As those with heavy SQL backgrounds know, this allows filtering based on grouped results:
<ruby>
developers = Developer.find(:all, :group => "salary",
:having => "sum(salary) > 10000", :select => "salary")
</ruby>
* Lead Contributor: "Emilio Tagua":http://github.com/miloops
h4. Hash Conditions for has_many relationships
You can once again use a hash in conditions for a +has_many+ relationship:
<ruby>
has_many :orders, :conditions => {:status => 'confirmed'}
</ruby>
That worked in Rails 2.1, fails in Rails 2.2, and will now work again in Rails 2.3 (if you're dealing with this issue in Rails 2.2, you can use a string rather than a hash to specify conditions).
* Lead Contributor: "Frederick Cheung":http://www.spacevatican.org/
h4. Reconnecting MySQL Connections
MySQL supports a reconnect flag in its connections - if set to true, then the client will try reconnecting to the server before giving up in case of a lost connection. You can now set +reconnect = true+ for your MySQL connections in +database.yml+ to get this behavior from a Rails application. The default is +false+, so the behavior of existing applications doesn't change.
* Lead Contributor: "Dov Murik":http://twitter.com/dubek
* More information:
** "Controlling Automatic Reconnection Behavior":http://dev.mysql.com/doc/refman/5.0/en/auto-reconnect.html
** "MySQL auto-reconnect revisited":http://groups.google.com/group/rubyonrails-core/browse_thread/thread/49d2a7e9c96cb9f4
h4. Other Active Record Changes
* An extra +AS+ was removed from the generated SQL for has_and_belongs_to_many preloading, making it work better for some databases.
* +ActiveRecord::Base#new_record?+ now returns +false+ rather than +nil+ when confronted with an existing record.
* A bug in quoting table names in some +has_many :through+ associations was fixed.
* You can now specify a particular timestamp for +updated_at+ timestamps: +cust = Customer.create(:name => "ABC Industries", :updated_at => 1.day.ago)+
* Better error messages on failed +find_by_attribute!+ calls.
* Active Record's +to_xml+ support gets just a little bit more flexible with the addition of a +:camelize+ option.
* A bug in canceling callbacks from +before_update+ or +before_create_ was fixed.
* Rake tasks for testing databases via JDBC have been added.
* +validates_length_of+ will use a custom error message with the +:in+ or +:within+ options (if one is supplied)
h3. Action Controller
Action Controller rolls out some significant changes to rendering, as well as improvements in routing and other areas, in this release.
h4. Unified Rendering
+ActionController::Base#render+ is a lot smarter about deciding what to render. Now you can just tell it what to render and expect to get the right results. In older versions of Rails, you often need to supply explicit information to render:
<ruby>
render :file => '/tmp/random_file.erb'
render :template => 'other_controller/action'
render :action => 'show'
</ruby>
Now in Rails 2.3, you can just supply what you want to render:
<ruby>
render '/tmp/random_file.erb'
render 'other_controller/action'
render 'show'
render :show
</ruby>
Rails chooses between file, template, and action depending on whether there is a leading slash, an embedded slash, or no slash at all in what's to be rendered. Note that you can also use a symbol instead of a string when rendering an action. Other rendering styles (+:inline+, +:text+, +:update+, +:nothing+, +:json+, +:xml+, +:js+) still require an explicit option.
h4. Application Controller Renamed
If you're one of the people who has always been bothered by the special-case naming of +application.rb+, rejoice! It's been reworked to be application_controller.rb in Rails 2.3. In addition, there's a new rake task, +rake rails:update:application_controller+ to do this automatically for you - and it will be run as part of the normal +rake rails:update+ process.
* More Information:
** "The Death of Application.rb":http://afreshcup.com/2008/11/17/rails-2x-the-death-of-applicationrb/
** "What's New in Edge Rails: Application.rb Duality is no More":http://ryandaigle.com/articles/2008/11/19/what-s-new-in-edge-rails-application-rb-duality-is-no-more
h4. HTTP Digest Authentication Support
Rails now has built-in support for HTTP digest authentication. To use it, you call +authenticate_or_request_with_http_digest+ with a block that returns the users password (which is then hashed and compared against the transmitted credentials):
<ruby>
class PostsController < ApplicationController
Users = {"dhh" => "secret"}
before_filter :authenticate
def secret
render :text => "Password Required!"
end
private
def authenticate
realm = "Application"
authenticate_or_request_with_http_digest(realm) do |name|
Users[name]
end
end
end
</ruby>
* Lead Contributor: "Gregg Kellogg":http://www.kellogg-assoc.com/
* More Information: "What's New in Edge Rails: HTTP Digest Authentication":http://ryandaigle.com/articles/2009/1/30/what-s-new-in-edge-rails-http-digest-authentication
h4. More Efficient Routing
There are a couple of significant routing changes in Rails 2.3. The +formatted_+ route helpers are gone, in favor just passing in +:format+ as an option. This cuts down the route generation process by 50% for any resource - and can save a substantial amount of memory (up to 100MB on large applications). If your code uses the +formatted_+ helpers, it will still work for the time being - but that behavior is deprecated and your application will be more efficient if you rewrite those routes using the new standard. Another big change is that Rails now supports multiple routing files, not just +routes.rb+. You can use +RouteSet#add_configuration_file+ to bring in more routes at any time - without clearing the currently-loaded routes. While this change is most useful for Engines, you can use it in any application that needs to load routes in batches.
* Lead Contributors: "Aaron Batalion":http://blog.hungrymachine.com/
h4. Rack-based Lazy-loaded Sessions
A big change pushed the underpinnings of Action Controller session storage down to the Rack level. This involved a good deal of work in the code, though it should be completely transparent to your Rails applications (as a bonus, some icky patches around the old CGI session handler got removed). It's still significant, though, for one simple reason: non-Rails Rack applications have access to the same session storage handlers (and therefore the same session) as your Rails applications. In addition, sessions are now lazy-loaded (in line with the loading improvements to the rest of the framework). This means that you no longer need to explicitly disable sessions if you don't want them; just don't refer to them and they won't load.
h4. MIME Type Handling Changes
There are a couple of changes to the code for handling MIME types in Rails. First, +MIME::Type+ now implements the +=~+ operator, making things much cleaner when you need to check for the presence of a type that has synonyms:
<ruby>
if content_type && Mime::JS =~ content_type
# do something cool
end
Mime::JS =~ "text/javascript" => true
Mime::JS =~ "application/javascript" => true
</ruby>
The other change is that the framework now uses the +Mime::JS+ when checking for javascript in various spots, making it handle those alternatives cleanly.
* Lead Contributor: "Seth Fitzsimmons":http://www.workingwithrails.com/person/5510-seth-fitzsimmons
h4. Optimization of +respond_to+
In some of the first fruits of the Rails-Merb team merger, Rails 2.3 includes some optimizations for the +respond_to+ method, which is of course heavily used in many Rails applications to allow your controller to format results differently based on the MIME type of the incoming request. After eliminating a call to +method_missing+ and some profiling and tweaking, we're seeing an 8% improvement in the number of requests per second served with a simple +respond_to+ that switches between three formats. The best part? No change at all required to the code of your application to take advantage of this speedup.
h4. Improved Caching Performance
Rails now keeps a per-request local cache of requests, cutting down on unnecessary reads and leading to better site performance. While this work was originally limited to +MemCacheStore+, it is available to any remote store than implements the required methods.
* Lead Contributor: "Nahum Wild":http://www.motionstandingstill.com/
h4. Localized Views
Rails can now provide localized views, depending on the locale that you have set. For example, suppose you have a +Posts+ controller with a +show+ action. By default, this will render +app/views/posts/show.html.erb+. But if you set +I18n.locale = :da+, it will render +app/views/posts/show.da.html.erb+. If the localized template isn't present, the undecorated version will be used. Rails also includes +I18n#available_locales+ and +I18n::SimpleBackend#available_locales+, which return an array of the translations that are available in the current Rails project.
h4. Other Action Controller Changes
* ETag handling has been cleaned up a bit: Rails will now skip sending an ETag header when there's no body to the response or when sending files with +send_file+.
* The fact that Rails checks for IP spoofing can be a nuisance for sites that do heavy traffic with cell phones, because their proxies don't generally set things up right. If that's you, you can now set +ActionController::Base.ip_spoofing_check = false+ to disable the check entirely.
* +ActionController::Dispatcher+ now implements its own middleware stack, which you can see by running +rake middleware+.
* Cookie sessions now have persistent session identifiers, with API compatibility with the server-side stores.
* You can now use symbols for the +:type+ option of +send_file+ and +send_data+, like this: +send_file("fabulous.png", :type => :png)+.
* The +:only+ and +:except+ options for +map.resources+ are no longer inherited by nested resources.
h3. Action View
Action View in Rails 2.3 picks up nested model forms, improvements to +render+, more flexible prompts for the date select helpers, and a speedup in asset caching, among other things.
h4. Nested Object Forms
Provided the parent model accepts nested attributes for the child objects (as discussed in the Active Record section), you can create nested forms using +form_for+ and +field_for+. These forms can be nested arbitrarily deep, allowing you to edit complex object hierarchies on a single view without excessive code. For example, given this model:
<ruby>
class Customer < ActiveRecord::Base
has_many :orders
accepts_nested_attributes_for :orders, :allow_destroy => true
end
</ruby>
You can write this view in Rails 2.3:
<erb>
<% form_for @customer do |customer_form| %>
<div>
<%= customer_form.label :name, 'Customer Name:' %>
<%= customer_form.text_field :name %>
</div>
<!-- Here we call fields_for on the customer_form builder instance.
The block is called for each member of the orders collection. -->
<% customer_form.fields_for :orders do |order_form| %>
<p>
<div>
<%= order_form.label :number, 'Order Number:' %>
<%= order_form.text_field :number %>
</div>
<!-- The allow_destroy option in the model enables deletion of
child records. -->
<% unless order_form.object.new_record? %>
<div>
<%= order_form.label :_delete, 'Remove:' %>
<%= order_form.check_box :_delete %>
</div>
<% end %>
</p>
<% end %>
<%= customer_form.submit %>
<% end %>
</erb>
* Lead Contributor: "Eloy Duran":http://www.superalloy.nl/blog/
* More Information:
** "Nested Model Forms":http://weblog.rubyonrails.org/2009/1/26/nested-model-forms
** "complex-form-examples":http://github.com/alloy/complex-form-examples/tree/nested_attributes
** "What's New in Edge Rails: Nested Object Forms":http://ryandaigle.com/articles/2009/2/1/what-s-new-in-edge-rails-nested-attributes
h4. Smart Rendering of Partials
The render method has been getting smarter over the years, and it's even smarter now. If you have an object or a collection and an appropriate partial, and the naming matches up, you can now just render the object and things will work. For example, in Rails 2.3, these render calls will work in your view (assuming sensible naming):
<ruby>
# Equivalent of render :partial => 'articles/_article',
# :object => @article
render @article
# Equivalent of render :partial => 'articles/_article',
# :collection => @articles
render @articles
</ruby>
* More Information: "What's New in Edge Rails: render Stops Being High-Maintenance":http://ryandaigle.com/articles/2008/11/20/what-s-new-in-edge-rails-render-stops-being-high-maintenance
h4. Prompts for Date Select Helpers
In Rails 2.3, you can supply custom prompts for the various date select helpers (+date_select+, +time_select+, and +datetime_select+), the same way you can with collection select helpers. You can supply a prompt string or a hash of individual prompt strings for the various components. You can also just set +:prompt+ to +true+ to use the custom generic prompt:
<ruby>
select_datetime(DateTime.now, :prompt => true)
select_datetime(DateTime.now, :prompt => "Choose date and time")
select_datetime(DateTime.now, :prompt =>
{:day => 'Choose day', :month => 'Choose month',
:year => 'Choose year', :hour => 'Choose hour',
:minute => 'Choose minute'})
</ruby>
* Lead Contributor: "Sam Oliver":http://samoliver.com/
h4. AssetTag Timestamp Caching
You're likely familiar with Rails' practice of adding timestamps to static asset paths as a "cache buster." This helps ensure that stale copies of things like images and stylesheets don't get served out of the user's browser cache when you change them on the server. You can now modify this behavior with the +cache_asset_timestamps+ configuration option for Action View. If you enable the cache, then Rails will calculate the timestamp once when it first serves an asset, and save that value. This means fewer (expensive) file system calls to serve static assets - but it also means that you can't modify any of the assets while the server is running and expect the changes to get picked up by clients.
h4. Asset Hosts as Objects
Asset hosts get more flexible in edge Rails with the ability to declare an asset host as a specific object that responds to a call. This allows you to to implement any complex logic you need in your asset hosting.
* More Information: "asset-hosting-with-minimum-ssl":http://github.com/dhh/asset-hosting-with-minimum-ssl/tree/master
h4. grouped_options_for_select Helper Method
Action View already had a bunch of helpers to aid in generating select controls, but now there's one more: +grouped_options_for_select+. This one accepts an array or hash of strings, and converts them into a string of +option+ tags wrapped with +optgroup+ tags. For example:
<ruby>
grouped_options_for_select([["Hats", ["Baseball Cap","Cowboy Hat"]]],
"Cowboy Hat", "Choose a product...")
</ruby>
returns
<ruby>
<option value="">Choose a product...</option>
<optgroup label="Hats">
<option value="Baseball Cap">Baseball Cap</option>
<option selected="selected" value="Cowboy Hat">Cowboy Hat</option>
</optgroup>
</ruby>
h4. Other Action View Changes
* Token generation for CSRF protection has been simplified; now Rails uses a simple random string generated by +ActiveSupport::SecureRandom+ rather than mucking around with session IDs.
* +auto_link+ now properly applies options (such as +:target+ and +:class+) to generated e-mail links.
* The +autolink+ helper has been refactored to make it a bit less messy and more intuitive.
h3. Active Support
Active Support has a few interesting changes, including the introduction of +Object#try+.
h4. Object#try
A lot of folks have adopted the notion of using try() to attempt operations on objects. It's especially helpful in views where you can avoid nil-checking by writing code like +<%= @person.try(:name) %>+. Well, now it's baked right into Rails. As implemented in Rails, it raises +NoMethodError+ on private methods and always returns +nil+ if the object is nil.
* More Information: "try()":http://ozmm.org/posts/try.html.
h4. Object#tap Backport
+Object#tap+ is an addition to "Ruby 1.9":http://www.ruby-doc.org/core-1.9/classes/Object.html#M000309 and 1.8.7 that is similar to the +returning+ method that Rails has had for a while: it yields to a block, and then returns the object that was yielded. Rails now includes code to make this available under older versions of Ruby as well.
h4. Fractional seconds for TimeWithZone
The +Time+ and +TimeWithZone+ classes include an +xmlschema+ method to return the time in an XML-friendly string. As of Rails 2.3, +TimeWithZone+ supports the same argument for specifying the number of digits in the fractional second part of the returned string that +Time+ does:
<ruby>
>> Time.zone.now.xmlschema(6)
=> "2009-01-16T13:00:06.13653Z"
</ruby>
* Lead Contributor: "Nicholas Dainty":http://www.workingwithrails.com/person/13536-nicholas-dainty
h4. JSON Key Quoting
If you look up the spec on the "json.org" site, you'll discover that all keys in a JSON structure must be strings, and they must be quoted with double quotes. Starting with Rails 2.3, we do the right thing here, even with numeric keys.
h4. Other Active Support Changes
* You can use +Enumerable#none?+ to check that none of the elements match the supplied block.
* If you're using Active Support "delegates":http://afreshcup.com/2008/10/19/coming-in-rails-22-delegate-prefixes/, the new +:allow_nil+ option lets you return +nil+ instead of raising an exception when the target object is nil.
* +ActiveSupport::OrderedHash+: now implements +each_key+ and +each_value+.
* +ActiveSupport::MessageEncryptor+ provides a simple way to encrypt information for storage in an untrusted location (like cookies).
* Active Support's +from_xml+ no longer depends on XmlSimple. Instead, Rails now includes its own XmlMini implementation, with just the functionality that it requires. This lets Rails dispense with the bundled copy of XmlSimple that it's been carting around.
h3. Railties
In addition to the Rack changes covered above, Railties (the core code of Rails itself) sports a number of significant changes, including Rails Metal, application templates, and quiet backtraces.
h4. Rails Metal
Rails Metal is a new mechanism that provides superfast endpoints inside of your Rails applications. Metal classes bypass routing and Action Controller to give you raw speed (at the cost of all the things in Action Controller, of course). This builds on all of the recent foundation work to make Rails a Rack application with an exposed middleware stack.
* More Information:
** "Introducing Rails Metal":http://weblog.rubyonrails.org/2008/12/17/introducing-rails-metal
** "Rails Metal: a micro-framework with the power of Rails":http://soylentfoo.jnewland.com/articles/2008/12/16/rails-metal-a-micro-framework-with-the-power-of-rails-m
** "Metal: Super-fast Endpoints within your Rails Apps":http://www.railsinside.com/deployment/180-metal-super-fast-endpoints-within-your-rails-apps.html
** "What's New in Edge Rails: Rails Metal":http://ryandaigle.com/articles/2008/12/18/what-s-new-in-edge-rails-rails-metal
h4. Application Templates
Rails 2.3 incorporates Jeremy McAnally's "rg":http://github.com/jeremymcanally/rg/tree/master application generator. What this means is that we now have template-based application generation built right into Rails; if you have a set of plugins you include in every application (among many other use cases), you can just set up a template once and use it over and over again when you run the +rails+ command. There's also a rake task to apply a template to an existing application:
<ruby>
rake rails:template LOCATION=~/template.rb
</ruby>
This will layer the changes from the template on top of whatever code the project already contains.
* Lead Contributor: "Jeremy McAnally":http://www.jeremymcanally.com/
* More Info:"Rails templates":http://m.onkey.org/2008/12/4/rails-templates
h4. Quieter Backtraces
Building on Thoughtbot's "Quiet Backtrace":http://www.thoughtbot.com/projects/quietbacktrace plugin, which allows you to selectively remove lines from +Test::Unit+ backtraces, Rails 2.3 implements +ActiveSupport::BacktraceCleaner+ and +Rails::BacktraceCleaner+ in core. This supports both filters (to perform regex-based substitutions on backtrace lines) and silencers (to remove backtrace lines entirely). Rails automatically adds silencers to get rid of the most common noise in a new application, and builds a +config/backtrace_silencers.rb+ file to hold your own additions. This feature also enables prettier printing from any gem in the backtrace.
h4. Faster Boot Time in Development Mode with Lazy Loading/Autoload
Quite a bit of work was done to make sure that bits of Rails (and its dependencies) are only brought into memory when they're actually needed. The core frameworks - Active Support, Active Record, Action Controller, Action Mailer and Action View - are now using +autoload+ to lazy-load their individual classes. This work should help keep the memory footprint down and improve overall Rails performance.
You can also specify (by using the new +preload_frameworks+ option) whether the core libraries should be autoloaded at startup. This defaults to +false+ so that Rails autoloads itself piece-by-piece, but there are some circumstances where you still need to bring in everything at once - Passenger and JRuby both want to see all of Rails loaded together.
h4. Other Railties Changes
* The instructions for updating a CI server to build Rails have been updated and expanded.
* Internal Rails testing has been switched from +Test::Unit::TestCase+ to +ActiveSupport::TestCase+, and the Rails core requires Mocha to test.
* The default +environment.rb+ file has been decluttered.
* The dbconsole script now lets you use an all-numeric password without crashing.
* +Rails.root+ now returns a +Pathname+ object, which means you can use it directly with the +join+ method to "clean up existing code":http://afreshcup.com/2008/12/05/a-little-rails_root-tidiness/ that uses +File.join+.
* Various files in /public that deal with CGI and FCGI dispatching are no longer generated in every Rails application by default (you can still get them if you need them by adding +--with-dispatches+ when you run the +rails+ command, or add them later with +rake rails:generate_dispatchers+).
h3. Deprecated
A few pieces of older code are deprecated in this release:
* If you're one of the (fairly rare) Rails developers who deploys in a fashion that depends on the inspector, reaper, and spawner scripts, you'll need to know that those scripts are no longer included in core Rails. If you need them, you'll be able to pick up copies via the "irs_process_scripts":http://github.com/rails/irs_process_scripts/tree plugin.
* +render_component+ goes from "deprecated" to "nonexistent" in Rails 2.3. If you still need it, you can install the "render_component plugin":http://github.com/rails/render_component/tree/master.
* Support for Rails components has been removed.
* If you were one of the people who got used to running +script/performance/request+ to look at performance based on integration tests, you need to learn a new trick: that script has been removed from core Rails now. Theres a new request_profiler plugin that you can install to get the exact same functionality back.
* +ActionController::Base#session_enabled?+ is deprecated because sessions are lazy-loaded now.
* The +:digest+ and +:secret+ options to +protect_from_forgery+ are deprecated and have no effect.
* Some integration test helpers have been removed. +response.headers["Status"]+ and +headers["Status"]+ will no longer return anything. Rack does not allow "Status" in its return headers. However you can still use the +status+ and +status_message+ helpers. +response.headers["cookie"]+ and +headers["cookie"]+ will no longer return any CGI cookies. You can inspect +headers["Set-Cookie"]+ to see the raw cookie header or use the +cookies+ helper to get a hash of the cookies sent to the client.
* +formatted_polymorphic_url+ is deprecated. Use +polymorphic_url+ with +:format+ instead.
h3. Credits
Release notes compiled by "Mike Gunderloy":http://afreshcup.com

View File

@ -0,0 +1,798 @@
h2. Action Controller Overview
In this guide you will learn how controllers work and how they fit into the request cycle in your application. After reading this guide, you will be able to:
* Follow the flow of a request through a controller
* Understand why and how to store data in the session or cookies
* Work with filters to execute code during request processing
* Use Action Controller's built-in HTTP authentication
* Stream data directly to the user's browser
* Filter sensitive parameters so they do not appear in the application's log
* Deal with exceptions that may be raised during request processing
endprologue.
h3. What Does a Controller do?
Action Controller is the C in MVC. After routing has determined which controller to use for a request, your controller is responsible for making sense of the request and producing the appropriate output. Luckily, Action Controller does most of the groundwork for you and uses smart conventions to make this as straight-forward as possible.
For most conventional RESTful applications, the controller will receive the request (this is invisible to you as the developer), fetch or save data from a model and use a view to create HTML output. If your controller needs to do things a little differently, that's not a problem, this is just the most common way for a controller to work.
A controller can thus be thought of as a middle man between models and views. It makes the model data available to the view so it can display that data to the user, and it saves or updates data from the user to the model.
NOTE: For more details on the routing process, see "Rails Routing from the Outside In":routing_outside_in.html.
h3. Methods and Actions
A controller is a Ruby class which inherits from ApplicationController and has methods just like any other class. When your application receives a request, the routing will determine which controller and action to run, then Rails creates an instance of that controller and runs the public method with the same name as the action.
<ruby>
class ClientsController < ApplicationController
# Actions are public methods
def new
end
# Action methods are responsible for producing output
def edit
end
# Helper methods are private and can not be used as actions
private
def foo
end
end
</ruby>
There's no rule saying a method on a controller has to be an action; they may well be used for other purposes such as filters, which will be covered later in this guide.
As an example, if a user goes to +/clients/new+ in your application to add a new client, Rails will create an instance of ClientsController and run the +new+ method. Note that the empty method from the example above could work just fine because Rails will by default render the +new.html.erb+ view unless the action says otherwise. The +new+ method could make available to the view a +@client+ instance variable by creating a new Client:
<ruby>
def new
@client = Client.new
end
</ruby>
The "Layouts & rendering guide":layouts_and_rendering.html explains this in more detail.
ApplicationController inherits from ActionController::Base, which defines a number of helpful methods. This guide will cover some of these, but if you're curious to see what's in there, you can see all of them in the API documentation or in the source itself.
h3. Parameters
You will probably want to access data sent in by the user or other parameters in your controller actions. There are two kinds of parameters possible in a web application. The first are parameters that are sent as part of the URL, called query string parameters. The query string is everything after "?" in the URL. The second type of parameter is usually referred to as POST data. This information usually comes from a HTML form which has been filled in by the user. It's called POST data because it can only be sent as part of an HTTP POST request. Rails does not make any distinction between query string parameters and POST parameters, and both are available in the +params+ hash in your controller:
<ruby>
class ClientsController < ActionController::Base
# This action uses query string parameters because it gets run by a HTTP
# GET request, but this does not make any difference to the way in which
# the parameters are accessed. The URL for this action would look like this
# in order to list activated clients: /clients?status=activated
def index
if params[:status] = "activated"
@clients = Client.activated
else
@clients = Client.unativated
end
end
# This action uses POST parameters. They are most likely coming from an HTML
# form which the user has submitted. The URL for this RESTful request will
# be "/clients", and the data will be sent as part of the request body.
def create
@client = Client.new(params[:client])
if @client.save
redirect_to @client
else
# This line overrides the default rendering behavior, which would have been
# to render the "create" view.
render :action => "new"
end
end
end
</ruby>
h4. Hash and Array Parameters
The params hash is not limited to one-dimensional keys and values. It can contain arrays and (nested) hashes. To send an array of values, append "[]" to the key name:
<pre><code>
GET /clients?ids[]=1&ids[]=2&ids[]=3
</code></pre>
NOTE: The actual URL in this example will be encoded as "/clients?ids%5b%5d=1&ids%5b%5d=2&ids%5b%5b=3" as [ and ] are not allowed in URLs. Most of the time you don't have to worry about this because the browser will take care of it for you, and Rails will decode it back when it receives it, but if you ever find yourself having to send those requests to the server manually you have to keep this in mind.
The value of +params[:ids]+ will now be +["1", "2", "3"]+. Note that parameter values are always strings; Rails makes no attempt to guess or cast the type.
To send a hash you include the key name inside the brackets:
<html>
<form action="/clients" method="post">
<input type="text" name="client[name]" value="Acme" />
<input type="text" name="client[phone]" value="12345" />
<input type="text" name="client[address][postcode]" value="12345" />
<input type="text" name="client[address][city]" value="Carrot City" />
</form>
</html>
The value of +params[:client]+ when this form is submitted will be +{"name" => "Acme", "phone" => "12345", "address" => {"postcode" => "12345", "city" => "Carrot City"}}+. Note the nested hash in +params[:client][:address]+.
Note that the params hash is actually an instance of HashWithIndifferentAccess from Active Support which is a subclass of Hash which lets you use symbols and strings interchangeably as keys.
h4. Routing Parameters
The +params+ hash will always contain the +:controller+ and +:action+ keys, but you should use the methods +controller_name+ and +action_name+ instead to access these values. Any other parameters defined by the routing, such as +:id+ will also be available. As an example, consider a listing of clients where the list can show either active or inactive clients. We can add a route which captures the +:status+ parameter in a "pretty" URL:
<ruby>
# ...
map.connect "/clients/:status", :controller => "clients", :action => "index", :foo => "bar"
# ...
</ruby>
In this case, when a user opens the URL +/clients/active+, +params[:status]+ will be set to "active". When this route is used, +params[:foo]+ will also be set to "bar" just like it was passed in the query string in the same way +params[:action]+ will contain "index".
h4. default_url_options
You can set global default parameters that will be used when generating URLs with +default_url_options+. To do this, define a method with that name in your controller:
<ruby>
class ApplicationController < ActionController::Base
#The options parameter is the hash passed in to +url_for+
def default_url_options(options)
{:locale => I18n.locale}
end
end
</ruby>
These options will be used as a starting-point when generating, so it's possible they'll be overridden by +url_for+. Because this method is defined in the controller, you can define it on ApplicationController so it would be used for all URL generation, or you could define it on only one controller for all URLs generated there.
h3. Session
Your application has a session for each user in which you can store small amounts of data that will be persisted between requests. The session is only available in the controller and the view and can use one of a number of different storage mechanisms:
* CookieStore - Stores everything on the client.
* DRbStore - Stores the data on a DRb server.
* MemCacheStore - Stores the data in a memcache.
* ActiveRecordStore - Stores the data in a database using Active Record.
All session stores use a cookie - this is required and Rails does not allow any part of the session to be passed in any other way (e.g. you can't use the query string to pass a session ID) because of security concerns (it's easier to hijack a session when the ID is part of the URL).
Most stores use a cookie to store the session ID which is then used to look up the session data on the server. The default and recommended store, the CookieStore, does not store session data on the server, but in the cookie itself. The data is cryptographically signed to make it tamper-proof, but it is not encrypted, so anyone with access to it can read its contents but not edit it (Rails will not accept it if it has been edited). It can only store about 4kB of data - much less than the others - but this is usually enough. Storing large amounts of data is discouraged no matter which session store your application uses. You should especially avoid storing complex objects (anything other than basic Ruby objects, the most common example being model instances) in the session, as the server might not be able to reassemble them between requests, which will result in an error. The CookieStore has the added advantage that it does not require any setting up beforehand - Rails will generate a "secret key" which will be used to sign the cookie when you create the application.
Read more about session storage in the "Security Guide":security.html.
If you need a different session storage mechanism, you can change it in the +config/environment.rb+ file:
<ruby>
# Set to one of [:active_record_store, :drb_store, :mem_cache_store, :cookie_store]
config.action_controller.session_store = :active_record_store
</ruby>
h4. Accessing the Session
In your controller you can access the session through the +session+ instance method.
NOTE: Sessions are lazily loaded. If you don't access sessions in your action's code, they will not be loaded. Hence you will never need to disable sessions, just not accessing them will do the job.
Session values are stored using key/value pairs like a hash:
<ruby>
class ApplicationController < ActionController::Base
private
# Finds the User with the ID stored in the session with the key :current_user_id
# This is a common way to handle user login in a Rails application; logging in sets the
# session value and logging out removes it.
def current_user
@_current_user ||= session[:current_user_id] && User.find(session[:current_user_id])
end
end
</ruby>
To store something in the session, just assign it to the key like a hash:
<ruby>
class LoginsController < ApplicationController
# "Create" a login, aka "log the user in"
def create
if user = User.authenticate(params[:username, params[:password])
# Save the user ID in the session so it can be used in subsequent requests
session[:current_user_id] = user.id
redirect_to root_url
end
end
end
</ruby>
To remove something from the session, assign that key to be +nil+:
<ruby>
class LoginsController < ApplicationController
# "Delete" a login, aka "log the user out"
def destroy
# Remove the user id from the session
session[:current_user_id] = nil
redirect_to root_url
end
end
</ruby>
To reset the entire session, use +reset_session+.
h4. The flash
The flash is a special part of the session which is cleared with each request. This means that values stored there will only be available in the next request, which is useful for storing error messages etc. It is accessed in much the same way as the session, like a hash. Let's use the act of logging out as an example. The controller can send a message which will be displayed to the user on the next request:
<ruby>
class LoginsController < ApplicationController
def destroy
session[:current_user_id] = nil
flash[:notice] = "You have successfully logged out"
redirect_to root_url
end
end
</ruby>
The +destroy+ action redirects to the application's +root_url+, where the message will be displayed. Note that it's entirely up to the next action to decide what, if anything, it will do with what the previous action put in the flash. It's conventional to display eventual errors or notices from the flash in the application's layout:
<ruby>
<html>
<!-- <head/> -->
<body>
<% if flash[:notice] -%>
<p class="notice"><%= flash[:notice] %></p>
<% end -%>
<% if flash[:error] -%>
<p class="error"><%= flash[:error] %></p>
<% end -%>
<!-- more content -->
</body>
</html>
</ruby>
This way, if an action sets an error or a notice message, the layout will display it automatically.
If you want a flash value to be carried over to another request, use the +keep+ method:
<ruby>
class MainController < ApplicationController
# Let's say this action corresponds to root_url, but you want all requests here to be redirected to
# UsersController#index. If an action sets the flash and redirects here, the values would normally be
# lost when another redirect happens, but you can use keep to make it persist for another request.
def index
flash.keep # Will persist all flash values. You can also use a key to keep only that value: flash.keep(:notice)
redirect_to users_url
end
end
</ruby>
h5. flash.now
By default, adding values to the flash will make them available to the next request, but sometimes you may want to access those values in the same request. For example, if the +create+ action fails to save a resource and you render the +new+ template directly, that's not going to result in a new request, but you may still want to display a message using the flash. To do this, you can use +flash.now+ in the same way you use the normal +flash+:
<ruby>
class ClientsController < ApplicationController
def create
@client = Client.new(params[:client])
if @client.save
# ...
else
flash.now[:error] = "Could not save client"
render :action => "new"
end
end
end
</ruby>
h3. Cookies
Your application can store small amounts of data on the client - called cookies - that will be persisted across requests and even sessions. Rails provides easy access to cookies via the +cookies+ method, which - much like the +session+ - works like a hash:
<ruby>
class CommentsController < ApplicationController
def new
#Auto-fill the commenter's name if it has been stored in a cookie
@comment = Comment.new(:name => cookies[:commenter_name])
end
def create
@comment = Comment.new(params[:comment])
if @comment.save
flash[:notice] = "Thanks for your comment!"
if params[:remember_name]
# Remember the commenter's name
cookies[:commenter_name] = @comment.name
else
# Don't remember, and delete the name if it has been remembered before
cookies.delete(:commenter_name)
end
redirect_to @comment.article
else
render :action => "new"
end
end
end
</ruby>
Note that while for session values you set the key to +nil+, to delete a cookie value you should use +cookies.delete(:key)+.
h3. Filters
Filters are methods that are run before, after or "around" a controller action. For example, one filter might check to see if the logged in user has the right credentials to access that particular controller or action. Filters are inherited, so if you set a filter on ApplicationController, it will be run on every controller in your application. A common, simple filter is one which requires that a user is logged in for an action to be run. You can define the filter method this way:
<ruby>
class ApplicationController < ActionController::Base
private
def require_login
unless logged_in?
flash[:error] = "You must be logged in to access this section"
redirect_to new_login_url # Prevents the current action from running
end
end
# The logged_in? method simply returns true if the user is logged in and
# false otherwise. It does this by "booleanizing" the current_user method
# we created previously using a double ! operator. Note that this is not
# common in Ruby and is discouraged unless you really mean to convert something
# into true or false.
def logged_in?
!!current_user
end
end
</ruby>
The method simply stores an error message in the flash and redirects to the login form if the user is not logged in. If a before filter (a filter which is run before the action) renders or redirects, the action will not run. If there are additional filters scheduled to run after the rendering or redirecting filter, they are also cancelled. To use this filter in a controller, use the +before_filter+ method:
<ruby>
class ApplicationController < ActionController::Base
before_filter :require_login
end
</ruby>
In this example, the filter is added to ApplicationController and thus all controllers in the application. This will make everything in the application require the user to be logged in in order to use it. For obvious reasons (the user wouldn't be able to log in in the first place!), not all controllers or actions should require this. You can prevent this filter from running before particular actions with +skip_before_filter+:
<ruby>
class LoginsController < Application
skip_before_filter :require_login, :only => [:new, :create]
end
</ruby>
Now, the LoginsController's +new+ and +create+ actions will work as before without requiring the user to be logged in. The +:only+ option is used to only skip this filter for these actions, and there is also an +:except+ option which works the other way. These options can be used when adding filters too, so you can add a filter which only runs for selected actions in the first place.
h4. After Filters and Around Filters
In addition to the before filters, you can run filters after an action has run or both before and after. The after filter is similar to the before filter, but because the action has already been run it has access to the response data that's about to be sent to the client. Obviously, after filters can not stop the action from running. Around filters are responsible for running the action, but they can choose not to, which is the around filter's way of stopping it.
<ruby>
# Example taken from the Rails API filter documentation:
# http://api.rubyonrails.org/classes/ActionController/Filters/ClassMethods.html
class ApplicationController < Application
around_filter :catch_exceptions
private
def catch_exceptions
yield
rescue => exception
logger.debug "Caught exception! #{exception}"
raise
end
end
</ruby>
h4. Other Ways to Use Filters
While the most common way to use filters is by creating private methods and using *_filter to add them, there are two other ways to do the same thing.
The first is to use a block directly with the *_filter methods. The block receives the controller as an argument, and the +require_login+ filter from above could be rewritten to use a block:
<ruby>
class ApplicationController < ActionController::Base
before_filter { |controller| redirect_to new_login_url unless controller.send(:logged_in?) }
end
</ruby>
Note that the filter in this case uses +send+ because the +logged_in?+ method is private and the filter is not run in the scope of the controller. This is not the recommended way to implement this particular filter, but in more simple cases it might be useful.
The second way is to use a class (actually, any object that responds to the right methods will do) to handle the filtering. This is useful in cases that are more complex than can not be implemented in a readable and reusable way using the two other methods. As an example, you could rewrite the login filter again to use a class:
<ruby>
class ApplicationController < ActionController::Base
before_filter LoginFilter
end
class LoginFilter
def self.filter(controller)
unless logged_in?
controller.flash[:error] = "You must be logged in to access this section"
controller.redirect_to controller.new_login_url
end
end
end
</ruby>
Again, this is not an ideal example for this filter, because it's not run in the scope of the controller but gets the controller passed as an argument. The filter class has a class method +filter+ which gets run before or after the action, depending on if it's a before or after filter. Classes used as around filters can also use the same +filter+ method, which will get run in the same way. The method must +yield+ to execute the action. Alternatively, it can have both a +before+ and an +after+ method that are run before and after the action.
The Rails API documentation has "more information on using filters":http://api.rubyonrails.org/classes/ActionController/Filters/ClassMethods.html.
h3. Verification
Verifications make sure certain criteria are met in order for a controller or action to run. They can specify that a certain key (or several keys in the form of an array) is present in the +params+, +session+ or +flash+ hashes or that a certain HTTP method was used or that the request was made using XMLHTTPRequest (Ajax). The default action taken when these criteria are not met is to render a 400 Bad Request response, but you can customize this by specifying a redirect URL or rendering something else and you can also add flash messages and HTTP headers to the response. It is described in the "API documentation":http://api.rubyonrails.org/classes/ActionController/Verification/ClassMethods.html as "essentially a special kind of before_filter".
Here's an example of using verification to make sure the user supplies a username and a password in order to log in:
<ruby>
class LoginsController < ApplicationController
verify :params => [:username, :password],
:render => {:action => "new"},
:add_flash => {:error => "Username and password required to log in"}
def create
@user = User.authenticate(params[:username], params[:password])
if @user
flash[:notice] = "You're logged in"
redirect_to root_url
else
render :action => "new"
end
end
end
</ruby>
Now the +create+ action won't run unless the "username" and "password" parameters are present, and if they're not, an error message will be added to the flash and the +new+ action will be rendered. But there's something rather important missing from the verification above: It will be used for *every* action in LoginsController, which is not what we want. You can limit which actions it will be used for with the +:only+ and +:except+ options just like a filter:
<ruby>
class LoginsController < ApplicationController
verify :params => [:username, :password],
:render => {:action => "new"},
:add_flash => {:error => "Username and password required to log in"},
:only => :create # Only run this verification for the "create" action
end
</ruby>
h3. Request Forgery Protection
Cross-site request forgery is a type of attack in which a site tricks a user into making requests on another site, possibly adding, modifying or deleting data on that site without the user's knowledge or permission. The first step to avoid this is to make sure all "destructive" actions (create, update and destroy) can only be accessed with non-GET requests. If you're following RESTful conventions you're already doing this. However, a malicious site can still send a non-GET request to your site quite easily, and that's where the request forgery protection comes in. As the name says, it protects from forged requests. The way this is done is to add a non-guessable token which is only known to your server to each request. This way, if a request comes in without the proper token, it will be denied access.
If you generate a form like this:
<ruby>
<% form_for @user do |f| -%>
<%= f.text_field :username %>
<%= f.text_field :password -%>
<% end -%>
</ruby>
You will see how the token gets added as a hidden field:
<ruby>
<form action="/users/1" method="post">
<div><!-- ... --><input type="hidden" value="67250ab105eb5ad10851c00a5621854a23af5489" name="authenticity_token"/></div>
<!-- Fields -->
</form>
</ruby>
Rails adds this token to every form that's generated using the "form helpers":form_helpers.html, so most of the time you don't have to worry about it. If you're writing a form manually or need to add the token for another reason, it's available through the method +form_authenticity_token+:
TODO: Add line below as description
Add a JavaScript variable containing the token for use with Ajax
<erb>
<%= javascript_tag "MyApp.authenticity_token = '#{form_authenticity_token}'" %>
</erb>
The "Security Guide":security.html has more about this and a lot of other security-related issues that you should be aware of when developing a web application.
h3. The request and response Objects
In every controller there are two accessor methods pointing to the request and the response objects associated with the request cycle that is currently in execution. The +request+ method contains an instance of AbstractRequest and the +response+ method returns a +response+ object representing what is going to be sent back to the client.
h4. The request Object
The request object contains a lot of useful information about the request coming in from the client. To get a full list of the available methods, refer to the "API documentation":http://api.rubyonrails.org/classes/ActionController/AbstractRequest.html. Among the properties that you can access on this object are:
* host - The hostname used for this request.
* domain(n=2) - The hostname's first +n+ segments, starting from the right (the TLD)
* format - The content type requested by the client.
* method - The HTTP method used for the request.
* get?, post?, put?, delete?, head? - Returns true if the HTTP method is GET/POST/PUT/DELETE/HEAD.
* headers - Returns a hash containing the headers associated with the request.
* port - The port number (integer) used for the request.
* protocol - Returns a string containing the prototol used plus "://", for example "http://"
* query_string - The query string part of the URL - everything after "?".
* remote_ip - The IP address of the client.
* url - The entire URL used for the request.
h5. path_parameters, query_parameters and request_parameters
Rails collects all of the parameters sent along with the request in the +params+ hash, whether they are sent as part of the query string or the post body. The request object has three accessors that give you access to these parameters depending on where they came from. The +query_parameters+ hash contains parameters that were sent as part of the query string while the +request_parameters+ hash contains parameters sent as part of the post body. The +path_parameters+ hash contains parameters that were recognized by the routing as being part of the path leading to this particular controller and action.
h4. The response Object
The response object is not usually used directly, but is built up during the execution of the action and rendering of the data that is being sent back to the user, but sometimes - like in an after filter - it can be useful to access the response directly. Some of these accessor methods also have setters, allowing you to change their values.
* body - This is the string of data being sent back to the client. This is most often HTML.
* status - The HTTP status code for the response, like 200 for a successful request or 404 for file not found.
* location - The URL the client is being redirected to, if any.
* content_type - The content type of the response.
* charset - The character set being used for the response. Default is "utf8".
* headers - Headers used for the response.
h5. Setting Custom Headers
If you want to set custom headers for a response then +response.headers+ is the place to do it. The headers attribute is a hash which maps header names to their values, and Rails will set some of them - like "Content-Type" - automatically. If you want to add or change a header, just assign it to +headers+ with the name and value:
<ruby>
response.headers["Content-Type"] = "application/pdf"
</ruby>
h3. HTTP Authentications
Rails comes with two built-in HTTP authentication mechanisms :
* Basic Authentication
* Digest Authentication
h4. HTTP Basic Authentication
HTTP Basic authentication is an authentication scheme that is supported by the majority of browsers and other HTTP clients. As an example, consider an administration section which will only be available by entering a username and a password into the browser's HTTP Basic dialog window. Using the built-in authentication is quite easy and only requires you to use one method, +authenticate_or_request_with_http_basic+.
<ruby>
class AdminController < ApplicationController
USERNAME, PASSWORD = "humbaba", "5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8"
before_filter :authenticate
private
def authenticate
authenticate_or_request_with_http_basic do |username, password|
username == USERNAME && Digest::SHA1.hexdigest(password) == PASSWORD
end
end
end
</ruby>
With this in place, you can create namespaced controllers that inherit from AdminController. The before filter will thus be run for all actions in those controllers, protecting them with HTTP Basic authentication.
h4. HTTP Digest Authentication
HTTP Digest authentication is superior to the Basic authentication as it does not require the client to send unencrypted password over the network. Using Digest authentication with Rails is quite easy and only requires using one method, +authenticate_or_request_with_http_digest+.
<ruby>
class AdminController < ApplicationController
USERS = { "lifo" => "world" }
before_filter :authenticate
private
def authenticate
authenticate_or_request_with_http_digest do |username|
USERS[username]
end
end
end
</ruby>
As seen in the example above, +authenticate_or_request_with_http_digest+ block takes only one argument - the username. And the block returns the password. Returning +false+ or +nil+ from the +authenticate_or_request_with_http_digest+ will cause authentication failure.
h3. Streaming and File Downloads
Sometimes you may want to send a file to the user instead of rendering an HTML page. All controllers in Rails have the +send_data+ and the +send_file+ methods, that will both stream data to the client. +send_file+ is a convenience method which lets you provide the name of a file on the disk and it will stream the contents of that file for you.
To stream data to the client, use +send_data+:
<ruby>
require "prawn"
class ClientsController < ApplicationController
# Generate a PDF document with information on the client and return it.
# The user will get the PDF as a file download.
def download_pdf
client = Client.find(params[:id])
send_data(generate_pdf, :filename => "#{client.name}.pdf", :type => "application/pdf")
end
private
def generate_pdf(client)
Prawn::Document.new do
text client.name, :align => :center
text "Address: #{client.address}"
text "Email: #{client.email}"
end.render
end
end
</ruby>
The +download_pdf+ action in the example above will call a private method which actually generates the file (a PDF document) and returns it as a string. This string will then be streamed to the client as a file download and a filename will be suggested to the user. Sometimes when streaming files to the user, you may not want them to download the file. Take images, for example, which can be embedded into HTML pages. To tell the browser a file is not meant to be downloaded, you can set the +:disposition+ option to "inline". The opposite and default value for this option is "attachment".
h4. Sending Files
If you want to send a file that already exists on disk, use the +send_file+ method. This is usually not recommended, but can be useful if you want to perform some authentication before letting the user download the file.
<ruby>
class ClientsController < ApplicationController
# Stream a file that has already been generated and stored on disk
def download_pdf
client = Client.find(params[:id])
send_data("#{RAILS_ROOT}/files/clients/#{client.id}.pdf", :filename => "#{client.name}.pdf", :type => "application/pdf")
end
end
</ruby>
This will read and stream the file 4Kb at the time, avoiding loading the entire file into memory at once. You can turn off streaming with the +:stream+ option or adjust the block size with the +:buffer_size+ option.
WARNING: Be careful when using (or just don't use) "outside" data (params, cookies, etc) to locate the file on disk, as this is a security risk that might allow someone to gain access to files they are not meant to see.
TIP: It is not recommended that you stream static files through Rails if you can instead keep them in a public folder on your web server. It is much more efficient to let the user download the file directly using Apache or another web server, keeping the request from unnecessarily going through the whole Rails stack. Although if you do need the request to go through Rails for some reason, you can set the +:x_sendfile+ option to true, and Rails will let the web server handle sending the file to the user, freeing up the Rails process to do other things. Note that your web server needs to support the +X-Sendfile+ header for this to work, and you still have to be careful not to use user input in a way that lets someone retrieve arbitrary files.
h4. RESTful Downloads
While +send_data+ works just fine, if you are creating a RESTful application having separate actions for file downloads is usually not necessary. In REST terminology, the PDF file from the example above can be considered just another representation of the client resource. Rails provides an easy and quite sleek way of doing "RESTful downloads". Here's how you can rewrite the example so that the PDF download is a part of the +show+ action, without any streaming:
<ruby>
class ClientsController < ApplicationController
# The user can request to receive this resource as HTML or PDF.
def show
@client = Client.find(params[:id])
respond_to do |format|
format.html
format.pdf{ render :pdf => generate_pdf(@client) }
end
end
end
</ruby>
In order for this example to work, you have to add the PDF MIME type to Rails. This can be done by adding the following line to the file +config/initializers/mime_types.rb+:
<ruby>
Mime::Type.register "application/pdf", :pdf
</ruby>
NOTE: Configuration files are not reloaded on each request, so you have to restart the server in order for their changes to take effect.
Now the user can request to get a PDF version of a client just by adding ".pdf" to the URL:
<ruby>
GET /clients/1.pdf
</ruby>
h3. Parameter Filtering
Rails keeps a log file for each environment (development, test and production) in the +log+ folder. These are extremely useful when debugging what's actually going on in your application, but in a live application you may not want every bit of information to be stored in the log file. The +filter_parameter_logging+ method can be used to filter out sensitive information from the log. It works by replacing certain values in the +params+ hash with "[FILTERED]" as they are written to the log. As an example, let's see how to filter all parameters with keys that include "password":
<ruby>
class ApplicationController < ActionController::Base
filter_parameter_logging :password
end
</ruby>
The method works recursively through all levels of the params hash and takes an optional second parameter which is used as the replacement string if present. It can also take a block which receives each key in turn and replaces those for which the block returns true.
h3. Rescue
Most likely your application is going to contain bugs or otherwise throw an exception that needs to be handled. For example, if the user follows a link to a resource that no longer exists in the database, Active Record will throw the ActiveRecord::RecordNotFound exception. Rails' default exception handling displays a 500 Server Error message for all exceptions. If the request was made locally, a nice traceback and some added information gets displayed so you can figure out what went wrong and deal with it. If the request was remote Rails will just display a simple "500 Server Error" message to the user, or a "404 Not Found" if there was a routing error or a record could not be found. Sometimes you might want to customize how these errors are caught and how they're displayed to the user. There are several levels of exception handling available in a Rails application:
h4. The Default 500 and 404 Templates
By default a production application will render either a 404 or a 500 error message. These messages are contained in static HTML files in the +public+ folder, in +404.html+ and +500.html+ respectively. You can customize these files to add some extra information and layout, but remember that they are static; i.e. you can't use RHTML or layouts in them, just plain HTML.
h4. rescue_from
If you want to do something a bit more elaborate when catching errors, you can use +rescue_from+, which handles exceptions of a certain type (or multiple types) in an entire controller and its subclasses. When an exception occurs which is caught by a +rescue_from+ directive, the exception object is passed to the handler. The handler can be a method or a Proc object passed to the +:with+ option. You can also use a block directly instead of an explicit Proc object.
Here's how you can use +rescue_from+ to intercept all ActiveRecord::RecordNotFound errors and do something with them.
<ruby>
class ApplicationController < ActionController::Base
rescue_from ActiveRecord::RecordNotFound, :with => :record_not_found
private
def record_not_found
render :text => "404 Not Found", :status => 404
end
end
</ruby>
Of course, this example is anything but elaborate and doesn't improve on the default exception handling at all, but once you can catch all those exceptions you're free to do whatever you want with them. For example, you could create custom exception classes that will be thrown when a user doesn't have access to a certain section of your application:
<ruby>
class ApplicationController < ActionController::Base
rescue_from User::NotAuthorized, :with => :user_not_authorized
private
def user_not_authorized
flash[:error] = "You don't have access to this section."
redirect_to :back
end
end
class ClientsController < ApplicationController
# Check that the user has the right authorization to access clients.
before_filter :check_authorization
# Note how the actions don't have to worry about all the auth stuff.
def edit
@client = Client.find(params[:id])
end
private
# If the user is not authorized, just throw the exception.
def check_authorization
raise User::NotAuthorized unless current_user.admin?
end
end
</ruby>
NOTE: Certain exceptions are only rescuable from the ApplicationController class, as they are raised before the controller gets initialized and the action gets executed. See Pratik Naik's "article":http://m.onkey.org/2008/7/20/rescue-from-dispatching on the subject for more information.
h3. Changelog
"Lighthouse Ticket":http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/17
* November 4, 2008: First release version by Tore Darell

View File

@ -0,0 +1,423 @@
h2. Action Mailer Basics
This guide should provide you with all you need to get started in sending and receiving emails from/to your application, and many internals of Action Mailer. It will also cover how to test your mailers.
endprologue.
h3. Introduction
Action Mailer allows you to send email from your application using a mailer model and views. Yes, that is correct, in Rails, emails are used by creating models that inherit from ActionMailer::Base. They live alongside other models in `app/models` but they have views just like controllers that appear alongside other views in `app/views`.
h3. Sending Emails
Let's say you want to send a welcome email to a user after they signup. Here is how you would go about this:
h4. Walkthrough to generating a mailer
h5. Create the mailer:
<shell>
./script/generate mailer UserMailer
exists app/models/
create app/views/user_mailer
exists test/unit/
create test/fixtures/user_mailer
create app/models/user_mailer.rb
create test/unit/user_mailer_test.rb
</shell>
So we got the model, the fixtures, and the tests all created for us
h5. Edit the model:
If you look at +app/models/user_mailer.rb+, you will see:
<ruby>
class UserMailer < ActionMailer::Base
end
</ruby>
Lets add a method called +welcome_email+, that will send an email to the user's registered email address:
<ruby>
class UserMailer < ActionMailer::Base
def welcome_email(user)
recipients user.email
from "My Awesome Site Notifications <notifications@example.com>"
subject "Welcome to My Awesome Site"
sent_on Time.now
body {:user => user, :url => "http://example.com/login"}
content_type "text/html"
end
end
</ruby>
So what do we have here?
|recipients| The recipients of the email. It can be a string or, if there are multiple recipients, an array of strings|
|from| Who the email will appear to come from in the recipients' mailbox|
|subject| The subject of the email|
|sent_on| Timestamp for the email|
|content_type| The content type, by default is text/plain|
The keys of the hash passed to `body` become instance variables in the view. Thus, in our example the mailer view will have a @user and a @url instance variables available.
h5. Create the mailer view
Create a file called +welcome_email.html.erb+ in +#{RAILS_ROOT}/app/views/user_mailer/+. This will be the template used for the email. This file will be used for html formatted emails. Had we wanted to send text-only emails, the file would have been called +welcome_email.txt.erb+, and we would have set the content type to text/plain in the mailer model.
The file can look like:
<erb>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
</head>
<body>
<h1>Welcome to example.com, <%=h @user.first_name %></h1>
<p>
You have successfully signed up to example.com, and your username is: <%= @user.login %>.<br/>
To login to the site, just follow this link: <%=h @url %>.
</p>
<p>Thanks for joining and have a great day!</p>
</body>
</html>
</erb>
h5. Wire it up so that the system sends the email when a user signs up
There are three ways to achieve this. One is to send the email from the controller that sends the email, another is to put it in a +before_create+ callback in the user model, and the last one is to use an observer on the user model. Whether you use the second or third methods is up to you, but staying away from the first is recommended. Not because it's wrong, but because it keeps your controller clean, and keeps all logic related to the user model within the user model. This way, whichever way a user is created (from a web form, or from an API call, for example), we are guaranteed that the email will be sent.
Let's see how we would go about wiring it up using an observer:
In +config/environment.rb+:
<ruby>
# Code that already exists
Rails::Initializer.run do |config|
# Code that already exists
config.active_record.observers = :user_observer
end
</ruby>
There was a bit of a debate on where to put observers. Some people put them in app/models, but a cleaner method may be to create an app/observers folder to store all observers, and add that to your load path. Open +config/environment.rb+ and make it look like:
<ruby>
# Code that already exists
Rails::Initializer.run do |config|
# Code that already exists
config.load_paths += %W(#{RAILS_ROOT}/app/observers)
config.active_record.observers = :user_observer
end
</ruby>
Now create a file called user_observer in +app/models+ or +app/observers+ depending on where you stored it, and make it look like:
<ruby>
class UserObserver < ActiveRecord::Observer
def after_create(user)
UserMailer.deliver_welcome_email(user)
end
end
</ruby>
Notice how we call +deliver_welcome_email+? Where is that method? Well if you remember, we created a method called +welcome_email+ in UserMailer, right? Well, as part of the "magic" of Rails, we deliver the email identified by +welcome_email+ by calling +deliver_welcome_email+. The next section will go through this in more detail.
That's it! Now whenever your users signup, they will be greeted with a nice welcome email.
h4. Action Mailer and dynamic deliver_<method> methods
So how does Action Mailer understand this deliver_welcome_email call? If you read the documentation (http://api.rubyonrails.org/files/vendor/rails/actionmailer/README.html), you will find this in the "Sending Emails" section:
You never instantiate your mailer class. Rather, your delivery instance methods are automatically wrapped in class methods that start with the word +deliver_+ followed by the name of the mailer method that you would like to deliver. The +welcome_email+ method defined above is delivered by invoking +Notifier.deliver_welcome_email+.
So, how exactly does this work?
In +ActionMailer::Base+, you will find this:
<ruby>
def method_missing(method_symbol, *parameters)#:nodoc:
case method_symbol.id2name
when /^create_([_a-z]\w*)/ then new($1, *parameters).mail
when /^deliver_([_a-z]\w*)/ then new($1, *parameters).deliver!
when "new" then nil
else super
end
end
</ruby>
Hence, if the method name starts with +deliver_+ followed by any combination of lowercase letters or underscore, +method_missing+ calls +new+ on your mailer class (+UserMailer+ in our example above), sending the combination of lower case letters or underscore, along with the parameters. The resulting object is then sent the +deliver!+ method, which well... delivers it.
h4. Complete List of ActionMailer user-settable attributes
|bcc| Specify the BCC addresses for the message|
|body| Define the body of the message. This is either a Hash (in which case it specifies the variables to pass to the template when it is rendered), or a string, in which case it specifies the actual text of the message.|
|cc| Specify the CC addresses for the message.|
|charset| Specify the charset to use for the message. This defaults to the default_charset specified for ActionMailer::Base.|
|content_type| Specify the content type for the message. This defaults to <text/plain in most cases, but can be automatically set in some situations.|
|from| Specify the from address for the message.|
|reply_to| Specify the address (if different than the "from" address) to direct replies to this message.|
|headers| Specify additional headers to be added to the message.|
|implicit_parts_order| Specify the order in which parts should be sorted, based on content-type. This defaults to the value for the default_implicit_parts_order.|
|mime_version| Defaults to "1.0", but may be explicitly given if needed.|
|recipient| The recipient addresses for the message, either as a string (for a single address) or an array (for multiple addresses).|
|sent_on| The date on which the message was sent. If not set (the default), the header will be set by the delivery agent.|
|subject| Specify the subject of the message.|
|template| Specify the template name to use for current message. This is the "base" template name, without the extension or directory, and may be used to have multiple mailer methods share the same template.|
h4. Mailer Views
Mailer views are located in /app/views/name_of_mailer_class. The specific mailer view is known to the class because it's name is the same as the mailer method. So for example, in our example from above, our mailer view for the welcome_email method will be in /app/views/user_mailer/welcome_email.html.erb for the html version and welcome_email.txt.erb for the plain text version.
To change the default mailer view for your action you do something like:
<ruby>
class UserMailer < ActionMailer::Base
def welcome_email(user)
recipients user.email
from "My Awesome Site Notifications<notifications@example.com>"
subject "Welcome to My Awesome Site"
sent_on Time.now
body {:user => user, :url => "http://example.com/login"}
content_type "text/html"
# change the default from welcome_email.[html, txt].erb
template "some_other_template" # this will be in app/views/user_mailer/some_other_template.[html, txt].erb
end
end
</ruby>
h4. Action Mailer Layouts
Just like controller views, you can also have mailer layouts. The layout name needs to end in _mailer to be automatically recognized by your mailer as a layout. So in our UserMailer example, we need to call our layout user_mailer.[html,txt].erb. In order to use a different file just use:
<ruby>
class UserMailer < ActionMailer::Base
layout 'awesome' # will use awesome.html.erb as the layout
end
</ruby>
Just like with controller views, use yield to render the view inside the layout.
h4. Generating URL's in Action Mailer views
URLs can be generated in mailer views using url_for or named routes.
Unlike controllers from Action Pack, the mailer instance doesn't have any context about the incoming request, so you'll need to provide all of the details needed to generate a URL.
When using url_for you'll need to provide the :host, :controller, and :action:
<erb>
<%= url_for(:host => "example.com", :controller => "welcome", :action => "greeting") %>
</erb>
When using named routes you only need to supply the :host:
<erb>
<%= users_url(:host => "example.com") %>
</erb>
You will want to avoid using the name_of_route_path form of named routes because it doesn't make sense to generate relative URLs in email messages. The reason that it doesn't make sense is because the email is opened on a mail client outside of your environment. Since the email is not being served by your server, a URL like "/users/show/1", will have no context. In order for the email client to properly link to a URL on your server it needs something like "http://yourserver.com/users/show/1".
It is also possible to set a default host that will be used in all mailers by setting the :host option in
the ActionMailer::Base.default_url_options hash as follows:
<erb>
ActionMailer::Base.default_url_options[:host] = "example.com"
</erb>
This can also be set as a configuration option in config/environment.rb:
<erb>
config.action_mailer.default_url_options = { :host => "example.com" }
</erb>
If you do decide to set a default :host for your mailers you will want to use the :only_path => false option when using url_for. This will ensure that absolute URLs are generated because the url_for view helper will, by default, generate relative URLs when a :host option isn't explicitly provided.
h4. Sending multipart emails
Action Mailer will automatically send multipart emails if you have different templates for the same action. So, for our UserMailer example, if you have welcome_email.txt.erb and welcome_email.html.erb in app/views/user_mailer, Action Mailer will automatically send a multipart email with the html and text versions setup as different parts.
To explicitly specify multipart messages, you can do something like:
<ruby>
class UserMailer < ActionMailer::Base
def welcome_email(user)
recipients user.email_address
subject "New account information"
from "system@example.com"
content_type "multipart/alternative"
part :content_type => "text/html",
:body => "<p>html content, can also be the name of an action that you call<p>"
part "text/plain" do |p|
p.body = "text content, can also be the name of an action that you call"
end
end
end
</ruby>
h4. Sending emails with attachments
Attachments can be added by using the attachment method:
<ruby>
class UserMailer < ActionMailer::Base
def welcome_email(user)
recipients user.email_address
subject "New account information"
from "system@example.com"
content_type "multipart/alternative"
attachment :content_type => "image/jpeg",
:body => File.read("an-image.jpg")
attachment "application/pdf" do |a|
a.body = generate_your_pdf_here()
end
end
end
</ruby>
h3. Receiving Emails
Receiving and parsing emails with Action Mailer can be a rather complex endeavour. Before your email reaches your Rails app, you would have had to configure your system to somehow forward emails to your app, which needs to be listening for that.
So, to receive emails in your Rails app you'll need:
1. Configure your email server to forward emails from the address(es) you would like your app to receive to /path/to/app/script/runner \'UserMailer.receive(STDIN.read)'
2. Implement a receive method in your mailer
Once a method called receive is defined in any mailer, Action Mailer will parse the raw incoming email into an email object, decode it, instantiate a new mailer, and pass the email object to the mailer objects receive method. Here's an example:
<ruby>
class UserMailer < ActionMailer::Base
def receive(email)
page = Page.find_by_address(email.to.first)
page.emails.create(
:subject => email.subject,
:body => email.body
)
if email.has_attachments?
for attachment in email.attachments
page.attachments.create({
:file => attachment,
:description => email.subject
})
end
end
end
end
</ruby>
h3. Using Action Mailer Helpers
Action Mailer classes have 4 helper methods available to them:
|add_template_helper(helper_module)|Makes all the (instance) methods in the helper module available to templates rendered through this controller.|
|helper(*args, &block)| Declare a helper: helper :foo requires 'foo_helper' and includes FooHelper in the template class. helper FooHelper includes FooHelper in the template class. helper { def foo() "#{bar} is the very best" end } evaluates the block in the template class, adding method foo. helper(:three, BlindHelper) { def mice() 'mice' end } does all three. |
|helper_method| Declare a controller method as a helper. For example, helper_method :link_to def link_to(name, options) ... end makes the link_to controller method available in the view.|
|helper_attr| Declare a controller attribute as a helper. For example, helper_attr :name attr_accessor :name makes the name and name= controller methods available in the view. The is a convenience wrapper for helper_method.|
h3. Action Mailer Configuration
The following configuration options are best made in one of the environment files (environment.rb, production.rb, etc...)
|template_root|Determines the base from which template references will be made.|
|logger|the logger is used for generating information on the mailing run if available. Can be set to nil for no logging. Compatible with both Ruby's own Logger and Log4r loggers.|
|smtp_settings|Allows detailed configuration for :smtp delivery method: :address - Allows you to use a remote mail server. Just change it from its default "localhost" setting. :port - On the off chance that your mail server doesn't run on port 25, you can change it. :domain - If you need to specify a HELO domain, you can do it here. :user_name - If your mail server requires authentication, set the username in this setting. :password - If your mail server requires authentication, set the password in this setting. :authentication - If your mail server requires authentication, you need to specify the authentication type here. This is a symbol and one of :plain, :login, :cram_md5.|
|sendmail_settings|Allows you to override options for the :sendmail delivery method. :location - The location of the sendmail executable. Defaults to /usr/sbin/sendmail. :arguments - The command line arguments. Defaults to -i -t.|
|raise_delivery_errors|Whether or not errors should be raised if the email fails to be delivered.|
|delivery_method|Defines a delivery method. Possible values are :smtp (default), :sendmail, and :test.|
|perform_deliveries|Determines whether deliver_* methods are actually carried out. By default they are, but this can be turned off to help functional testing.|
|deliveries|Keeps an array of all the emails sent out through the Action Mailer with delivery_method :test. Most useful for unit and functional testing.|
|default_charset|The default charset used for the body and to encode the subject. Defaults to UTF-8. You can also pick a different charset from inside a method with charset.|
|default_content_type|The default content type used for the main part of the message. Defaults to "text/plain". You can also pick a different content type from inside a method with content_type.|
|default_mime_version|The default mime version used for the message. Defaults to 1.0. You can also pick a different value from inside a method with mime_version.|
|default_implicit_parts_order|When a message is built implicitly (i.e. multiple parts are assembled from templates which specify the content type in their filenames) this variable controls how the parts are ordered. Defaults to ["text/html", "text/enriched", "text/plain"]. Items that appear first in the array have higher priority in the mail client and appear last in the mime encoded message. You can also pick a different order from inside a method with implicit_parts_order.|
h4. Example Action Mailer Configuration
An example would be:
<ruby>
ActionMailer::Base.delivery_method = :sendmail
ActionMailer::Base.sendmail_settings = {
:location => '/usr/sbin/sendmail',
:arguments => '-i -t'
}
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.default_charset = "iso-8859-1"
</ruby>
h4. Action Mailer Configuration for GMail
Instructions copied from http://http://www.fromjavatoruby.com/2008/11/actionmailer-with-gmail-must-issue.html
First you must install the action_mailer_tls plugin from http://code.openrain.com/rails/action_mailer_tls/, then all you have to do is configure action mailer.
<ruby>
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "domain.com",
:user_name => "user@domain.com",
:password => "password",
:authentication => :plain
}
</ruby>
h4. Configure Action Mailer to recognize HAML templates
In environment.rb, add the following line:
<ruby>
ActionMailer::Base.register_template_extension('haml')
</ruby>
h3. Mailer Testing
Testing mailers involves 2 things. One is that the mail was queued and the other that the body contains what we expect it to contain. With that in mind, we could test our example mailer from above like so:
<ruby>
class UserMailerTest < ActionMailer::TestCase
tests UserMailer
def test_welcome_email
user = users(:some_user_in_your_fixtures)
# Send the email, then test that it got queued
email = UserMailer.deliver_welcome_email(user)
assert !ActionMailer::Base.deliveries.empty?
# Test the body of the sent email contains what we expect it to
assert_equal [@user.email], email.to
assert_equal "Welcome to My Awesome Site", email.subject
assert email.body =~ /Welcome to example.com, #{user.first_name}/
end
end
</ruby>
What have we done? Well, we sent the email and stored the returned object in the email variable. We then ensured that it was sent (the first assert), then, in the second batch of assertion, we ensure that the email does indeed contain the values that we expect.
h3. Epilogue
This guide presented how to create a mailer and how to test it. In reality, you may find that writing your tests before you actually write your code to be a rewarding experience. It may take some time to get used to TDD (Test Driven Development), but coding this way achieves two major benefits. Firstly, you know that the code does indeed work, because the tests fail (because there's no code), then they pass, because the code that satisfies the tests was written. Secondly, when you start with the tests, you don't have to make time AFTER you write the code, to write the tests, then never get around to it. The tests are already there and testing has now become part of your coding regimen.

View File

@ -0,0 +1,135 @@
h2. Active Record Basics
This guide will give you a strong grasp of the Active Record pattern and how it can be used with or without Rails. Hopefully, some of the philosophical and theoretical intentions discussed here will also make you a stronger and better developer.
After reading this guide we hope that you'll be able to:
* Understand the way Active Record fits into the MVC model.
* Create basic Active Record models and map them with your database tables.
* Use your models to execute CRUD (Create, Read, Update and Delete) database operations.
* Follow the naming conventions used by Rails to make developing database applications easier and obvious.
* Take advantage of the way Active Record maps it's attributes with the database tables' columns to implement your application's logic.
* Use Active Record with legacy databases that do not follow the Rails naming conventions.
endprologue.
h3. What's Active Record?
Rails' ActiveRecord is an implementation of Martin Fowler's "Active Record Design Pattern":http://martinfowler.com/eaaCatalog/activeRecord.html. This pattern is based on the idea of creating relations between the database and the application in the following way:
* Each database table is mapped to a class.
* Each table column is mapped to an attribute of this class.
* Each instance of this class is mapped to a single row in the database table.
The definition of the Active Record pattern in Martin Fowler's words:
??An object that wraps a row in a database table or view, encapsulates the database access, and adds domain logic on that data.??
h3. Object Relational Mapping
The relation between databases and object-oriented software is called ORM, which is short for "Object Relational Mapping". The purpose of an ORM framework is to minimize the mismatch existent between relational databases and object-oriented software. In applications with a domain model, we have objects that represent both the state of the system and the behaviour of the real world elements that were modeled through these objects. Since we need to store the system's state somehow, we can use relational databases, which are proven to be an excelent approach to data management. Usually this may become a very hard thing to do, since we need to create an object-oriented model of everything that lives in the database, from simple columns to complicated relations between different tables. Doing this kind of thing by hand is a tedious and error prone job. This is where an ORM framework comes in.
h3. ActiveRecord as an ORM framework
ActiveRecord gives us several mechanisms, being the most important ones the hability to:
* Represent models.
* Represent associations between these models.
* Represent inheritance hierarquies through related models.
* Validate models before they get recorded to the database.
* Perform database operations in an object-oriented fashion.
It's easy to see that the Rails Active Record implementation goes way beyond the basic description of the Active Record Pattern.
h3. Active Record inside the MVC model
Active Record plays the role of model inside the MVC structure followed by Rails applications. Since model objects should encapsulate both state and logic of your applications, it's ActiveRecord responsability to deliver you the easiest possible way to recover this data from the database.
h3. Convention over Configuration in ActiveRecord
When writing applications using other programming languages or frameworks, it may be necessary to write a lot of configuration code. This is particulary true for ORM frameworks in general. However, if you follow the conventions adopted by Rails, you'll need to write very little configuration (in some case no configuration at all) when creating ActiveRecord models. The idea is that if you configure your applications in the very same way most of the times then this should be the default way. In this cases, explicity configuration would be needed only in those cases where you can't follow the conventions for any reason.
h4. Naming Conventions
By default, ActiveRecord uses some naming conventions to find out how the mapping between models and database tables should be created. Rails will pluralize your class names to find the respective database table. So, for a class +Book+, you should have a database table called *books*. The Rails pluralization mechanisms are very powerful, being capable to pluralize (and singularize) both regular and irregular words. When using class names composed of two or more words, the model class name should follow the Ruby conventions, using the camelCase form, while the table name must contain the words separated by underscores. Examples:
* Database Table - Plural with underscores separating words i.e. (book_clubs)
* Model Class - Singular with the first letter of each word capitalized i.e. (BookClub)
|_.Model / Class |_.Table / Schema |
|Post |posts|
|LineItem |line_items|
|Deer |deer|
|Mouse |mice|
|Person |people|
h4. Schema Conventions
ActiveRecord uses naming conventions for the columns in database tables, depending on the purpose of these columns.
* *Foreign keys* - These fields should be named following the pattern table_id i.e. (item_id, order_id). These are the fields that ActiveRecord will look for when you create associations between your models.
* *Primary keys* - By default, ActiveRecord will use a integer column named "id" as the table's primary key. When using "Rails Migrations":http://guides.rails.info/migrations.html to create your tables, this column will be automaticaly created.
There are also some optional column names that will create additional features to ActiveRecord instances:
* *created_at / created_on* - ActiveRecord will store the current date and time to this field when creating the record.
* *updated_at / updated_on* - ActiveRecord will store the current date and times to this field when updating the record.
* *lock_version* - Adds "optimistic locking":http://api.rubyonrails.com/classes/ActiveRecord/Locking.html to a model.
* *type* - Specifies that the model uses "Single Table Inheritance":http://api.rubyonrails.com/classes/ActiveRecord/Base.html
* *(table_name)_count* - Used to cache the number of belonging objects on associations. For example, a +comments_count+ column in a +Post+ class that has many instances of +Comment+ will cache the number of existent comments for each post.
NOTE: While these column names are optional they are in fact reserved by ActiveRecord. Steer clear of reserved keywords unless you want the extra functionality. For example, "type" is a reserved keyword used to designate a table using Single Table Inheritance. If you are not using STI, try an analogous keyword like "context", that may still accurately describe the data you are modeling.
h3. Creating ActiveRecord models
It's very easy to create ActiveRecord models. All you have to do is to subclass the ActiveRecord::Base class and you're good to go:
<ruby>
class Product < ActiveRecord::Base; end
</ruby>
This will create a +Product+ model, mapped to a *products* table at the database. By doing this you'll also have the hability to map the columns of each row in that table with the attributes of the instances of your model. So, suppose that the *products* table was created using a SQL sentence like:
<sql>
CREATE TABLE products (
id int(11) NOT NULL auto_increment,
name varchar(255),
PRIMARY KEY (id)
);
</sql>
Following the table schema above, you would be able to write code like the following:
<ruby>
p = Product.new
p.name = "Some Book"
puts p.name # "Some Book"
</ruby>
h3. Overriding the naming conventions
What if you need to follow a different naming convention or need to use your Rails application with a legacy database? No problem, you can easily override the default conventions.
You can use the +ActiveRecord::Base.set_table_name+ method to specify the table name that should be used:
<ruby>
class Product < ActiveRecord::Base
set_table_name "PRODUCT"
end
</ruby>
It's also possible to override the column that should be used as the table's primary key. Use the +ActiveRecord::Base.set_primary_key+ method for that:
<ruby>
class Product < ActiveRecord::Base
set_primary_key "product_id"
end
</ruby>
h3. Validations
ActiveRecord gives the hability to validate the state of your models before they get recorded into the database. There are several methods that you can use to hook into the lifecycle of your models and validate that an attribute value is not empty or follow a specific format and so on. You can learn more about validations in the "Active Record Validations and Callbacks guide":http://guides.rails.info/activerecord_validations_callbacks.html#_overview_of_activerecord_validation.
h3. Callbacks
ActiveRecord callbacks allow you to attach code to certain events in the lifecycle of your models. This way you can add behaviour to your models by transparently executing code when those events occur, like when you create a new record, update it, destroy it and so on. You can learn more about callbacks in the "Active Record Validations and Callbacks guide":http://guides.rails.info/activerecord_validations_callbacks.html#_callbacks.

View File

@ -0,0 +1,704 @@
h2. Active Record Query Interface
This guide covers different ways to retrieve data from the database using Active Record. By referring to this guide, you will be able to:
* Find records using a variety of methods and conditions
* Specify the order, retrieved attributes, grouping, and other properties of the found records
* Use eager loading to reduce the number of database queries needed for data retrieval
* Use dynamic finders methods
* Create named scopes to add custom finding behavior to your models
* Check for the existence of particular records
* Perform various calculations on Active Record models
endprologue.
If you're used to using raw SQL to find database records then, generally, you will find that there are better ways to carry out the same operations in Rails. Active Record insulates you from the need to use SQL in most cases.
Code examples throughout this guide will refer to one or more of the following models:
<ruby>
class Client < ActiveRecord::Base
has_one :address
has_one :mailing_address
has_many :orders
has_and_belongs_to_many :roles
end
</ruby>
<ruby>
class Address < ActiveRecord::Base
belongs_to :client
end
</ruby>
<ruby>
class MailingAddress < Address
end
</ruby>
<ruby>
class Order < ActiveRecord::Base
belongs_to :client, :counter_cache => true
end
</ruby>
<ruby>
class Role < ActiveRecord::Base
has_and_belongs_to_many :clients
end
</ruby>
bq. Active Record will perform queries on the database for you and is compatible with most database systems (MySQL, PostgreSQL and SQLite to name a few). Regardless of which database system you're using, the Active Record method format will always be the same.
h3. Retrieving objects
To retrieve objects from the database, Active Record provides a primary method called +find+. This method allows you to pass arguments into it to perform certain queries on your database without the need of SQL. If you wanted to find the record with the id of 1, you could type +Client.find(1)+ which would execute this query on your database:
<sql>
SELECT * FROM clients WHERE (clients.id = 1)
</sql>
NOTE: Because this is a standard table created from a migration in Rails, the primary key is defaulted to 'id'. If you have specified a different primary key in your migrations, this is what Rails will find on when you call the find method, not the id column.
If you wanted to find clients with id 1 or 2, you call +Client.find([1,2])+ or +Client.find(1,2)+ and then this will be executed as:
<sql>
SELECT * FROM clients WHERE (clients.id IN (1,2))
</sql>
<shell>
>> Client.find(1,2)
=> [#<Client id: 1, name: => "Ryan", locked: false, orders_count: 2,
created_at: "2008-09-28 15:38:50", updated_at: "2008-09-28 15:38:50">,
#<Client id: 2, name: => "Michael", locked: false, orders_count: 3,
created_at: "2008-09-28 13:12:40", updated_at: "2008-09-28 13:12:40">]
</shell>
Note that if you pass in a list of numbers that the result will be returned as an array, not as a single Client object.
NOTE: If +find(id)+ or +find([id1, id2])+ fails to find any records, it will raise a RecordNotFound exception.
If you wanted to find the first Client object you would simply type +Client.first+ and that would find the first client in your clients table:
<shell>
>> Client.first
=> #<Client id: 1, name: => "Ryan", locked: false, orders_count: 2,
created_at: "2008-09-28 15:38:50", updated_at: "2008-09-28 15:38:50">
</shell>
If you were reading your log file (the default is log/development.log) you may see something like this:
<sql>
SELECT * FROM clients LIMIT 1
</sql>
Indicating the query that Rails has performed on your database.
To find the last Client object you would simply type +Client.last+ and that would find the last client created in your clients table:
<shell>
>> Client.last
=> #<Client id: 2, name: => "Michael", locked: false, orders_count: 3,
created_at: "2008-09-28 13:12:40", updated_at: "2008-09-28 13:12:40">
</shell>
If you were reading your log file (the default is log/development.log) you may see something like this:
<sql>
SELECT * FROM clients ORDER BY id DESC LIMIT 1
</sql>
NOTE: Please be aware that the syntax that Rails uses to find the first record in the table means that it may not be the actual first record. If you want the actual first record based on a field in your table (e.g. +created_at+) specify an order option in your find call. The last method call works differently: it finds the last record on your table based on the primary key column.
<sql>
SELECT * FROM clients ORDER BY clients.id DESC LIMIT 1
</sql>
To find all the Client objects you would simply type +Client.all+ and that would find all the clients in your clients table:
<shell>
>> Client.all
=> [#<Client id: 1, name: => "Ryan", locked: false, orders_count: 2,
created_at: "2008-09-28 15:38:50", updated_at: "2008-09-28 15:38:50">,
#<Client id: 2, name: => "Michael", locked: false, orders_count: 3,
created_at: "2008-09-28 13:12:40", updated_at: "2008-09-28 13:12:40">]
</shell>
You may see in Rails code that there are calls to methods such as +Client.find(:all)+, +Client.find(:first)+ and +Client.find(:last)+. These methods are just alternatives to +Client.all+, +Client.first+ and +Client.last+ respectively.
Be aware that +Client.first+/+Client.find(:first)+ and +Client.last+/+Client.find(:last)+ will both return a single object, where as +Client.all+/+Client.find(:all)+ will return an array of Client objects, just as passing in an array of ids to +find+ will do also.
h3. Conditions
The +find+ method allows you to specify conditions to limit the records returned. You can specify conditions as a string, array, or hash.
h4. Pure String Conditions
If you'd like to add conditions to your find, you could just specify them in there, just like +Client.first(:conditions => "orders_count = '2'")+. This will find all clients where the +orders_count+ field's value is 2.
WARNING: Building your own conditions as pure strings can leave you vulnerable to SQL injection exploits. For example, +Client.first(:conditions => "name LIKE '%#{params[:name]}%'")+ is not safe. See the next section for the preferred way to handle conditions using an array.
h4. Array Conditions
Now what if that number could vary, say as a argument from somewhere, or perhaps from the user's level status somewhere? The find then becomes something like +Client.first(:conditions => ["orders_count = ?", params[:orders]])+. Active Record will go through the first element in the conditions value and any additional elements will replace the question marks (?) in the first element. If you want to specify two conditions, you can do it like +Client.first(:conditions => ["orders_count = ? AND locked = ?", params[:orders], false])+. In this example, the first question mark will be replaced with the value in +params[:orders]+ and the second will be replaced with the SQL representation of +false+, which depends on the adapter.
The reason for doing code like:
<ruby>
Client.first(:conditions => ["orders_count = ?", params[:orders]])
</ruby>
instead of:
<ruby>
Client.first(:conditions => "orders_count = #{params[:orders]}")
</ruby>
is because of argument safety. Putting the variable directly into the conditions string will pass the variable to the database *as-is*. This means that it will be an unescaped variable directly from a user who may have malicious intent. If you do this, you put your entire database at risk because once a user finds out he or she can exploit your database they can do just about anything to it. Never ever put your arguments directly inside the conditions string.
TIP: For more information on the dangers of SQL injection, see the "Ruby on Rails Security Guide":../security.html#_sql_injection.
If you're looking for a range inside of a table (for example, users created in a certain timeframe) you can use the conditions option coupled with the IN sql statement for this. If you had two dates coming in from a controller you could do something like this to look for a range:
<ruby>
Client.all(:conditions => ["created_at IN (?)",
(params[:start_date].to_date)..(params[:end_date].to_date)])
</ruby>
This would generate the proper query which is great for small ranges but not so good for larger ranges. For example if you pass in a range of date objects spanning a year that's 365 (or possibly 366, depending on the year) strings it will attempt to match your field against.
<sql>
SELECT * FROM users WHERE (created_at IN
('2007-12-31','2008-01-01','2008-01-02','2008-01-03','2008-01-04','2008-01-05',
'2008-01-06','2008-01-07','2008-01-08','2008-01-09','2008-01-10','2008-01-11',
'2008-01-12','2008-01-13','2008-01-14','2008-01-15','2008-01-16','2008-01-17',
'2008-01-18','2008-01-19','2008-01-20','2008-01-21','2008-01-22','2008-01-23',...
2008-12-15','2008-12-16','2008-12-17','2008-12-18','2008-12-19','2008-12-20',
'2008-12-21','2008-12-22','2008-12-23','2008-12-24','2008-12-25','2008-12-26',
'2008-12-27','2008-12-28','2008-12-29','2008-12-30','2008-12-31'))
</sql>
Things can get *really* messy if you pass in Time objects as it will attempt to compare your field to *every second* in that range:
<ruby>
Client.all(:conditions => ["created_at IN (?)",
(params[:start_date].to_date.to_time)..(params[:end_date].to_date.to_time)])
</ruby>
<sql>
SELECT * FROM users WHERE (created_at IN
('2007-12-01 00:00:00', '2007-12-01 00:00:01' ...
'2007-12-01 23:59:59', '2007-12-02 00:00:00'))
</sql>
This could possibly cause your database server to raise an unexpected error, for example MySQL will throw back this error:
<shell>
Got a packet bigger than 'max_allowed_packet' bytes: _query_
</shell>
Where _query_ is the actual query used to get that error.
In this example it would be better to use greater-than and less-than operators in SQL, like so:
<ruby>
Client.all(:conditions =>
["created_at > ? AND created_at < ?", params[:start_date], params[:end_date]])
</ruby>
You can also use the greater-than-or-equal-to and less-than-or-equal-to like this:
<ruby>
Client.all(:conditions =>
["created_at >= ? AND created_at <= ?", params[:start_date], params[:end_date]])
</ruby>
Just like in Ruby. If you want a shorter syntax be sure to check out the "Hash Conditions":hash-conditions section later on in the guide.
h4. Placeholder Conditions
Similar to the array style of params you can also specify keys in your conditions:
<ruby>
Client.all(:conditions =>
["created_at >= :start_date AND created_at <= :end_date", { :start_date => params[:start_date], :end_date => params[:end_date] }])
</ruby>
This makes for clearer readability if you have a large number of variable conditions.
h4. Hash Conditions
Rails also allows you to pass in a hash conditions which can increase the readability of your conditions syntax. With hash conditions, you pass in a hash with keys of the fields you want conditionalised and the values of how you want to conditionalise them:
<ruby>
Client.all(:conditions => { :locked => true })
</ruby>
The field name does not have to be a symbol it can also be a string:
<ruby>
Client.all(:conditions => { 'locked' => true })
</ruby>
The good thing about this is that we can pass in a range for our fields without it generating a large query as shown in the preamble of this section.
<ruby>
Client.all(:conditions => { :created_at => (Time.now.midnight - 1.day)..Time.now.midnight})
</ruby>
This will find all clients created yesterday by using a BETWEEN sql statement:
<sql>
SELECT * FROM clients WHERE (clients.created_at BETWEEN '2008-12-21 00:00:00' AND '2008-12-22 00:00:00')
</sql>
This demonstrates a shorter syntax for the examples in "Array Conditions":#array-conditions
You can also join in tables and specify their columns in the hash:
<ruby>
Client.all(:include => "orders", :conditions => { 'orders.created_at' => (Time.now.midnight - 1.day)..Time.now.midnight })
</ruby>
An alternative and cleaner syntax to this is:
<ruby>
Client.all(:include => "orders", :conditions => { :orders => { :created_at => (Time.now.midnight - 1.day)..Time.now.midnight } })
</ruby>
This will find all clients who have orders that were created yesterday, again using a BETWEEN expression.
If you want to find records using the IN expression you can pass an array to the conditions hash:
<ruby>
Client.all(:include => "orders", :conditions => { :orders_count => [1,3,5] }
</ruby>
This code will generate SQL like this:
<sql>
SELECT * FROM clients WHERE (clients.orders_count IN (1,2,3))
</sql>
h3. Ordering
If you're getting a set of records and want to order them in ascending order by the +created_at+ field in your table, you can use +Client.all(:order => "created_at")+. If you'd like to order it in descending order, just tell it to do that using +Client.all(:order => "created_at desc")+. The value for this option is passed in as sanitized SQL and allows you to sort via multiple fields: +Client.all(:order => "created_at desc, orders_count asc")+.
h3. Selecting Certain Fields
To select certain fields, you can use the select option like this: +Client.first(:select => "viewable_by, locked")+. This select option does not use an array of fields, but rather requires you to type SQL-like code. The above code will execute +SELECT viewable_by, locked FROM clients LIMIT 1+ on your database.
Be careful because this also means you're initializing a model object with only the fields that you've selected. If you attempt to access a field that is not in the initialized record you'll receive:
<shell>
ActiveRecord::MissingAttributeError: missing attribute: <attribute>
</shell>
Where <attribute> is the atrribute you asked for. The +id+ method will not raise the +ActiveRecord::MissingAttributeError+, so just be careful when working with associations because they need the +id+ method to function properly.
You can also call SQL functions within the select option. For example, if you would like to only grab a single record per unique value in a certain field by using the +DISTINCT+ function you can do it like this: +Client.all(:select => "DISTINCT(name)")+.
h3. Limit & Offset
If you want to limit the amount of records to a certain subset of all the records retrieved you usually use limit for this, sometimes coupled with offset. Limit is the maximum number of records that will be retrieved from a query, and offset is the number of records it will start reading from from the first record of the set. Take this code for example:
<ruby>
Client.all(:limit => 5)
</ruby>
This code will return a maximum of 5 clients and because it specifies no offset it will return the first 5 clients in the table. The SQL it executes will look like this:
<sql>
SELECT * FROM clients LIMIT 5
</sql>
<ruby>
Client.all(:limit => 5, :offset => 5)
</ruby>
This code will return a maximum of 5 clients and because it specifies an offset this time, it will return these records starting from the 5th client in the clients table. The SQL looks like:
<sql>
SELECT * FROM clients LIMIT 5, 5
</sql>
h3. Group
The group option for find is useful, for example, if you want to find a collection of the dates orders were created on. You could use the option in this context:
<ruby>
Order.all(:group => "date(created_at)", :order => "created_at")
</ruby>
And this will give you a single +Order+ object for each date where there are orders in the database.
The SQL that would be executed would be something like this:
<sql>
SELECT * FROM orders GROUP BY date(created_at)
</sql>
h3. Having
The +:having+ option allows you to specify SQL and acts as a kind of a filter on the group option. +:having+ can only be specified when +:group+ is specified.
An example of using it would be:
<ruby>
Order.all(:group => "date(created_at)", :having => ["created_at > ?", 1.month.ago])
</ruby>
This will return single order objects for each day, but only for the last month.
h3. Read Only
+readonly+ is a +find+ option that you can set in order to make that instance of the record read-only. Any attempt to alter or destroy the record will not succeed, raising an ActiveRecord::ReadOnlyRecord exception. To set this option, specify it like this:
<ruby>
Client.first(:readonly => true)
</ruby>
If you assign this record to a variable client, calling the following code will raise an ActiveRecord::ReadOnlyRecord exception:
<ruby>
client = Client.first(:readonly => true)
client.locked = false
client.save
</ruby>
h3. Lock
If you're wanting to stop race conditions for a specific record (for example, you're incrementing a single field for a record, potentially from multiple simultaneous connections) you can use the lock option to ensure that the record is updated correctly. For safety, you should use this inside a transaction.
<ruby>
Topic.transaction do
t = Topic.find(params[:id], :lock => true)
t.increment!(:views)
end
</ruby>
You can also pass SQL to this option to allow different types of locks. For example, MySQL has an expression called LOCK IN SHARE MODE where you can lock a record but still allow other queries to read it. To specify this expression just pass it in as the lock option:
<ruby>
Topic.transaction do
t = Topic.find(params[:id], :lock => "LOCK IN SHARE MODE")
t.increment!(:views)
end
</ruby>
h3. Making It All Work Together
You can chain these options together in no particular order as Active Record will write the correct SQL for you. If you specify two instances of the same options inside the +find+ method Active Record will use the last one you specified. This is because the options passed to find are a hash and defining the same key twice in a hash will result in the last definition being used.
h3. Eager Loading
Eager loading is loading associated records along with any number of records in as few queries as possible. For example, if you wanted to load all the addresses associated with all the clients in a single query you could use +Client.all(:include => :address)+. If you wanted to include both the address and mailing address for the client you would use +Client.find(:all, :include => [:address, :mailing_address])+. Include will first find the client records and then load the associated address records. Running script/server in one window, and executing the code through script/console in another window, the output should look similar to this:
<sql>
Client Load (0.000383) SELECT * FROM clients
Address Load (0.119770) SELECT addresses.* FROM addresses
WHERE (addresses.client_id IN (13,14))
MailingAddress Load (0.001985) SELECT mailing_addresses.* FROM
mailing_addresses WHERE (mailing_addresses.client_id IN (13,14))
</sql>
The numbers +13+ and +14+ in the above SQL are the ids of the clients gathered from the +Client.all+ query. Rails will then run a query to gather all the addresses and mailing addresses that have a client_id of 13 or 14. Although this is done in 3 queries, this is more efficient than not eager loading because without eager loading it would run a query for every time you called +address+ or +mailing_address+ on one of the objects in the clients array, which may lead to performance issues if you're loading a large number of records at once and is often called the "N+1 query problem". The problem is that the more queries your server has to execute, the slower it will run.
If you wanted to get all the addresses for a client in the same query you would do +Client.all(:joins => :address)+.
If you wanted to find the address and mailing address for that client you would do +Client.all(:joins => [:address, :mailing_address])+. This is more efficient because it does all the SQL in one query, as shown by this example:
<sql>
+Client Load (0.000455) SELECT clients.* FROM clients INNER JOIN addresses
ON addresses.client_id = client.id INNER JOIN mailing_addresses ON
mailing_addresses.client_id = client.id
</sql>
This query is more efficent, but there's a gotcha: if you have a client who does not have an address or a mailing address they will not be returned in this query at all. If you have any association as an optional association, you may want to use include rather than joins. Alternatively, you can use a SQL join clause to specify exactly the join you need (Rails always assumes an inner join):
<ruby>
Client.all(:joins => “LEFT OUTER JOIN addresses ON
client.id = addresses.client_id LEFT OUTER JOIN mailing_addresses ON
client.id = mailing_addresses.client_id”)
</ruby>
When using eager loading you can specify conditions for the columns of the tables inside the eager loading to get back a smaller subset. If, for example, you want to find a client and all their orders within the last two weeks you could use eager loading with conditions for this:
<ruby>
Client.first(:include => "orders", :conditions =>
["orders.created_at >= ? AND orders.created_at <= ?", 2.weeks.ago, Time.now])
</ruby>
h3. Dynamic finders
For every field (also known as an attribute) you define in your table, Active Record provides a finder method. If you have a field called +name+ on your Client model for example, you get +find_by_name+ and +find_all_by_name+ for free from Active Record. If you have also have a +locked+ field on the Client model, you also get +find_by_locked+ and +find_all_by_locked+.
You can do +find_last_by_*+ methods too which will find the last record matching your argument.
You can specify an exclamation point (!) on the end of the dynamic finders to get them to raise an ActiveRecord::RecordNotFound error if they do not return any records, like +Client.find_by_name!("Ryan")+
If you want to find both by name and locked, you can chain these finders together by simply typing +and+ between the fields for example +Client.find_by_name_and_locked("Ryan", true)+.
There's another set of dynamic finders that let you find or create/initialize objects if they aren't found. These work in a similar fashion to the other finders and can be used like +find_or_create_by_name(params[:name])+. Using this will firstly perform a find and then create if the find returns nil. The SQL looks like this for +Client.find_or_create_by_name("Ryan")+:
<sql>
SELECT * FROM clients WHERE (clients.name = 'Ryan') LIMIT 1
BEGIN
INSERT INTO clients (name, updated_at, created_at, orders_count, locked)
VALUES('Ryan', '2008-09-28 15:39:12', '2008-09-28 15:39:12', 0, '0')
COMMIT
</sql>
+find_or_create+'s sibling, +find_or_initialize+, will find an object and if it does not exist will act similar to calling +new+ with the arguments you passed in. For example:
<ruby>
client = Client.find_or_initialize_by_name('Ryan')
</ruby>
will either assign an existing client object with the name 'Ryan' to the client local variable, or initialize a new object similar to calling +Client.new(:name => 'Ryan')+. From here, you can modify other fields in client by calling the attribute setters on it: +client.locked = true+ and when you want to write it to the database just call +save+ on it.
h3. Finding By SQL
If you'd like to use your own SQL to find records in a table you can use +find_by_sql+. The +find_by_sql+ method will return an array of objects even the underlying query returns just a single record. For example you could run this query:
<ruby>
Client.find_by_sql("SELECT * FROM clients INNER JOIN orders ON clients.id = orders.client_id ORDER clients.created_at desc")
</ruby>
+find_by_sql+ provides you with a simple way of making custom calls to the database and retrieving instantiated objects.
h3. select_all
+find_by_sql+ has a close relative called +connection#select_all+. +select_all+ will retrieve objects from the database using custom SQL just like +find_by_sql+ but will not instantiate them. Instead, you will get an array of hashes where each hash indicates a record.
<ruby>
Client.connection.select_all("SELECT * FROM clients WHERE id = '1'")
</ruby>
h3. Working with Associations
When you define a has_many association on a model you get the +find+ method and dynamic finders also on that association. This is helpful for finding associated records within the scope of an existing record, for example finding all the orders for a client that have been sent and not received by doing something like +Client.find(params[:id]).orders.find_by_sent_and_received(true, false)+. Having this find method available on associations is extremely helpful when using nested resources.
h3. Named Scopes
Named scopes are another way to add custom finding behavior to the models in the application. Named scopes provide an object-oriented way to narrow the results of a query.
h4. Simple Named Scopes
Suppose we want to find all clients who are male. You could use this code:
<ruby>
class Client < ActiveRecord::Base
named_scope :males, :conditions => { :gender => "male" }
end
</ruby>
Then you could call +Client.males.all+ to get all the clients who are male. Please note that if you do not specify the +all+ on the end you will get a +Scope+ object back, not a set of records which you do get back if you put the +all+ on the end.
If you wanted to find all the clients who are active, you could use this:
<ruby>
class Client < ActiveRecord::Base
named_scope :active, :conditions => { :active => true }
end
</ruby>
You can call this new named_scope with +Client.active.all+ and this will do the same query as if we just used +Client.all(:conditions => ["active = ?", true])+. If you want to find the first client within this named scope you could do +Client.active.first+.
h4. Combining Named Scopes
If you wanted to find all the clients who are active and male you can stack the named scopes like this:
<ruby>
Client.males.active.all
</ruby>
If you would then like to do a +all+ on that scope, you can. Just like an association, named scopes allow you to call +all+ on them:
<ruby>
Client.males.active.all(:conditions => ["age > ?", params[:age]])
</ruby>
h4. Runtime Evaluation of Named Scope Conditions
Consider the following code:
<ruby>
class Client < ActiveRecord::Base
named_scope :recent, :conditions => { :created_at > 2.weeks.ago }
end
</ruby>
This looks like a standard named scope that defines a method called +recent+ which gathers all records created any time between now and 2 weeks ago. That's correct for the first time the model is loaded but for any time after that, +2.weeks.ago+ is set to that same value, so you will consistently get records from a certain date until your model is reloaded by something like your application restarting. The way to fix this is to put the code in a lambda block:
<ruby>
class Client < ActiveRecord::Base
named_scope :recent, lambda { { :conditions => ["created_at > ?", 2.weeks.ago] } }
end
</ruby>
And now every time the +recent+ named scope is called, the code in the lambda block will be executed, so you'll get actually 2 weeks ago from the code execution, not 2 weeks ago from the time the model was loaded.
h4. Named Scopes with Multiple Models
In a named scope you can use +:include+ and +:joins+ options just like in +find+.
<ruby>
class Client < ActiveRecord::Base
named_scope :active_within_2_weeks, :joins => :order,
lambda { { :conditions => ["orders.created_at > ?", 2.weeks.ago] } }
end
</ruby>
This method, called as +Client.active_within_2_weeks.all+, will return all clients who have placed orders in the past 2 weeks.
h4. Arguments to Named Scopes
If you want to pass to a named scope a required arugment, just specify it as a block argument like this:
<ruby>
class Client < ActiveRecord::Base
named_scope :recent, lambda { |time| { :conditions => ["created_at > ?", time] } }
end
</ruby>
This will work if you call +Client.recent(2.weeks.ago).all+ but not if you call +Client.recent+. If you want to add an optional argument for this, you have to use prefix the arugment with an *.
<ruby>
class Client < ActiveRecord::Base
named_scope :recent, lambda { |*args| { :conditions => ["created_at > ?", args.first || 2.weeks.ago] } }
end
</ruby>
This will work with +Client.recent(2.weeks.ago).all+ and +Client.recent.all+, with the latter always returning records with a created_at date between right now and 2 weeks ago.
Remember that named scopes are stackable, so you will be able to do +Client.recent(2.weeks.ago).unlocked.all+ to find all clients created between right now and 2 weeks ago and have their locked field set to false.
h4. Anonymous Scopes
All Active Record models come with a named scope named +scoped+, which allows you to create anonymous scopes. For example:
<ruby>
class Client < ActiveRecord::Base
def self.recent
scoped :conditions => ["created_at > ?", 2.weeks.ago]
end
end
</ruby>
Anonymous scopes are most useful to create scopes "on the fly":
<ruby>
Client.scoped(:conditions => { :gender => "male" })
</ruby>
Just like named scopes, anonymous scopes can be stacked, either with other anonymous scopes or with regular named scopes.
h3. Existence of Objects
If you simply want to check for the existence of the object there's a method called +exists?+. This method will query the database using the same query as +find+, but instead of returning an object or collection of objects it will return either +true+ or false+.
<ruby>
Client.exists?(1)
</ruby>
The +exists?+ method also takes multiple ids, but the catch is that it will return true if any one of those records exists.
<ruby>
Client.exists?(1,2,3)
# or
Client.exists?([1,2,3])
</ruby>
Further more, +exists+ takes a +conditions+ option much like find:
<ruby>
Client.exists?(:conditions => "first_name = 'Ryan'")
</ruby>
h3. Calculations
This section uses count as an example method in this preamble, but the options described apply to all sub-sections.
+count+ takes conditions much in the same way +exists?+ does:
<ruby>
Client.count(:conditions => "first_name = 'Ryan'")
</ruby>
Which will execute:
<sql>
SELECT count(*) AS count_all FROM clients WHERE (first_name = 'Ryan')
</sql>
You can also use +:include+ or +:joins+ for this to do something a little more complex:
<ruby>
Client.count(:conditions => "clients.first_name = 'Ryan' AND orders.status = 'received'", :include => "orders")
</ruby>
Which will execute:
<sql>
SELECT count(DISTINCT clients.id) AS count_all FROM clients
LEFT OUTER JOIN orders ON orders.client_id = client.id WHERE
(clients.first_name = 'Ryan' AND orders.status = 'received')
</sql>
This code specifies +clients.first_name+ just in case one of the join tables has a field also called +first_name+ and it uses +orders.status+ because that's the name of our join table.
h4. Count
If you want to see how many records are in your model's table you could call +Client.count+ and that will return the number. If you want to be more specific and find all the clients with their age present in the database you can use +Client.count(:age)+.
For options, please see the parent section, "Calculations":#calculations.
h4. Average
If you want to see the average of a certain number in one of your tables you can call the +average+ method on the class that relates to the table. This method call will look something like this:
<ruby>
Client.average("orders_count")
</ruby>
This will return a number (possibly a floating point number such as 3.14159265) representing the average value in the field.
For options, please see the parent section, "Calculations":#calculations.
h4. Minimum
If you want to find the minimum value of a field in your table you can call the +minimum+ method on the class that relates to the table. This method call will look something like this:
<ruby>
Client.minimum("age")
</ruby>
For options, please see the parent section, "Calculations":#calculations.
h4. Maximum
If you want to find the maximum value of a field in your table you can call the +maximum+ method on the class that relates to the table. This method call will look something like this:
<ruby>
Client.maximum("age")
</ruby>
For options, please see the parent section, "Calculations":#calculations.
h4. Sum
If you want to find the sum of a field for all records in your table you can call the +sum+ method on the class that relates to the table. This method call will look something like this:
<ruby>
Client.sum("orders_count")
</ruby>
For options, please see the parent section, "Calculations":#calculations.
h3. Changelog
"Lighthouse ticket":http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/16
* December 29 2008: Initial version by Ryan Bigg

View File

@ -0,0 +1,921 @@
h2. Active Record Validations and Callbacks
This guide teaches you how to hook into the lifecycle of your Active Record objects. More precisely, you will learn how to validate the state of your objects before they go into the database as well as how to perform custom operations at certain points in the object lifecycle.
After reading this guide and trying out the presented concepts, we hope that you'll be able to:
* Use the built-in Active Record validation helpers
* Create your own custom validation methods
* Work with the error messages generated by the validation process
* Create callback methods to respond to events in the object lifecycle.
* Create special classes that encapsulate common behavior for your callbacks
* Create Rails Observers
endprologue.
# TODO consider starting with an overview of what validations and callbacks are, and the object lifecycle
# http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html
# http://api.rubyonrails.org/classes/ActiveRecord/Base.html
h3. Overview of ActiveRecord Validation
Before you dive into the detail of validations in Rails, you should understand a bit about how validations fit into the big picture. Why should you use validations? When do these validations take place?
h4. Why Use ActiveRecord Validations?
The main reason for validating your objects before they get into the database is to ensure that only valid data is recorded. It's important to be sure that an email address column only contains valid email addresses, or that the customer's name column will never be empty. Constraints like that keep your database organized and helps your application to work properly.
There are several ways that you could validate the data that goes to the database, including native database constraints, client-side validations, and model-level validations. Each of these has pros and cons:
* Using database constraints and/or stored procedures makes the validation mechanisms database-dependent and may turn your application into a hard to test and maintain beast. However, if your database is used by other applications, it may be a good idea to use some constraints also at the database level. Additionally, database-level validations can safely handle some things (such as uniqueness in heavily-used tables) that are problematic to implement from the application level.
* Implementing validations only at the client side can be difficult in web-based applications. Usually this kind of validation is done using javascript, which may be turned off in the user's browser, leading to invalid data getting inside your database. However, if combined with server side validation, client side validation may be useful, since the user can have a faster feedback from the application when trying to save invalid data.
* Using validation directly in your Active Record classes ensures that only valid data gets recorded, while still keeping the validation code in the right place, avoiding breaking the MVC pattern. Since the validation happens on the server side, the user cannot disable it, so it's also safer. It may be a hard and tedious work to implement some of the logic involved in your models' validations, but fear not: Active Record gives you the ability to easily create validations, providing built-in helpers for common validations while still allowing you to create your own validation methods.
# TODO consider adding a bullet point on validations in controllers, and why model validations should be preferred over complicated controllers.
# http://weblog.jamisbuck.org/2006/10/18/skinny-controller-fat-model
h4. When Does Validation Happen?
There are two kinds of Active Record objects: those that correspond to a row inside your database and those that do not. When you create a fresh object, using the +new+ method, that object does not belong to the database yet. Once you call +save+ upon that object it will be saved into the appropriate database table. Active Record uses the +new_record?+ instance method to determine whether an object is already in the database or not. Consider the following simple Active Record class:
<ruby>
class Person < ActiveRecord::Base
end
</ruby>
We can see how it works by looking at some script/console output:
<shell>
>> p = Person.new(:name => "John Doe", :birthdate => Date.parse("09/03/1979"))
=> #<Person id: nil, name: "John Doe", birthdate: "1979-09-03", created_at: nil, updated_at: nil>
>> p.new_record?
=> true
>> p.save
=> true
>> p.new_record?
=> false
</shell>
Creating and saving a new record will send an SQL +INSERT+ operation to the database. Updating an existing record will send an SQL +UPDATE+ operation instead. Validations are typically run before these commands are sent to the database. If any validations fail, the object will be marked as invalid and Active Record will not trigger the +INSERT+ or +UPDATE+ operation. This helps to avoid storing an object in the database that's invalid. You can choose to have specific validations run when an object is created, saved, or updated.
CAUTION: There are many ways to change the state of an object in the database. Some methods will trigger validations, but some will not. This means that it's possible to save an object in the database in an invalid state if you aren't careful.
The following methods trigger validations, and will save the object to the database only if the object is valid. The bang versions (e.g. +save!+) will raise an exception if the record is invalid. The non-bang versions (e.g. +save+) simply return +false+.
* +create+
* +create!+
* +save+
* +save!+
* +update+
* +update_attributes+
* +update_attributes!+
h4. Skipping Validations
The following methods skip validations, and will save the object to the database regardless of its validity. They should be used with caution.
* +decrement!+
* +decrement_counter+
* +increment!+
* +increment_counter+
* +toggle!+
* +update_all+
* +update_attribute+
* +update_counters+
Note that +save+ also has the ability to skip validations (and callbacks!) if passed +false+. This technique should be used with caution.
* +save(false)+
h4. Object#valid? and Object#invalid?
To verify whether or not an object is valid, you can use the +valid?+ method. This runs validations and returns true if no errors were added to the object, and false otherwise.
<ruby>
class Person < ActiveRecord::Base
validates_presence_of :name
end
Person.create(:name => "John Doe").valid? # => true
Person.create.valid? # => false
</ruby>
When Active Record is performing validations, any errors found are collected into an +errors+ instance variable and can be accessed through an +errors+ instance method. An object is considered invalid if it has errors, and calling +save+ or +save!+ will not save it to the database.
However, note that an object instantiated with +new+ will not report errors even if it's technically invalid, because validations are not run when using +new+.
<ruby>
class Person < ActiveRecord::Base
validates_presence_of :name
end
>> p = Person.new
=> #<Person id: nil, name: nil>
>> p.errors
=> #<ActiveRecord::Errors:0x3b8b46c @base=#<Person id: nil, name: nil>, @errors={}>
>> p.valid?
=> false
>> p.errors
=> #<ActiveRecord::Errors:0x3b8b46c @base=#<Person id: nil, name: nil>, @errors={"name"=>["can't be blank"]}>
>> p = Person.create
=> #<Person id: nil, name: nil>
>> p.errors
=> #<ActiveRecord::Errors:0x3b8b46c @base=#<Person id: nil, name: nil>, @errors={"name"=>["can't be blank"]}>
>> p.save
=> false
>> p.save!
=> ActiveRecord::RecordInvalid: Validation failed: Name can't be blank
>> p = Person.create!
=> ActiveRecord::RecordInvalid: Validation failed: Name can't be blank
</ruby>
To verify whether or not a particular attribute of an object is valid, you can use the +invalid?+ method. This method is only useful _after_ validations have been run, because it only inspects the errors collection and does not trigger validations itself. It's different from the +valid?+ method because it doesn't verify the validity of the object as a whole, but only if there are errors found on an individual attribute of the object.
<ruby>
class Person < ActiveRecord::Base
validates_presence_of :name
end
>> Person.new.errors.invalid?(:name) # => false
>> Person.create.errors.invalid?(:name) # => true
</ruby>
h3. Declarative Validation Helpers
Active Record offers many pre-defined validation helpers that you can use directly inside your class definitions. These helpers create validation rules that are commonly used. Every time a validation fails, an error message is added to the object's +errors+ collection, and this message is associated with the field being validated.
Each helper accepts an arbitrary number of attributes identified by symbols, so with a single line of code you can add the same kind of validation to several attributes.
All these helpers accept the +:on+ and +:message+ options, which define when the validation should be applied and what message should be added to the +errors+ collection when it fails, respectively. The +:on+ option takes one of the values +:save+ (the default), +:create+ or +:update+. There is a default error message for each one of the validation helpers. These messages are used when the +:message+ option isn't used. Let's take a look at each one of the available helpers.
h4. validates_acceptance_of
Validates that a checkbox on the user interface was checked when a form was submitted. This is normally used when the user needs to agree to your application's terms of service, confirm reading some text, or any similar concept. This validation is very specific to web applications and actually this 'acceptance' does not need to be recorded anywhere in your database (if you don't have a field for it, the helper will just create a virtual attribute).
<ruby>
class Person < ActiveRecord::Base
validates_acceptance_of :terms_of_service
end
</ruby>
The default error message for +validates_acceptance_of+ is "_must be accepted_"
+validates_acceptance_of+ can receive an +:accept+ option, which determines the value that will be considered acceptance. It defaults to "1", but you can change this.
<ruby>
class Person < ActiveRecord::Base
validates_acceptance_of :terms_of_service, :accept => 'yes'
end
</ruby>
h4. validates_associated
You should use this helper when your model has associations with other models and they also need to be validated. When you try to save your object, +valid?+ will be called upon each one of the associated objects.
<ruby>
class Library < ActiveRecord::Base
has_many :books
validates_associated :books
end
</ruby>
This validation will work with all the association types.
CAUTION: Don't use +validates_associated+ on both ends of your associations, because this will lead to several recursive calls and blow up the method calls' stack.
The default error message for +validates_associated+ is "_is invalid_". Note that each associated object will contain its own +errors+ collection; errors do not bubble up to the calling model.
h4. validates_confirmation_of
You should use this helper when you have two text fields that should receive exactly the same content. For example, you may want to confirm an email address or a password. This validation creates a virtual attribute, using the name of the field that has to be confirmed with '_confirmation' appended.
<ruby>
class Person < ActiveRecord::Base
validates_confirmation_of :email
end
</ruby>
In your view template you could use something like
<erb>
<%= text_field :person, :email %>
<%= text_field :person, :email_confirmation %>
</erb>
NOTE: This check is performed only if +email_confirmation+ is not nil, and by default only on save. To require confirmation, make sure to add a presence check for the confirmation attribute (we'll take a look at +validates_presence_of+ later on this guide):
<ruby>
class Person < ActiveRecord::Base
validates_confirmation_of :email
validates_presence_of :email_confirmation
end
</ruby>
The default error message for +validates_confirmation_of+ is "_doesn't match confirmation_"
h4. validates_exclusion_of
This helper validates that the attributes' values are not included in a given set. In fact, this set can be any enumerable object.
<ruby>
class MovieFile < ActiveRecord::Base
validates_exclusion_of :format, :in => %w(mov avi),
:message => "Extension %s is not allowed"
end
</ruby>
The +validates_exclusion_of+ helper has an option +:in+ that receives the set of values that will not be accepted for the validated attributes. The +:in+ option has an alias called +:within+ that you can use for the same purpose, if you'd like to. This example uses the +:message+ option to show how you can personalize it with the current attribute's value, through the +%s+ format mask.
The default error message for +validates_exclusion_of+ is "_is not included in the list_".
h4. validates_format_of
This helper validates the attributes' values by testing whether they match a given pattern. This pattern must be specified using a Ruby regular expression, which is specified using the +:with+ option.
<ruby>
class Product < ActiveRecord::Base
validates_format_of :description, :with => /^[a-zA-Z]+$/,
:message => "Only letters allowed"
end
</ruby>
The default error message for +validates_format_of+ is "_is invalid_".
h4. validates_inclusion_of
This helper validates that the attributes' values are included in a given set. In fact, this set can be any enumerable object.
<ruby>
class Coffee < ActiveRecord::Base
validates_inclusion_of :size, :in => %w(small medium large),
:message => "%s is not a valid size"
end
</ruby>
The +validates_inclusion_of+ helper has an option +:in+ that receives the set of values that will be accepted. The +:in+ option has an alias called +:within+ that you can use for the same purpose, if you'd like to. The previous example uses the +:message+ option to show how you can personalize it with the current attribute's value, through the +%s+ format mask.
The default error message for +validates_inclusion_of+ is "_is not included in the list_".
h4. validates_length_of
This helper validates the length of your attribute's value. It includes a variety of different options, so you can specify length constraints in different ways:
<ruby>
class Person < ActiveRecord::Base
validates_length_of :name, :minimum => 2
validates_length_of :bio, :maximum => 500
validates_length_of :password, :in => 6..20
validates_length_of :registration_number, :is => 6
end
</ruby>
The possible length constraint options are:
* +:minimum+ - The attribute cannot have less than the specified length.
* +:maximum+ - The attribute cannot have more than the specified length.
* +:in+ (or +:within+) - The attribute length must be included in a given interval. The value for this option must be a Ruby range.
* +:is+ - The attribute length must be equal to a given value.
The default error messages depend on the type of length validation being performed. You can personalize these messages, using the +:wrong_length+, +:too_long+ and +:too_short+ options and the +%d+ format mask as a placeholder for the number corresponding to the length constraint being used. You can still use the +:message+ option to specify an error message.
<ruby>
class Person < ActiveRecord::Base
validates_length_of :bio, :too_long => "you're writing too much. %d characters is the maximum allowed."
end
</ruby>
The +validates_size_of+ helper is an alias for +validates_length_of+.
h4. validates_numericality_of
This helper validates that your attributes have only numeric values. By default, it will match an optional sign followed by a integral or floating point number. Using the +:integer_only+ option set to true, you can specify that only integral numbers are allowed.
If you set +:integer_only+ to +true+, then it will use the +$$/\A[+\-]?\d+\Z/+ regular expression to validate the attribute's value. Otherwise, it will try to convert the value to a number using +Kernel.Float+.
<ruby>
class Player < ActiveRecord::Base
validates_numericality_of :points
validates_numericality_of :games_played, :only_integer => true
end
</ruby>
Besides +:only_integer+, the +validates_numericality_of+ helper also accepts the following options to add constraints to acceptable values:
* +:greater_than+ - Specifies the value must be greater than the supplied value. The default error message for this option is "_must be greater than (value)_"
* +:greater_than_or_equal_to+ - Specifies the value must be greater than or equal the supplied value. The default error message for this option is "_must be greater than or equal to (value)_"
* +:equal_to+ - Specifies the value must be equal to the supplied value. The default error message for this option is "_must be equal to (value)_"
* +:less_than+ - Specifies the value must be less than the supplied value. The default error message for this option is "_must e less than (value)_"
* +:less_than_or_equal_to+ - Specifies the value must be less than or equal the supplied value. The default error message for this option is "_must be less or equal to (value)_"
* +:odd+ - Specifies the value must be an odd number if set to true. The default error message for this option is "_must be odd_"
* +:even+ - Specifies the value must be an even number if set to true. The default error message for this option is "_must be even_"
The default error message for +validates_numericality_of+ is "_is not a number_".
h4. validates_presence_of
This helper validates that the specified attributes are not empty. It uses the +blank?+ method to check if the value is either +nil+ or an empty string (if the string has only spaces, it will still be considered empty).
<ruby>
class Person < ActiveRecord::Base
validates_presence_of :name, :login, :email
end
</ruby>
NOTE: If you want to be sure that an association is present, you'll need to test whether the foreign key used to map the association is present, and not the associated object itself.
<ruby>
class LineItem < ActiveRecord::Base
belongs_to :order
validates_presence_of :order_id
end
</ruby>
NOTE: If you want to validate the presence of a boolean field (where the real values are true and false), you should use validates_inclusion_of :field_name, :in => [true, false] This is due to the way Object#blank? handles boolean values. false.blank? # => true
The default error message for +validates_presence_of+ is "_can't be empty_".
h4. validates_uniqueness_of
This helper validates that the attribute's value is unique right before the object gets saved. It does not create a uniqueness constraint directly into your database, so it may happen that two different database connections create two records with the same value for a column that you intend to be unique. To avoid that, you must create an unique index in your database.
<ruby>
class Account < ActiveRecord::Base
validates_uniqueness_of :email
end
</ruby>
The validation happens by performing a SQL query into the model's table, searching for a record where the attribute that must be validated is equal to the value in the object being validated.
There is a +:scope+ option that you can use to specify other attributes that are used to limit the uniqueness check:
<ruby>
class Holiday < ActiveRecord::Base
validates_uniqueness_of :name, :scope => :year,
:message => "Should happen once per year"
end
</ruby>
There is also a +:case_sensitive+ option that you can use to define whether the uniqueness constraint will be case sensitive or not. This option defaults to true.
<ruby>
class Person < ActiveRecord::Base
validates_uniqueness_of :name, :case_sensitive => false
end
</ruby>
The default error message for +validates_uniqueness_of+ is "_has already been taken_".
h4. validates_each
This helper validates attributes against a block. It doesn't have a predefined validation function. You should create one using a block, and every attribute passed to +validates_each+ will be tested against it. In the following example, we don't want names and surnames to begin with lower case.
<ruby>
class Person < ActiveRecord::Base
validates_each :name, :surname do |model, attr, value|
model.errors.add(attr, 'Must start with upper case') if value =~ /^[a-z]/
end
end
</ruby>
The block receives the model, the attribute's name and the attribute's value. You can do anything you like to check for valid data within the block. If your validation fails, you can add an error message to the model, therefore making it invalid.
h3. Common Validation Options
There are some common options that all the validation helpers can use. Here they are, except for the +:if+ and +:unless+ options, which are discussed later in the conditional validation topic.
h4. :allow_nil
The +:allow_nil+ option skips the validation when the value being validated is +nil+. You may be asking yourself if it makes any sense to use +:allow_nil+ and +validates_presence_of+ together. Well, it does. Remember, the validation will be skipped only for +nil+ attributes, but empty strings are not considered +nil+.
<ruby>
class Coffee < ActiveRecord::Base
validates_inclusion_of :size, :in => %w(small medium large),
:message => "%s is not a valid size", :allow_nil => true
end
</ruby>
h4. :allow_blank
The +:allow_blank+ option is similar to the +:allow_nil+ option. This option will let validation pass if the attribute's value is +nil+ or an empty string, i.e., any value that returns +true+ for +blank?+.
<ruby>
class Topic < ActiveRecord::Base
validates_length_of :title, :is => 5, :allow_blank => true
end
Topic.create("title" => "").valid? # => true
Topic.create("title" => nil).valid? # => true
</ruby>
h4. :message
As you've already seen, the +:message+ option lets you specify the message that will be added to the +errors+ collection when validation fails. When this option is not used, Active Record will use the respective default error message for each validation helper, together with the attribute name.
h4. :on
The +:on+ option lets you specify when the validation should happen. The default behavior for all the built-in validation helpers is to be ran on save (both when you're creating a new record and when you're updating it). If you want to change it, you can use +:on => :create+ to run the validation only when a new record is created or +:on => :update+ to run the validation only when a record is updated.
<ruby>
class Person < ActiveRecord::Base
# => it will be possible to update email with a duplicated value
validates_uniqueness_of :email, :on => :create
# => it will be possible to create the record with a 'non-numerical age'
validates_numericality_of :age, :on => :update
# => the default (validates on both create and update)
validates_presence_of :name, :on => :save
end
</ruby>
h3. Conditional validation
Sometimes it will make sense to validate an object just when a given predicate is satisfied. You can do that by using the +:if+ and +:unless+ options, which can take a symbol, a string or a Ruby Proc. You may use the +:if+ option when you want to specify when the validation *should* happen. If you want to specify when the validation *should not* happen, then you may use the +:unless+ option.
h4. Using a symbol with :if and :unless
You can associate the +:if+ and +:unless+ options with a symbol corresponding to the name of a method that will get called right before validation happens. This is the most commonly used option.
<ruby>
class Order < ActiveRecord::Base
validates_presence_of :card_number, :if => :paid_with_card?
def paid_with_card?
payment_type == "card"
end
end
</ruby>
h4. Using a string with the :if and :unless
You can also use a string that will be evaluated using +:eval+ and needs to contain valid Ruby code. You should use this option only when the string represents a really short condition.
<ruby>
class Person < ActiveRecord::Base
validates_presence_of :surname, :if => "name.nil?"
end
</ruby>
h4. Using a Proc object with :if and :unless
Finally, it's possible to associate +:if+ and +:unless+ with a Ruby Proc object which will be called. Using a Proc object can give you the ability to write a condition that will be executed only when the validation happens and not when your code is loaded by the Ruby interpreter. This option is best suited when writing short validation methods, usually one-liners.
<ruby>
class Account < ActiveRecord::Base
validates_confirmation_of :password,
:unless => Proc.new { |a| a.password.blank? }
end
</ruby>
h3. Writing your own validation methods
When the built-in validation helpers are not enough for your needs, you can write your own validation methods. You can do that by implementing methods that verify the state of your models and add messages to their +errors+ collection when they are invalid. You must then register those methods by using one or more of the +validate+, +validate_on_create+ or +validate_on_update+ class methods, passing in the symbols for the validation methods' names. You can pass more than one symbol for each class method and the respective validations will be ran in the same order as they were registered.
<ruby>
class Invoice < ActiveRecord::Base
validate :expiration_date_cannot_be_in_the_past,
:discount_cannot_be_more_than_total_value
def expiration_date_cannot_be_in_the_past
errors.add(:expiration_date, "can't be in the past") if
!expiration_date.blank? and expiration_date < Date.today
end
def discount_cannot_be_greater_than_total_value
errors.add(:discount, "can't be greater than total value") unless
discount <= total_value
end
end
</ruby>
You can even create your own validation helpers and reuse them in several different models. Here is an example where we create a custom validation helper to validate the format of fields that represent email addresses:
<ruby>
module ActiveRecord
module Validations
module ClassMethods
def validates_email_format_of(value)
validates_format_of value,
:with => /\A[\w\._%-]+@[\w\.-]+\.[a-zA-Z]{2,4}\z/,
:if => Proc.new { |u| !u.email.blank? },
:message => "Invalid format for email address"
end
end
end
end
</ruby>
The recipe is simple: just create a new validation method inside the +ActiveRecord::Validations::ClassMethods+ module. You can put this code in a file inside your application's *lib* folder, and then requiring it from your *environment.rb* or any other file inside *config/initializers*. You can use this helper like this:
<ruby>
class Person < ActiveRecord::Base
validates_email_format_of :email_address
end
</ruby>
h3. Manipulating the errors collection
# TODO consider renaming section to something more simple, like "Validation errors"
# Consider combining with new section at the top with Object#valid? and Object#invalid?
# Consider moving this stuff above the current 2-6 sections (e.g. save the validation details until after this?)
You can do more than just call +valid?+ upon your objects based on the existence of the +errors+ collection. Here is a list of the other available methods that you can use to manipulate errors or ask for an object's state.
* +add_to_base+ lets you add errors messages that are related to the object's state as a whole, instead of being related to a specific attribute. You can use this method when you want to say that the object is invalid, no matter the values of it's attributes. +add_to_base+ receives a string with the message.
<ruby>
class Person < ActiveRecord::Base
def a_method_used_for_validation_purposes
errors.add_to_base("This person is invalid because ...")
end
end
</ruby>
* +add+ lets you manually add messages that are related to particular attributes. When writing those messages, keep in mind that Rails will prepend them with the name of the attribute that holds the error, so write it in a way that makes sense. +add+ receives a symbol with the name of the attribute that you want to add the message to and the message itself.
<ruby>
class Person < ActiveRecord::Base
def a_method_used_for_validation_purposes
errors.add(:name, "can't have the characters !@#$%*()_-+=")
end
end
</ruby>
* +invalid?+ is used when you want to check if a particular attribute is invalid. It receives a symbol with the name of the attribute that you want to check.
<ruby>
class Person < ActiveRecord::Base
validates_presence_of :name, :email
end
person = Person.new(:name => "John Doe")
person.errors.invalid?(:email) # => true
</ruby>
* +on+ is used when you want to check the error messages for a specific attribute. It will return different kinds of objects depending on the state of the +errors+ collection for the given attribute. If there are no errors related to the attribute, +on+ will return +nil+. If there is just one errors message for this attribute, +on+ will return a string with the message. When +errors+ holds two or more error messages for the attribute, +on+ will return an array of strings, each one with one error message.
<ruby>
class Person < ActiveRecord::Base
validates_presence_of :name
validates_length_of :name, :minimum => 3
end
person = Person.new(:name => "John Doe")
person.valid? # => true
person.errors.on(:name) # => nil
person = Person.new(:name => "JD")
person.valid? # => false
person.errors.on(:name)
# => "is too short (minimum is 3 characters)"
person = Person.new
person.valid? # => false
person.errors.on(:name)
# => ["can't be blank", "is too short (minimum is 3 characters)"]
</ruby>
* +clear+ is used when you intentionally want to clear all the messages in the +errors+ collection. However, calling +errors.clear+ upon an invalid object won't make it valid: the +errors+ collection will now be empty, but the next time you call +valid?+ or any method that tries to save this object to the database, the validations will run. If any of them fails, the +errors+ collection will get filled again.
<ruby>
class Person < ActiveRecord::Base
validates_presence_of :name
validates_length_of :name, :minimum => 3
end
person = Person.new
person.valid? # => false
person.errors.on(:name)
# => ["can't be blank", "is too short (minimum is 3 characters)"]
person.errors.clear
person.errors.empty? # => true
p.save # => false
p.errors.on(:name)
# => ["can't be blank", "is too short (minimum is 3 characters)"]
</ruby>
# TODO consider discussing other methods (e.g. errors.size)
# http://api.rubyonrails.org/classes/ActiveRecord/Errors.html
h3. Using error collection in views
Rails provides built-in helpers to display the error messages of your models in your view templates. When creating a form with the form_for helper, you can use the error_messages method on the form builder to render all failed validation messages for the current model instance.
<ruby>
class Product < ActiveRecord::Base
validates_presence_of :description, :value
validates_numericality_of :value, :allow_nil => true
end
</ruby>
<erb>
<% form_for(@product) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :description %><br />
<%= f.text_field :description %>
</p>
<p>
<%= f.label :value %><br />
<%= f.text_field :value %>
</p>
<p>
<%= f.submit "Create" %>
</p>
<% end %>
</erb>
!images/error_messages.png(Error messages)!
You can also use the +error_messages_for+ helper to display the error messages of a model assigned to a view template. It's very similar to the previous example and will achieve exactly the same result.
<erb>
<%= error_messages_for :product %>
</erb>
The displayed text for each error message will always be formed by the capitalized name of the attribute that holds the error, followed by the error message itself.
Both the +form.error_messages+ and the +error_messages_for+ helpers accept options that let you customize the +div+ element that holds the messages, changing the header text, the message below the header text and the tag used for the element that defines the header.
<erb>
<%= f.error_messages :header_message => "Invalid product!",
:message => "You'll need to fix the following fields:",
:header_tag => :h3 %>
</erb>
Which results in the following content
!images/customized_error_messages.png(Customized error messages)!
If you pass +nil+ to any of these options, it will get rid of the respective section of the +div+.
It's also possible to change the CSS classes used by the +error_messages+ helper. These classes are automatically defined at the *scaffold.css* file, generated by the scaffold script. If you're not using scaffolding, you can still define those CSS classes at your CSS files. Here is a list of the default CSS classes.
* +.fieldWithErrors+ - Style for the form fields with errors.
* +#errorExplanation+ - Style for the +div+ element with the error messages.
* +#errorExplanation h2+ - Style for the header of the +div+ element.
* +#errorExplanation p+ - Style for the paragraph that holds the message that appears right below the header of the +div+ element.
* +#errorExplanation ul li+ - Style for the list of error messages.
h4. Changing the way form fields with errors are displayed
By default, form fields with errors are displayed enclosed by a +div+ element with the +fieldWithErrors+ CSS class. However, we can write some Ruby code to override the way Rails treats those fields by default. Here is a simple example where we change the Rails behaviour to always display the error messages in front of each of the form fields with errors. The error messages will be enclosed by a +span+ element with a +validation-error+ CSS class. There will be no +div+ element enclosing the +input+ element, so we get rid of that red border around the text field. You can use the +validation-error+ CSS class to style it anyway you want.
<ruby>
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
if instance.error_message.kind_of?(Array)
%(#{html_tag}<span class='validation-error'>&nbsp;
#{instance.error_message.join(',')}</span>)
else
%(#{html_tag}<span class='validation-error'>&nbsp;
#{instance.error_message}</span>)
end
end
</ruby>
This will result in something like the following content:
!images/validation_error_messages.png(Validation error messages)!
The way form fields with errors are treated is defined by the +ActionView::Base.field_error_proc+ Ruby Proc. This Proc receives two parameters:
* A string with the HTML tag
* An object of the +ActionView::Helpers::InstanceTag+ class.
h3. Callbacks
Callbacks are methods that get called at certain moments of an object's lifecycle. With callbacks it's possible to write code that will run whenever an Active Record object is created, saved, updated, deleted or loaded from the database.
# TODO discuss what does/doesn't trigger callbacks, like we did in the validations section (e.g. destroy versus delete).
# Consider moving up to the (new) intro overview section, before getting into details.
# http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M002220
# http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html
h4. Callback registration
In order to use the available callbacks, you need to register them. You can do that by implementing them as an ordinary methods, and then using a macro-style class method to register then as callbacks.
<ruby>
class User < ActiveRecord::Base
validates_presence_of :login, :email
before_validation :ensure_login_has_a_value
protected
def ensure_login_has_a_value
if self.login.nil?
self.login = email unless email.blank?
end
end
end
</ruby>
The macro-style class methods can also receive a block. Rails best practices say that you should only use this style of registration if the code inside your block is so short that it fits in just one line.
<ruby>
class User < ActiveRecord::Base
validates_presence_of :login, :email
before_create {|user| user.name = user.login.capitalize if user.name.blank?}
end
</ruby>
CAUTION: Remember to always declare the callback methods as being protected or private. These methods should never be public, otherwise it will be possible to call them from code outside the model, violating object encapsulation and exposing implementation details.
# TODO consider not making this a warning, just a note (this is an implementation detail, not a requirement of the library)
h3. Conditional callbacks
Like in validations, we can also make our callbacks conditional, calling then only when a given predicate is satisfied. You can do that by using the +:if+ and +:unless+ options, which can take a symbol, a string or a Ruby Proc. You may use the +:if+ option when you want to specify when the callback *should* get called. If you want to specify when the callback *should not* be called, then you may use the +:unless+ option.
h4. Using a symbol with the :if and :unless
You can associate the +:if+ and +:unless+ options with a symbol corresponding to the name of a method that will get called right before the callback. If this method returns +false+ the callback won't be executed. This is the most common option. Using this form of registration it's also possible to register several different methods that should be called to check the if the callback should be executed.
<ruby>
class Order < ActiveRecord::Base
before_save :normalize_card_number, :if => :paid_with_card?
end
</ruby>
h4. Using a string with the :if and :unless
You can also use a string that will be evaluated using +:eval+ and needs to contain valid Ruby code. You should use this option only when the string represents a really short condition.
<ruby>
class Order < ActiveRecord::Base
before_save :normalize_card_number, :if => "paid_with_card?"
end
</ruby>
h4. Using a Proc object with the :if and :unless
Finally, it's possible to associate +:if+ and +:unless+ with a Ruby Proc object. This option is best suited when writing short validation methods, usually one-liners.
<ruby>
class Order < ActiveRecord::Base
before_save :normalize_card_number,
:if => Proc.new { |order| order.paid_with_card? }
end
</ruby>
h4. Multiple Conditions for Callbacks
When writing conditional callbacks, it's possible to mix both +:if+ and +:unless+ in the same callback declaration.
<ruby>
class Comment < ActiveRecord::Base
after_create :send_email_to_author, :if => :author_wants_emails?,
:unless => Proc.new { |comment| comment.post.ignore_comments? }
end
</ruby>
h3. Available callbacks
# TODO consider moving above the code examples and details, possibly combining with intro lifecycle stuff?
Here is a list with all the available Active Record callbacks, listed in the same order in which they will get called during the respective operations.
h4. Callbacks called both when creating or updating a record.
# TODO consider just listing these in the possible lifecycle of an object, roughly as they do here:
# http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html
* +before_validation+
* +after_validation+
* +before_save+
* *INSERT OR UPDATE OPERATION*
* +after_save+
h4. Callbacks called only when creating a new record.
* +before_validation_on_create+
* +after_validation_on_create+
* +before_create+
* *INSERT OPERATION*
* +after_create+
h4. Callbacks called only when updating an existing record.
* +before_validation_on_update+
* +after_validation_on_update+
* +before_update+
* *UPDATE OPERATION*
* +after_update+
h4. Callbacks called when removing a record from the database.
* +before_destroy+
* *DELETE OPERATION*
* +after_destroy+
The +before_destroy+ and +after_destroy+ callbacks will only be called if you delete the model using either the +destroy+ instance method or one of the +destroy+ or +destroy_all+ class methods of your Active Record class. If you use +delete+ or +delete_all+ no callback operations will run, since Active Record will not instantiate any objects, accessing the records to be deleted directly in the database.
h4. after_initialize and after_find callbacks
The +after_initialize+ callback will be called whenever an Active Record object is instantiated, either by directly using +new+ or when a record is loaded from the database. It can be useful to avoid the need to directly override your Active Record +initialize+ method.
The +after_find+ callback will be called whenever Active Record loads a record from the database. When used together with +after_initialize+ it will run first, since Active Record will first read the record from the database and them create the model object that will hold it.
The +after_initialize+ and +after_find+ callbacks are a bit different from the others, since the only way to register those callbacks is by defining them as methods. If you try to register +after_initialize+ or +after_find+ using macro-style class methods, they will just be ignored. This behaviour is due to performance reasons, since +after_initialize+ and +after_find+ will both be called for each record found in the database, significantly slowing down the queries.
h3. Halting Execution
As you start registering new callbacks for your models, they will be queued for execution. This queue will include all your model's validations, the registered callbacks and the database operation to be executed. However, if at any moment one of the +before_create+, +before_save+, +before_update+ or +before_destroy+ callback methods returns a boolean +false+ (not +nil+) value or raise and exception, this execution chain will be halted and the desired operation will not complete: your model will not get persisted in the database, or your records will not get deleted and so on. It's because the whole callback chain is wrapped in a transaction, so raising an exception or returning +false+ fires a database ROLLBACK.
h3. Callback classes
Sometimes the callback methods that you'll write will be useful enough to be reused at other models. Active Record makes it possible to create classes that encapsulate the callback methods, so it becomes very easy to reuse them.
Here's an example where we create a class with a after_destroy callback for a PictureFile model.
<ruby>
class PictureFileCallbacks
def after_destroy(picture_file)
File.delete(picture_file.filepath) if File.exists?(picture_file.filepath)
end
end
</ruby>
When declared inside a class the callback method will receive the model object as a parameter. We can now use it this way:
<ruby>
class PictureFile < ActiveRecord::Base
after_destroy PictureFileCallbacks.new
end
</ruby>
Note that we needed to instantiate a new PictureFileCallbacks object, since we declared our callback as an instance method. Sometimes it will make more sense to have it as a class method.
<ruby>
class PictureFileCallbacks
def self.after_destroy(picture_file)
File.delete(picture_file.filepath) if File.exists?(picture_file.filepath)
end
end
</ruby>
If the callback method is declared this way, it won't be necessary to instantiate a PictureFileCallbacks object.
<ruby>
class PictureFile < ActiveRecord::Base
after_destroy PictureFileCallbacks
end
</ruby>
You can declare as many callbacks as you want inside your callback classes.
h3. Observers
Active Record callbacks are a powerful feature, but they can pollute your model implementation with code that's not directly related to the model's purpose. In object-oriented software, it's always a good idea to design your classes with a single responsibility in the whole system. For example, it wouldn't make much sense to have a +User+ model with a method that writes data about a login attempt to a log file. Whenever you're using callbacks to write code that's not directly related to your model class purposes, it may be a good moment to create an Observer.
An Active Record Observer is an object that links itself to a model and registers its methods for callbacks. Your model's implementation remains clean, while you can reuse the code in the Observer to add behaviour to more than one model class. OK, you may say that we can also do that using callback classes, but it would still force us to add code to our model's implementation.
Observer classes are subclasses of the ActiveRecord::Observer class. When this class is subclassed, Active Record will look at the name of the new class and then strip the 'Observer' part to find the name of the Active Record class to observe.
Consider a Registration model, where we want to send an email every time a new registration is created. Since sending emails is not directly related to our model's purpose, we could create an Observer to do just that:
<ruby>
class RegistrationObserver < ActiveRecord::Observer
def after_create(model)
# code to send registration confirmation emails...
end
end
</ruby>
Like in callback classes, the observer's methods receive the observed model as a parameter.
Sometimes using the ModelName + Observer naming convention won't be the best choice, mainly when you want to use the same observer for more than one model class. It's possible to explicity specify the models that our observer should observe.
<ruby>
class Auditor < ActiveRecord::Observer
observe User, Registration, Invoice
end
</ruby>
h4. Registering observers
If you paid attention, you may be wondering where Active Record Observers are referenced in our applications, so they get instantiated and begin to interact with our models. For observers to work we need to register them somewhere. The usual place to do that is in our application's *config/environment.rb* file. In this file there is a commented-out line where we can define the observers that our application should load at start-up.
<ruby>
# Activate observers that should always be running
config.active_record.observers = :registration_observer, :auditor
</ruby>
You can uncomment the line with +config.active_record.observers+ and change the symbols for the name of the observers that should be registered.
It's also possible to register callbacks in any of the files living at *config/environments/*, if you want an observer to work only in a specific environment. There is not a +config.active_record.observers+ line at any of those files, but you can simply add it.
h4. Where to put the observers' source files
By convention, you should always save your observers' source files inside *app/models*.
# TODO this probably doesn't need it's own section and entry in the table of contents.
h3. Changelog
"Lighthouse ticket":http://rails.lighthouseapp.com/projects/16213/tickets/26-active-record-validations-and-callbacks
January 9, 2009: Initial version by "Cássio Marques":credits.html#cmarques

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,429 @@
h2. Caching with Rails: An overview
Everyone caches. This guide will teach you what you need to know about
avoiding that expensive round-trip to your database and returning what you
need to return to those hungry web clients in the shortest time possible.
endprologue.
h3. Basic Caching
This is an introduction to the three types of caching techniques that Rails
provides by default without the use of any third party plugins.
To get started make sure +config.action_controller.perform_caching+ is set
to +true+ for your environment. This flag is normally set in the
corresponding config/environments/*.rb and caching is disabled by default
there for development and test, and enabled for production.
<ruby>
config.action_controller.perform_caching = true
</ruby>
h4. Page Caching
Page caching is a Rails mechanism which allows the request for a generated
page to be fulfilled by the webserver, without ever having to go through the
Rails stack at all. Obviously, this is super-fast. Unfortunately, it can't be
applied to every situation (such as pages that need authentication) and since
the webserver is literally just serving a file from the filesystem, cache
expiration is an issue that needs to be dealt with.
So, how do you enable this super-fast cache behavior? Simple, let's say you
have a controller called ProductsController and a 'list' action that lists all
the products
<ruby>
class ProductsController < ActionController
caches_page :index
def index; end
end
</ruby>
The first time anyone requests products/index, Rails will generate a file
called +index.html+ and the webserver will then look for that file before it
passes the next request for products/index to your Rails application.
By default, the page cache directory is set to Rails.public_path (which is
usually set to +RAILS_ROOT + "/public"+) and this can be configured by
changing the configuration setting +config.action_controller.page_cache_directory+.
Changing the default from /public helps avoid naming conflicts, since you may
want to put other static html in /public, but changing this will require web
server reconfiguration to let the web server know where to serve the cached
files from.
The Page Caching mechanism will automatically add a +.html+ exxtension to
requests for pages that do not have an extension to make it easy for the
webserver to find those pages and this can be configured by changing the
configuration setting +config.action_controller.page_cache_extension+.
In order to expire this page when a new product is added we could extend our
example controler like this:
<ruby>
class ProductsController < ActionController
caches_page :list
def list; end
def create
expire_page :action => :list
end
end
</ruby>
If you want a more complicated expiration scheme, you can use cache sweepers
to expire cached objects when things change. This is covered in the section on Sweepers.
[More: caching paginated results? more examples? Walk-through of page caching?]
h4. Action Caching
One of the issues with Page Caching is that you cannot use it for pages that
require to restrict access somehow. This is where Action Caching comes in.
Action Caching works like Page Caching except for the fact that the incoming
web request does go from the webserver to the Rails stack and Action Pack so
that before filters can be run on it before the cache is served, so that
authentication and other restrictions can be used while still serving the
result of the output from a cached copy.
Clearing the cache works in the exact same way as with Page Caching.
Let's say you only wanted authenticated users to edit or create a Product
object, but still cache those pages:
<ruby>
class ProductsController < ActionController
before_filter :authenticate, :only => [ :edit, :create ]
caches_page :list
caches_action :edit
def list; end
def create
expire_page :action => :list
expire_action :action => :edit
end
def edit; end
end
</ruby>
And you can also use +:if+ (or +:unless+) to pass a Proc that specifies when the
action should be cached. Also, you can use +:layout => false+ to cache without
layout so that dynamic information in the layout such as logged in user info
or the number of items in the cart can be left uncached. This feature is
available as of Rails 2.2.
[More: more examples? Walk-through of Action Caching from request to response?
Description of Rake tasks to clear cached files? Show example of
subdomain caching? Talk about :cache_path, :if and assing blocks/Procs
to expire_action?]
h4. Fragment Caching
Life would be perfect if we could get away with caching the entire contents of
a page or action and serving it out to the world. Unfortunately, dynamic web
applications usually build pages with a variety of components not all of which
have the same caching characteristics. In order to address such a dynamically
created page where different parts of the page need to be cached and expired
differently Rails provides a mechanism called Fragment Caching.
Fragment Caching allows a fragment of view logic to be wrapped in a cache
block and served out of the cache store when the next request comes in.
As an example, if you wanted to show all the orders placed on your website
in real time and didn't want to cache that part of the page, but did want
to cache the part of the page which lists all products available, you
could use this piece of code:
<ruby>
<% Order.find_recent.each do |o| %>
<%= o.buyer.name %> bought <% o.product.name %>
<% end %>
<% cache do %>
All available products:
<% Product.find(:all).each do |p| %>
<%= link_to p.name, product_url(p) %>
<% end %>
<% end %>
</ruby>
The cache block in our example will bind to the action that called it and is
written out to the same place as the Action Cache, which means that if you
want to cache multiple fragments per action, you should provide an +action_suffix+ to the cache call:
<ruby>
<% cache(:action => 'recent', :action_suffix => 'all_products') do %>
All available products:
</ruby>
and you can expire it using the +expire_fragment+ method, like so:
<ruby>
expire_fragment(:controller => 'products', :action => 'recent', :action_suffix => 'all_products)
</ruby>
If you don't want the cache block to bind to the action that called it, You can
also use globally keyed fragments by calling the cache method with a key, like
so:
<ruby>
<% cache(:key => ['all_available_products', @latest_product.created_at].join(':')) do %>
All available products:
<% end %>
</ruby>
This fragment is then available to all actions in the ProductsController using
the key and can be expired the same way:
<ruby>
expire_fragment(:key => ['all_available_products', @latest_product.created_at].join(':'))
</ruby>
[More: more examples? description of fragment keys and expiration, etc? pagination?]
h4. Sweepers
Cache sweeping is a mechanism which allows you to get around having a ton of
expire_{page,action,fragment} calls in your code by moving all the work
required to expire cached content into a +ActionController::Caching::Sweeper+
class that is an Observer and looks for changes to an object via callbacks,
and when a change occurs it expires the caches associated with that object n
an around or after filter.
Continuing with our Product controller example, we could rewrite it with a
sweeper such as the following:
<ruby>
class StoreSweeper < ActionController::Caching::Sweeper
observe Product # This sweeper is going to keep an eye on the Product model
# If our sweeper detects that a Product was created call this
def after_create(product)
expire_cache_for(product)
end
# If our sweeper detects that a Product was updated call this
def after_update(product)
expire_cache_for(product)
end
# If our sweeper detects that a Product was deleted call this
def after_destroy(product)
expire_cache_for(product)
end
private
def expire_cache_for(record)
# Expire the list page now that we added a new product
expire_page(:controller => '#{record}', :action => 'list')
# Expire a fragment
expire_fragment(:controller => '#{record}', :action => 'recent', :action_suffix => 'all_products')
end
end
</ruby>
Then we add it to our controller to tell it to call the sweeper when certain
actions are called. So, if we wanted to expire the cached content for the
list and edit actions when the create action was called, we could do the
following:
<ruby>
class ProductsController < ActionController
before_filter :authenticate, :only => [ :edit, :create ]
caches_page :list
caches_action :edit
cache_sweeper :store_sweeper, :only => [ :create ]
def list; end
def create
expire_page :action => :list
expire_action :action => :edit
end
def edit; end
end
</ruby>
[More: more examples? better sweepers?]
h4. SQL Caching
Query caching is a Rails feature that caches the result set returned by each
query so that if Rails encounters the same query again for that request, it
will used the cached result set as opposed to running the query against the
database again.
For example:
<ruby>
class ProductsController < ActionController
before_filter :authenticate, :only => [ :edit, :create ]
caches_page :list
caches_action :edit
cache_sweeper :store_sweeper, :only => [ :create ]
def list
# Run a find query
Product.find(:all)
...
# Run the same query again
Product.find(:all)
end
def create
expire_page :action => :list
expire_action :action => :edit
end
def edit; end
end
</ruby>
In the 'list' action above, the result set returned by the first
Product.find(:all) will be cached and will be used to avoid querying the
database again the second time that finder is called.
Query caches are created at the start of an action and destroyed at the end of
that action and thus persist only for the duration of the action.
h4. Cache stores
Rails provides different stores for the cached data for action and fragment
caches. Page caches are always stored on disk.
The cache stores provided include:
1) Memory store: Cached data is stored in the memory allocated to the Rails
process, which is fine for WEBrick and for FCGI (if you
don't care that each FCGI process holds its own fragment
store). It's not suitable for CGI as the process is thrown
away at the end of each request. It can potentially also
take up a lot of memory since each process keeps all the
caches in memory.
<ruby>
ActionController::Base.cache_store = :memory_store
</ruby>
2) File store: Cached data is stored on the disk, this is the default store
and the default path for this store is: /tmp/cache. Works
well for all types of environments and allows all processes
running from the same application directory to access the
cached content.
<ruby>
ActionController::Base.cache_store = :file_store, "/path/to/cache/directory"
</ruby>
3) DRb store: Cached data is stored in a separate shared DRb process that all
servers communicate with. This works for all environments and
only keeps one cache around for all processes, but requires
that you run and manage a separate DRb process.
<ruby>
ActionController::Base.cache_store = :drb_store, "druby://localhost:9192"
</ruby>
4) MemCached store: Works like DRbStore, but uses Danga's MemCache instead.
Rails uses the bundled memcached-client gem by default.
<ruby>
ActionController::Base.cache_store = :mem_cache_store, "localhost"
</ruby>
5) Custom store: You can define your own cache store (new in Rails 2.1)
<ruby>
ActionController::Base.cache_store = MyOwnStore.new("parameter")
</ruby>
+Note: config.cache_store can be used in place of
ActionController::Base.cache_store in your Rails::Initializer.run block in
environment.rb+
h3. Conditional GET support
Conditional GETs are a facility of the HTTP spec that provide a way for web
servers to tell browsers that the response to a GET request hasnt changed
since the last request and can be safely pulled from the browser cache.
They work by using the HTTP_IF_NONE_MATCH and HTTP_IF_MODIFIED_SINCE headers to
pass back and forth both a unique content identifier and the timestamp of when
the content was last changed. If the browser makes a request where the content
identifier (etag) or last modified since timestamp matches the servers version
then the server only needs to send back an empty response with a not modified
status.
It is the servers (i.e. our) responsibility to look for a last modified
timestamp and the if-none-match header and determine whether or not to send
back the full response. With conditional-get support in rails this is a pretty
easy task:
<ruby>
class ProductsController < ApplicationController
def show
@product = Product.find(params[:id])
# If the request is stale according to the given timestamp and etag value
# (i.e. it needs to be processed again) then execute this block
if stale?(:last_modified => @product.updated_at.utc, :etag => @product)
respond_to do |wants|
# ... normal response processing
end
end
# If the request is fresh (i.e. it's not modified) then you don't need to do
# anything. The default render checks for this using the parameters
# used in the previous call to stale? and will automatically send a
# :not_modified. So that's it, you're done.
end
</ruby>
If you dont have any special response processing and are using the default
rendering mechanism (i.e. youre not using respond_to or calling render
yourself) then youve got an easy helper in fresh_when:
<ruby>
class ProductsController < ApplicationController
# This will automatically send back a :not_modified if the request is fresh,
# and will render the default template (product.*) if it's stale.
def show
@product = Product.find(params[:id])
fresh_when :last_modified => @product.published_at.utc, :etag => @article
end
end
</ruby>
h3. Advanced Caching
Along with the built-in mechanisms outlined above, a number of excellent
plugins exist to help with finer grained control over caching. These include
Chris Wanstrath's excellent cache_fu plugin (more info here:
http://errtheblog.com/posts/57-kickin-ass-w-cachefu) and Evan Weaver's
interlock plugin (more info here:
http://blog.evanweaver.com/articles/2007/12/13/better-rails-caching/). Both
of these plugins play nice with memcached and are a must-see for anyone
seriously considering optimizing their caching needs.

View File

@ -0,0 +1,511 @@
h2. A Guide to The Rails Command Line
Rails comes with every command line tool you'll need to
* Create a Rails application
* Generate models, controllers, database migrations, and unit tests
* Start a development server
* Mess with objects through an interactive shell
* Profile and benchmark your new creation
NOTE: This tutorial assumes you have basic Rails knowledge from reading the "Getting Started with Rails Guide":getting_started.html.
endprologue.
h3. Command Line Basics
There are a few commands that are absolutely critical to your everyday usage of Rails. In the order of how much you'll probably use them are:
* console
* server
* rake
* generate
* rails
Let's create a simple Rails application to step through each of these commands in context.
h4. rails
The first thing we'll want to do is create a new Rails application by running the +rails+ command after installing Rails.
WARNING: You know you need the rails gem installed by typing +gem install rails+ first, right? Okay, okay, just making sure.
<shell>
$ rails commandsapp
create
create app/controllers
create app/helpers
create app/models
...
...
create log/production.log
create log/development.log
create log/test.log
</shell>
Rails will set you up with what seems like a huge amount of stuff for such a tiny command! You've got the entire Rails directory structure now with all the code you need to run our simple application right out of the box.
INFO: This output will seem very familiar when we get to the +generate+ command. Creepy foreshadowing!
h4. server
Let's try it! The +server+ command launches a small web server named WEBrick which comes bundled with Ruby. You'll use this any time you want to view your work through a web browser.
INFO: WEBrick isn't your only option for serving Rails. We'll get to that in a later section.
Without any prodding of any kind, +server+ will run our new shiny Rails app:
<shell>
$ cd commandsapp
$ ./script/server
=> Booting WEBrick...
=> Rails 2.2.0 application started on http://0.0.0.0:3000
=> Ctrl-C to shutdown server; call with --help for options
[2008-11-04 10:11:38] INFO WEBrick 1.3.1
[2008-11-04 10:11:38] INFO ruby 1.8.5 (2006-12-04) [i486-linux]
[2008-11-04 10:11:38] INFO WEBrick::HTTPServer#start: pid=18994 port=3000
</shell>
WHOA. With just three commands we whipped up a Rails server listening on port 3000. Go! Go right now to your browser and go to http://localhost:3000. I'll wait.
See? Cool! It doesn't do much yet, but we'll change that.
h4. generate
The +generate+ command uses templates to create a whole lot of things. You can always find out what's available by running +generate+ by itself. Let's do that:
<shell>
$ ./script/generate
Usage: ./script/generate generator [options] [args]
...
...
Installed Generators
Builtin: controller, integration_test, mailer, migration, model, observer, performance_test, plugin, resource, scaffold, session_migration
...
...
</shell>
NOTE: You can install more generators through generator gems, portions of plugins you'll undoubtedly install, and you can even create your own!
Using generators will save you a large amount of time by writing *boilerplate code* for you -- necessary for the darn thing to work, but not necessary for you to spend time writing. That's what we have computers for, right?
Let's make our own controller with the controller generator. But what command should we use? Let's ask the generator:
INFO: All Rails console utilities have help text. As with most *NIX utilities, you can try adding +--help+ or +-h+ to the end, for example +./script/server --help+.
<shell>
$ ./script/generate controller
Usage: ./script/generate controller ControllerName [options]
...
...
Example:
./script/generate controller CreditCard open debit credit close
Credit card controller with URLs like /credit_card/debit.
Controller: app/controllers/credit_card_controller.rb
Views: app/views/credit_card/debit.html.erb [...]
Helper: app/helpers/credit_card_helper.rb
Test: test/functional/credit_card_controller_test.rb
Modules Example:
./script/generate controller 'admin/credit_card' suspend late_fee
Credit card admin controller with URLs /admin/credit_card/suspend.
Controller: app/controllers/admin/credit_card_controller.rb
Views: app/views/admin/credit_card/debit.html.erb [...]
Helper: app/helpers/admin/credit_card_helper.rb
Test: test/functional/admin/credit_card_controller_test.rb
</shell>
Ah, the controller generator is expecting parameters in the form of +generate controller ControllerName action1 action2+. Let's make a +Greetings+ controller with an action of *hello*, which will say something nice to us.
<shell>
$ ./script/generate controller Greeting hello
exists app/controllers/
exists app/helpers/
create app/views/greeting
exists test/functional/
create app/controllers/greetings_controller.rb
create test/functional/greetings_controller_test.rb
create app/helpers/greetings_helper.rb
create app/views/greetings/hello.html.erb
</shell>
Look there! Now what all did this generate? It looks like it made sure a bunch of directories were in our application, and created a controller file, a functional test file, a helper for the view, and a view file.
Let's check out the controller and modify it a little (in +app/controllers/greeting_controller.rb+):
<ruby>
class GreetingController < ApplicationController
def hello
@message = "Hello, how are you today? I am exuberant!"
end
end
</ruby>
Then the view, to display our nice message (in +app/views/greeting/hello.html.erb+):
<html>
<h1>A Greeting for You!</h1>
<p><%= @message %></p>
</html>
Deal. Go check it out in your browser. Fire up your server. Remember? +./script/server+ at the root of your Rails application should do it.
<shell>
$ ./script/server
=> Booting WEBrick...
</shell>
The URL will be +http://localhost:3000/greetings/hello+. I'll wait for you to be suitably impressed.
INFO: With a normal, plain-old Rails application, your URLs will generally follow the pattern of http://(host)/(controller)/(action), and a URL like http://(host)/(controller) will hit the *index* action of that controller.
"What about data, though?", you ask over a cup of coffee. Rails comes with a generator for data models too. Can you guess its generator name?
<shell>
$ ./script/generate model
Usage: ./script/generate model ModelName [field:type, field:type]
...
Examples:
./script/generate model account
creates an Account model, test, fixture, and migration:
Model: app/models/account.rb
Test: test/unit/account_test.rb
Fixtures: test/fixtures/accounts.yml
Migration: db/migrate/XXX_add_accounts.rb
./script/generate model post title:string body:text published:boolean
creates a Post model with a string title, text body, and published flag.
</shell>
But instead of generating a model directly (which we'll be doing later), let's set up a scaffold. A *scaffold* in Rails is a full set of model, database migration for that model, controller to manipulate it, views to view and manipulate the data, and a test suite for each of the above.
Let's set up a simple resource called "HighScore" that will keep track of our highest score on video games we play.
<shell>
$ ./script/generate scaffold HighScore game:string score:integer
exists app/models/
exists app/controllers/
exists app/helpers/
create app/views/high_scores
create app/views/layouts/
exists test/functional/
create test/unit/
create public/stylesheets/
create app/views/high_scores/index.html.erb
create app/views/high_scores/show.html.erb
create app/views/high_scores/new.html.erb
create app/views/high_scores/edit.html.erb
create app/views/layouts/high_scores.html.erb
create public/stylesheets/scaffold.css
create app/controllers/high_scores_controller.rb
create test/functional/high_scores_controller_test.rb
create app/helpers/high_scores_helper.rb
route map.resources :high_scores
dependency model
exists app/models/
exists test/unit/
create test/fixtures/
create app/models/high_score.rb
create test/unit/high_score_test.rb
create test/fixtures/high_scores.yml
exists db/migrate
create db/migrate/20081217071914_create_high_scores.rb
</shell>
Taking it from the top - the generator checks that there exist the directories for models, controllers, helpers, layouts, functional and unit tests, stylesheets, creates the views, controller, model and database migration for HighScore (creating the +high_scores+ table and fields), takes care of the route for the *resource*, and new tests for everything.
The migration requires that we *migrate*, that is, run some Ruby code (living in that +20081217071914_create_high_scores.rb+) to modify the schema of our database. Which database? The sqlite3 database that Rails will create for you when we run the +rake db:migrate+ command. We'll talk more about Rake in-depth in a little while.
CAUTION: Hey. Install the sqlite3-ruby gem while you're at it. +gem install sqlite3-ruby+
<shell>
$ rake db:migrate
(in /home/commandsapp)
CreateHighScores: migrating
create_table(:high_scores)
-> 0.0070s
CreateHighScores: migrated (0.0077s)
</shell>
INFO: Let's talk about unit tests. Unit tests are code that tests and makes assertions about code. In unit testing, we take a little part of code, say a method of a model, and test its inputs and outputs. Unit tests are your friend. The sooner you make peace with the fact that your quality of life will drastically increase when you unit test your code, the better. Seriously. We'll make one in a moment.
Let's see the interface Rails created for us. ./script/server; http://localhost:3000/high_scores
We can create new high scores (55,160 on Space Invaders!)
h4. console
The +console+ command lets you interact with your Rails application from the command line. On the underside, +script/console+ uses IRB, so if you've ever used it, you'll be right at home. This is useful for testing out quick ideas with code and changing data server-side without touching the website.
h4. dbconsole
+dbconsole+ figures out which database you're using and drops you into whichever command line interface you would use with it (and figures out the command line parameters to give to it, too!). It supports MySQL, PostgreSQL, SQLite and SQLite3.
h4. plugin
The +plugin+ command simplifies plugin management; think a miniature version of the Gem utility. Let's walk through installing a plugin. You can call the sub-command *discover*, which sifts through repositories looking for plugins, or call *source* to add a specific repository of plugins, or you can specify the plugin location directly.
Let's say you're creating a website for a client who wants a small accounting system. Every event having to do with money must be logged, and must never be deleted. Wouldn't it be great if we could override the behavior of a model to never actually take its record out of the database, but *instead*, just set a field?
There is such a thing! The plugin we're installing is called "acts_as_paranoid", and it lets models implement a "deleted_at" column that gets set when you call destroy. Later, when calling find, the plugin will tack on a database check to filter out "deleted" things.
<shell>
$ ./script/plugin install http://svn.techno-weenie.net/projects/plugins/acts_as_paranoid
+ ./CHANGELOG
+ ./MIT-LICENSE
...
...
</shell>
h4. runner
<tt>runner</tt> runs Ruby code in the context of Rails non-interactively. For instance:
<shell>
$ ./script/runner "Model.long_running_method"
</shell>
h4. destroy
Think of +destroy+ as the opposite of +generate+. It'll figure out what generate did, and undo it. Believe you-me, the creation of this tutorial used this command many times!
<shell>
$ ./script/generate model Oops
exists app/models/
exists test/unit/
exists test/fixtures/
create app/models/oops.rb
create test/unit/oops_test.rb
create test/fixtures/oops.yml
exists db/migrate
create db/migrate/20081221040817_create_oops.rb
$ ./script/destroy model Oops
notempty db/migrate
notempty db
rm db/migrate/20081221040817_create_oops.rb
rm test/fixtures/oops.yml
rm test/unit/oops_test.rb
rm app/models/oops.rb
notempty test/fixtures
notempty test
notempty test/unit
notempty test
notempty app/models
notempty app
</shell>
h4. about
Check it: Version numbers for Ruby, RubyGems, Rails, the Rails subcomponents, your application's folder, the current Rails environment name, your app's database adapter, and schema version! +about+ is useful when you need to ask for help, check if a security patch might affect you, or when you need some stats for an existing Rails installation.
<shell>
$ ./script/about
About your application's environment
Ruby version 1.8.6 (i486-linux)
RubyGems version 1.3.1
Rails version 2.2.0
Active Record version 2.2.0
Action Pack version 2.2.0
Active Resource version 2.2.0
Action Mailer version 2.2.0
Active Support version 2.2.0
Edge Rails revision unknown
Application root /home/commandsapp
Environment development
Database adapter sqlite3
Database schema version 20081217073400
</shell>
h3. The Rails Advanced Command Line
The more advanced uses of the command line are focused around finding useful (even surprising at times) options in the utilities, and fitting utilities to your needs and specific work flow. Listed here are some tricks up Rails' sleeve.
h4. Rails with databases and SCM
When creating a new Rails application, you have the option to specify what kind of database and what kind of source code management system your application is going to use. This will save you a few minutes, and certainly many keystrokes.
Let's see what a +--git+ option and a +--database=postgresql+ option will do for us:
<shell>
$ mkdir gitapp
$ cd gitapp
$ git init
Initialized empty Git repository in .git/
$ rails . --git --database=postgresql
exists
create app/controllers
create app/helpers
...
...
create tmp/cache
create tmp/pids
create Rakefile
add 'Rakefile'
create README
add 'README'
create app/controllers/application.rb
add 'app/controllers/application.rb'
create app/helpers/application_helper.rb
...
create log/test.log
add 'log/test.log'
</shell>
We had to create the *gitapp* directory and initialize an empty git repository before Rails would add files it created to our repository. Let's see what it put in our database configuration:
<shell>
$ cat config/database.yml
# PostgreSQL. Versions 7.4 and 8.x are supported.
#
# Install the ruby-postgres driver:
# gem install ruby-postgres
# On Mac OS X:
# gem install ruby-postgres -- --include=/usr/local/pgsql
# On Windows:
# gem install ruby-postgres
# Choose the win32 build.
# Install PostgreSQL and put its /bin directory on your path.
development:
adapter: postgresql
encoding: unicode
database: gitapp_development
pool: 5
username: gitapp
password:
...
...
</shell>
It also generated some lines in our database.yml configuration corresponding to our choice of PostgreSQL for database. The only catch with using the SCM options is that you have to make your application's directory first, then initialize your SCM, then you can run the +rails+ command to generate the basis of your app.
h4. server with different backends
Many people have created a large number different web servers in Ruby, and many of them can be used to run Rails. Since version 2.3, Rails uses Rack to serve its webpages, which means that any webserver that implements a Rack handler can be used. This includes WEBrick, Mongrel, Thin, and Phusion Passenger (to name a few!).
NOTE: For more details on the Rack integration, see "Rails on Rack":rails_on_rack.html.
To use a different server, just install its gem, then use its name for the first parameter to +script/server+:
<shell>
$ sudo gem install mongrel
Building native extensions. This could take a while...
Building native extensions. This could take a while...
Successfully installed gem_plugin-0.2.3
Successfully installed fastthread-1.0.1
Successfully installed cgi_multipart_eof_fix-2.5.0
Successfully installed mongrel-1.1.5
...
...
Installing RDoc documentation for mongrel-1.1.5...
$ script/server mongrel
=> Booting Mongrel (use 'script/server webrick' to force WEBrick)
=> Rails 2.2.0 application starting on http://0.0.0.0:3000
...
</shell>
h4. The Rails Generation: Generators
INFO: For a good rundown on generators, see "Understanding Generators":http://wiki.rubyonrails.org/rails/pages/UnderstandingGenerators. A lot of its material is presented here.
Generators are code that generates code. Let's experiment by building one. Our generator will generate a text file.
The Rails generator by default looks in these places for available generators, where RAILS_ROOT is the root of your Rails application, like /home/foobar/commandsapp:
* RAILS_ROOT/lib/generators
* RAILS_ROOT/vendor/generators
* Inside any plugin with a directory like "generators" or "rails_generators"
* ~/.rails/generators
* Inside any Gem you have installed with a name ending in "_generator"
* Inside *any* Gem installed with a "rails_generators" path, and a file ending in "_generator.rb"
* Finally, the builtin Rails generators (controller, model, mailer, etc.)
Let's try the fourth option (in our home directory), which will be easy to clean up later:
<shell>
$ mkdir -p ~/.rails/generators/tutorial_test/templates
$ touch ~/.rails/generators/tutorial_test/templates/tutorial.erb
$ touch ~/.rails/generators/tutorial_test/tutorial_test_generator.rb
</shell>
We'll fill +tutorial_test_generator.rb+ out with:
<ruby>
class TutorialTestGenerator < Rails::Generator::Base
def initialize(*runtime_args)
super(*runtime_args)
@tut_args = runtime_args
end
def manifest
record do |m|
m.directory "public"
m.template "tutorial.erb", File.join("public", "tutorial.txt"),
:assigns => { :args => @tut_args }
end
end
end
</ruby>
We take whatever args are supplied, save them to an instance variable, and literally copying from the Rails source, implement a +manifest+ method, which calls +record+ with a block, and we:
* Check there's a *public* directory. You bet there is.
* Run the ERb template called "tutorial.erb".
* Save it into "RAILS_ROOT/public/tutorial.txt".
* Pass in the args we saved through the +:assign+ parameter.
Next we'll build the template:
<shell>
$ cat ~/.rails/generators/tutorial_test/templates/tutorial.erb
I'm a template!
I got assigned some args:
<%= require 'pp'; PP.pp(args, "") %>
</shell>
Then we'll make sure it got included in the list of available generators:
<shell>
$ ./script/generate
...
...
Installed Generators
User: tutorial_test
</shell>
SWEET! Now let's generate some text, yeah!
<shell>
$ ./script/generate tutorial_test arg1 arg2 arg3
exists public
create public/tutorial.txt
</shell>
And the result:
<shell>
$ cat public/tutorial.txt
I'm a template!
I got assigned some args:
[["arg1", "arg2", "arg3"],
{:collision=>:ask,
:quiet=>false,
:generator=>"tutorial_test",
:command=>:create}]
</shell>
Tada!

View File

@ -0,0 +1,234 @@
h2. Configuring Rails Applications
This guide covers the configuration and initialization features available to Rails applications. By referring to this guide, you will be able to:
* Adjust the behavior of your Rails applications
* Add additional code to be run at application start time
endprologue.
NOTE: The first edition of this Guide was written from the Rails 2.3 source code. While the information it contains is broadly applicable to Rails 2.2, backwards compatibility is not guaranteed.
h3. Locations for Initialization Code
Rails offers (at least) five good spots to place initialization code:
* Preinitializers
* environment.rb
* Environment-specific Configuration Files
* Initializers (load_application_initializers)
* After-Initializers
h3. Using a Preinitializer
Rails allows you to use a preinitializer to run code before the framework itself is loaded. If you save code in +RAILS_ROOT/config/preinitializer.rb+, that code will be the first thing loaded, before any of the framework components (Active Record, Action Pack, and so on.) If you want to change the behavior of one of the classes that is used in the initialization process, you can do so in this file.
h3. Configuring Rails Components
In general, the work of configuring Rails means configuring the components of Rails, as well as configuring Rails itself. The +environment.rb+ and environment-specific configuration files (such as +config/environments/production.rb+) allow you to specify the various settings that you want to pass down to all of the components. For example, the default Rails 2.3 +environment.rb+ file includes one setting:
<ruby>
config.time_zone = 'UTC'
</ruby>
This is a setting for Rails itself. If you want to pass settings to individual Rails components, you can do so via the same +config+ object:
<ruby>
config.active_record.colorize_logging = false
</ruby>
Rails will use that particular setting to configure Active Record.
h4. Configuring Active Record
<tt>ActiveRecord::Base</tt> includes a variety of configuration options:
* +logger+ accepts a logger conforming to the interface of Log4r or the default Ruby 1.8.x Logger class, which is then passed on to any new database connections made. You can retrieve this logger by calling +logger+ on either an ActiveRecord model class or an ActiveRecord model instance. Set to nil to disable logging.
* +primary_key_prefix_type+ lets you adjust the naming for primary key columns. By default, Rails assumes that primary key columns are named +id+ (and this configuration option doesn't need to be set.) There are two other choices:
** +:table_name+ would make the primary key for the Customer class +customerid+
** +:table_name_with_underscore+ would make the primary key for the Customer class +customer_id+
* +table_name_prefix+ lets you set a global string to be prepended to table names. If you set this to +northwest_+, then the Customer class will look for +northwest_customers+ as its table. The default is an empty string.
* +table_name_suffix+ lets you set a global string to be appended to table names. If you set this to +_northwest+, then the Customer class will look for +customers_northwest+ as its table. The default is an empty string.
* +pluralize_table_names+ specifies whether Rails will look for singular or plural table names in the database. If set to +true+ (the default), then the Customer class will use the +customers+ table. If set to +false+, then the Customers class will use the +customer+ table.
* +colorize_logging+ (true by default) specifies whether or not to use ANSI color codes when logging information from ActiveRecord.
* +default_timezone+ determines whether to use +Time.local+ (if set to +:local+) or +Time.utc+ (if set to +:utc+) when pulling dates and times from the database. The default is +:local+.
* +schema_format+ controls the format for dumping the database schema to a file. The options are +:ruby+ (the default) for a database-independent version that depends on migrations, or +:sql+ for a set of (potentially database-dependent) SQL statements.
* +timestamped_migrations+ controls whether migrations are numbered with serial integers or with timestamps. The default is +true+, to use timestamps, which are preferred if there are multiple developers working on the same application.
* +lock_optimistically+ controls whether ActiveRecord will use optimistic locking. By default this is +true+.
The MySQL adapter adds one additional configuration option:
* +ActiveRecord::ConnectionAdapters::MysqlAdapter.emulate_booleans+ controls whether ActiveRecord will consider all +tinyint(1)+ columns in a MySQL database to be booleans. By default this is +true+.
The schema dumper adds one additional configuration option:
* +ActiveRecord::SchemaDumper.ignore_tables+ accepts an array of tables that should _not_ be included in any generated schema file. This setting is ignored unless +ActiveRecord::Base.schema_format == :ruby+.
h4. Configuring Action Controller
<tt>ActionController::Base</tt> includes a number of configuration settings:
* +asset_host+ provides a string that is prepended to all of the URL-generating helpers in +AssetHelper+. This is designed to allow moving all javascript, CSS, and image files to a separate asset host.
* +consider_all_requests_local+ is generally set to +true+ during development and +false+ during production; if it is set to +true+, then any error will cause detailed debugging information to be dumped in the HTTP response. For finer-grained control, set this to +false+ and implement +local_request?+ to specify which requests should provide debugging information on errors.
* +allow_concurrency+ should be set to +true+ to allow concurrent (threadsafe) action processing. Set to +false+ by default.
* +param_parsers+ provides an array of handlers that can extract information from incoming HTTP requests and add it to the +params+ hash. By default, parsers for multipart forms, URL-encoded forms, XML, and JSON are active.
* +default_charset+ specifies the default character set for all renders. The default is "utf-8".
* +logger+ accepts a logger conforming to the interface of Log4r or the default Ruby 1.8+ Logger class, which is then used to log information from Action Controller. Set to nil to disable logging.
* +resource_action_separator+ gives the token to be used between resources and actions when building or interpreting RESTful URLs. By default, this is "/".
* +resource_path_names+ is a hash of default names for several RESTful actions. By default, the new action is named +new+ and the edit action is named +edit+.
* +request_forgery_protection_token+ sets the token parameter name for RequestForgery. Calling +protect_from_forgery+ sets it to +:authenticity_token+ by default.
* +optimise_named_routes+ turns on some optimizations in generating the routing table. It is set to +true+ by default.
* +use_accept_header+ sets the rules for determining the response format. If this is set to +true+ (the default) then +respond_to+ and +Request#format+ will take the Accept header into account. If it is set to false then the request format will be determined solely by examining +params[:format]+. If there is no +format+ parameter, then the response format will be either HTML or Javascript depending on whether the request is an AJAX request.
* +allow_forgery_protection+ enables or disables CSRF protection. By default this is +false+ in test mode and +true+ in all other modes.
* +relative_url_root+ can be used to tell Rails that you are deploying to a subdirectory. The default is +ENV['RAILS_RELATIVE_URL_ROOT']+.
The caching code adds two additional settings:
* +ActionController::Caching::Pages.page_cache_directory+ sets the directory where Rails will create cached pages for your web server. The default is +Rails.public_path+ (which is usually set to +RAILS_ROOT + "/public"+).
* +ActionController::Caching::Pages.page_cache_extension+ sets the extension to be used when generating pages for the cache (this is ignored if the incoming request already has an extension). The default is +.html+.
The dispatcher includes one setting:
* +ActionController::Dispatcher.error_file_path+ gives the path where Rails will look for error files such as +404.html+. The default is +Rails.public_path+.
The Active Record session store can also be configured:
* +CGI::Session::ActiveRecordStore::Session.data_column_name+ sets the name of the column to use to store session data. By default it is 'data'
h4. Configuring Action View
There are only a few configuration options for Action View, starting with four on +ActionView::Base+:
* +debug_rjs+ specifies whether RJS responses should be wrapped in a try/catch block that alert()s the caught exception (and then re-raises it). The default is +false+.
* +warn_cache_misses+ tells Rails to display a warning whenever an action results in a cache miss on your view paths. The default is +false+.
* +field_error_proc+ provides an HTML generator for displaying errors that come from Active Record. The default is +Proc.new{ |html_tag, instance| "<div class=\"fieldWithErrors\">#{html_tag}</div>" }+
* +default_form_builder+ tells Rails which form builder to use by default. The default is +ActionView::Helpers::FormBuilder+.
The ERB template handler supplies one additional option:
* +ActionView::TemplateHandlers::ERB.erb_trim_mode+ gives the trim mode to be used by ERB. It defaults to +'-'+. See the "ERB documentation":http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/ for more information.
h4. Configuring Action Mailer
There are a number of settings available on +ActionMailer::Base+:
* +template_root+ gives the root folder for Action Mailer templates.
* +logger+ accepts a logger conforming to the interface of Log4r or the default Ruby 1.8+ Logger class, which is then used to log information from Action Mailer. Set to nil to disable logging.
* +smtp_settings+ allows detailed configuration for the +:smtp+ delivery method. It accepts a hash of options, which can include any of these options:
** +:address+ - Allows you to use a remote mail server. Just change it from its default "localhost" setting.
** +:port+ - On the off chance that your mail server doesn't run on port 25, you can change it.
** +:domain+ - If you need to specify a HELO domain, you can do it here.
** +:user_name+ - If your mail server requires authentication, set the username in this setting.
** +:password+ - If your mail server requires authentication, set the password in this setting.
** +:authentication+ - If your mail server requires authentication, you need to specify the authentication type here. This is a symbol and one of +:plain+, +:login+, +:cram_md5+.
* +sendmail_settings+ allows detailed configuration for the +sendmail+ delivery method. It accepts a hash of options, which can include any of these options:
** +:location+ - The location of the sendmail executable. Defaults to +/usr/sbin/sendmail+.
** +:arguments+ - The command line arguments. Defaults to +-i -t+.
* +raise_delivery_errors+ specifies whether to raise an error if email delivery cannot be completed. It defaults to +true+.
* +delivery_method+ defines the delivery method. The allowed values are +:smtp+ (default), +:sendmail+, and +:test+.
* +perform_deliveries+ specifies whether mail will actually be delivered. By default this is +true+; it can be convenient to set it to +false+ for testing.
* +default_charset+ tells Action Mailer which character set to use for the body and for encoding the subject. It defaults to +utf-8+.
* +default_content_type+ specifies the default content type used for the main part of the message. It defaults to "text/plain"
* +default_mime_version+ is the default MIME version for the message. It defaults to +1.0+.
* +default_implicit_parts_order+ - When a message is built implicitly (i.e. multiple parts are assembled from templates
which specify the content type in their filenames) this variable controls how the parts are ordered. Defaults to
* +["text/html", "text/enriched", "text/plain"]+. Items that appear first in the array have higher priority in the mail client
and appear last in the mime encoded message.
h4. Configuring Active Resource
There is a single configuration setting available on +ActiveResource::Base+:
<tt>logger</tt> accepts a logger conforming to the interface of Log4r or the default Ruby 1.8+ Logger class, which is then used to log information from Active Resource. Set to nil to disable logging.
h4. Configuring Active Support
There are a few configuration options available in Active Support:
* +ActiveSupport::BufferedLogger.silencer+ is set to +false+ to disable the ability to silence logging in a block. The default is +true+.
* +ActiveSupport::Cache::Store.logger+ specifies the logger to use within cache store operations.
* +ActiveSupport::Logger.silencer+ is set to +false+ to disable the ability to silence logging in a block. The default is +true+.
h4. Configuring Active Model
Active Model currently has a single configuration setting:
* +ActiveModel::Errors.default_error_messages+ is an array containing all of the validation error messages.
h3. Using Initializers
After it loads the framework plus any gems and plugins in your application, Rails turns to loading initializers. An initializer is any file of ruby code stored under +/config/initializers+ in your application. You can use initializers to hold configuration settings that should be made after all of the frameworks and plugins are loaded.
NOTE: You can use subfolders to organize your initializers if you like, because Rails will look into the whole file hierarchy from the +initializers+ folder on down.
TIP: If you have any ordering dependency in your initializers, you can control the load order by naming. For example, +01_critical.rb+ will be loaded before +02_normal.rb+.
h3. Using an After-Initializer
After-initializers are run (as you might guess) after any initializers are loaded. You can supply an +after_initialize+ block (or an array of such blocks) by setting up +config.after_initialize+ in any of the Rails configuration files:
<ruby>
config.after_initialize do
SomeClass.init
end
</ruby>
WARNING: Some parts of your application, notably observers and routing, are not yet set up at the point where the +after_initialize+ block is called.
h3. Rails Environment Settings
Some parts of Rails can also be configured externally by supplying environment variables. The following environment variables are recognized by various parts of Rails:
* +ENV['RAILS_ENV']+ defines the Rails environment (production, development, test, and so on) that Rails will run under.
* +ENV['RAILS_RELATIVE_URL_ROOT']+ is used by the routing code to recognize URLs when you deploy your application to a subdirectory.
* +ENV["RAILS_ASSET_ID"]+ will override the default cache-busting timestamps that Rails generates for downloadable assets.
* +ENV["RAILS_CACHE_ID"]+ and +ENV["RAILS_APP_VERSION"]+ are used to generate expanded cache keys in Rails' caching code. This allows you to have multiple separate caches from the same application.
* +ENV['RAILS_GEM_VERSION']+ defines the version of the Rails gems to use, if +RAILS_GEM_VERSION+ is not defined in your +environment.rb+ file.
h3. Changelog
"Lighthouse ticket":http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/28
* January 3, 2009: First reasonably complete draft by "Mike Gunderloy":credits.html#mgunderloy
* November 5, 2008: Rough outline by "Mike Gunderloy":credits.html#mgunderloy

View File

@ -0,0 +1,38 @@
<% content_for :header_section do %>
h2. Credits
p. We'd like to thank the following people for their tireless contributions to this project.
<% end %>
<% author('Frederick Cheung', 'fcheung') do %>
Frederick Cheung is Chief Wizard at Texperts where he has been using Rails since 2006. He is based in Cambridge (UK) and when not consuming fine ales he blogs at "spacevatican.org":http://www.spacevatican.org.
<% end %>
<% author('Mike Gunderloy', 'mgunderloy') do %>
Mike Gunderloy is an independent consultant who brings 25 years of experience in a variety of languages to bear on his current work with Rails. His near-daily links and other blogging can be found at "A Fresh Cup":http://afreshcup.com.
<% end %>
<% author('Emilio Tagua', 'miloops') do %>
Emilio Tagua -- a.k.a. miloops -- is an Argentinian entrepreneur, developer, open source contributor and Rails evangelist. Cofounder of "Eventioz":http://www.eventioz.com. He has been using Rails since 2006 and contributing since early 2008. Can be found at gmail, twitter, freenode, everywhere as miloops.
<% end %>
<% author('Heiko Webers', 'hawe') do %>
Heiko Webers is the founder of "bauland42":http://www.bauland42.de, a German web application security consulting and development company focused on Ruby on Rails. He blogs at http://www.rorsecurity.info. After 10 years of desktop application development, Heiko has rarely looked back.
<% end %>
<% author('Tore Darell', 'toretore') do %>
Tore Darell is an independent developer based in Menton, France who specialises in cruft-free web applications using Ruby, Rails and unobtrusive JavaScript. His home on the internet is his blog "Sneaky Abstractions":http://tore.darell.no.
<% end %>
<% author('Jeff Dean', 'zilkey') do %>
Jeff Dean is a software engineer with "Pivotal Labs":http://pivotallabs.com.
<% end %>
<% author('Cássio Marques', 'cmarques') do %>
Cássio Marques is a Brazilian software developer working with different programming languages such as Ruby, JavaScript, CPP and Java, as an independent consultant. He blogs at http://cassiomarques.wordpress.com, which is mainly written in portuguese, but will soon get a new section for posts with english translation.
<% end %>
<% author('Pratik Naik', 'lifo') do %>
Pratik Naik is an independent Ruby on Rails consultant and also a member of the "Rails core team":http://rubyonrails.com/core. He blogs semi-regularly at "has_many :bugs, :through => :rails":http://m.onkey.org and has an active "twitter account":http://twitter.com/lifo.
<% end %>

View File

@ -0,0 +1,709 @@
h2. Debugging Rails Applications
This guide introduces techniques for debugging Ruby on Rails applications. By referring to this guide, you will be able to:
* Understand the purpose of debugging
* Track down problems and issues in your application that your tests aren't identifying
* Learn the different ways of debugging
* Analyze the stack trace
endprologue.
h3. View Helpers for Debugging
One common task is to inspect the contents of a variable. In Rails, you can do this with three methods:
* +debug+
* +to_yaml+
* +inspect+
h4. debug
The +debug+ helper will return a &lt;pre&gt;-tag that renders the object using the YAML format. This will generate human-readable data from any object. For example, if you have this code in a view:
<html>
<%= debug @post %>
<p>
<b>Title:</b>
<%=h @post.title %>
</p>
</html>
You'll see something like this:
<yaml>
--- !ruby/object:Post
attributes:
updated_at: 2008-09-05 22:55:47
body: It's a very helpful guide for debugging your Rails app.
title: Rails debugging guide
published: t
id: "1"
created_at: 2008-09-05 22:55:47
attributes_cache: {}
Title: Rails debugging guide
</yaml>
h4. to_yaml
Displaying an instance variable, or any other object or method, in yaml format can be achieved this way:
<html>
<%= simple_format @post.to_yaml %>
<p>
<b>Title:</b>
<%=h @post.title %>
</p>
</html>
The +to_yaml+ method converts the method to YAML format leaving it more readable, and then the +simple_format+ helper is used to render each line as in the console. This is how +debug+ method does its magic.
As a result of this, you will have something like this in your view:
<yaml>
--- !ruby/object:Post
attributes:
updated_at: 2008-09-05 22:55:47
body: It's a very helpful guide for debugging your Rails app.
title: Rails debugging guide
published: t
id: "1"
created_at: 2008-09-05 22:55:47
attributes_cache: {}
Title: Rails debugging guide
</yaml>
h4. inspect
Another useful method for displaying object values is +inspect+, especially when working with arrays or hashes. This will print the object value as a string. For example:
<html>
<%= [1, 2, 3, 4, 5].inspect %>
<p>
<b>Title:</b>
<%=h @post.title %>
</p>
</html>
Will be rendered as follows:
<pre>
[1, 2, 3, 4, 5]
Title: Rails debugging guide
</pre>
h4. Debugging Javascript
Rails has built-in support to debug RJS, to active it, set +ActionView::Base.debug_rjs+ to _true_, this will specify whether RJS responses should be wrapped in a try/catch block that alert()s the caught exception (and then re-raises it).
To enable it, add the following in the +Rails::Initializer do |config|+ block inside +environment.rb+:
<ruby>
config.action_view[:debug_rjs] = true
</ruby>
Or, at any time, setting +ActionView::Base.debug_rjs+ to _true_:
<ruby>
ActionView::Base.debug_rjs = true
</ruby>
TIP: For more information on debugging javascript refer to "Firebug":http://getfirebug.com/, the popular debugger for Firefox.
h3. The Logger
It can also be useful to save information to log files at runtime. Rails maintains a separate log file for each runtime environment.
h4. What is The Logger?
Rails makes use of Ruby's standard +logger+ to write log information. You can also substitute another logger such as +Log4R+ if you wish.
You can specify an alternative logger in your +environment.rb+ or any environment file:
<ruby>
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Base.logger = Log4r::Logger.new("Application Log")
</ruby>
Or in the +Initializer+ section, add _any_ of the following
<ruby>
config.logger = Logger.new(STDOUT)
config.logger = Log4r::Logger.new("Application Log")
</ruby>
TIP: By default, each log is created under +RAILS_ROOT/log/+ and the log file name is +environment_name.log+.
h4. Log Levels
When something is logged it's printed into the corresponding log if the log level of the message is equal or higher than the configured log level. If you want to know the current log level you can call the +ActiveRecord::Base.logger.level+ method.
The available log levels are: +:debug+, +:info+, +:warn+, +:error+, and +:fatal+, corresponding to the log level numbers from 0 up to 4 respectively. To change the default log level, use
<ruby>
config.log_level = Logger::WARN # In any environment initializer, or
ActiveRecord::Base.logger.level = 0 # at any time
</ruby>
This is useful when you want to log under development or staging, but you don't want to flood your production log with unnecessary information.
TIP: The default Rails log level is +info+ in production mode and +debug+ in development and test mode.
h4. Sending Messages
To write in the current log use the +logger.(debug|info|warn|error|fatal)+ method from within a controller, model or mailer:
<ruby>
logger.debug "Person attributes hash: #{@person.attributes.inspect}"
logger.info "Processing the request..."
logger.fatal "Terminating application, raised unrecoverable error!!!"
</ruby>
Here's an example of a method instrumented with extra logging:
<ruby>
class PostsController < ApplicationController
# ...
def create
@post = Post.new(params[:post])
logger.debug "New post: #{@post.attributes.inspect}"
logger.debug "Post should be valid: #{@post.valid?}"
if @post.save
flash[:notice] = 'Post was successfully created.'
logger.debug "The post was saved and now is the user is going to be redirected..."
redirect_to(@post)
else
render :action => "new"
end
end
# ...
end
</ruby>
Here's an example of the log generated by this method:
<shell>
Processing PostsController#create (for 127.0.0.1 at 2008-09-08 11:52:54) [POST]
Session ID: BAh7BzoMY3NyZl9pZCIlMDY5MWU1M2I1ZDRjODBlMzkyMWI1OTg2NWQyNzViZjYiCmZsYXNoSUM6J0FjdGl
vbkNvbnRyb2xsZXI6OkZsYXNoOjpGbGFzaEhhc2h7AAY6CkB1c2VkewA=--b18cd92fba90eacf8137e5f6b3b06c4d724596a4
Parameters: {"commit"=>"Create", "post"=>{"title"=>"Debugging Rails",
"body"=>"I'm learning how to print in logs!!!", "published"=>"0"},
"authenticity_token"=>"2059c1286e93402e389127b1153204e0d1e275dd", "action"=>"create", "controller"=>"posts"}
New post: {"updated_at"=>nil, "title"=>"Debugging Rails", "body"=>"I'm learning how to print in logs!!!",
"published"=>false, "created_at"=>nil}
Post should be valid: true
Post Create (0.000443) INSERT INTO "posts" ("updated_at", "title", "body", "published",
"created_at") VALUES('2008-09-08 14:52:54', 'Debugging Rails',
'I''m learning how to print in logs!!!', 'f', '2008-09-08 14:52:54')
The post was saved and now is the user is going to be redirected...
Redirected to #<Post:0x20af760>
Completed in 0.01224 (81 reqs/sec) | DB: 0.00044 (3%) | 302 Found [http://localhost/posts]
</shell>
Adding extra logging like this makes it easy to search for unexpected or unusual behavior in your logs. If you add extra logging, be sure to make sensible use of log levels, to avoid filling your production logs with useless trivia.
h3. Debugging with ruby-debug
When your code is behaving in unexpected ways, you can try printing to logs or the console to diagnose the problem. Unfortunately, there are times when this sort of error tracking is not effective in finding the root cause of a problem. When you actually need to journey into your running source code, the debugger is your best companion.
The debugger can also help you if you want to learn about the Rails source code but don't know where to start. Just debug any request to your application and use this guide to learn how to move from the code you have written deeper into Rails code.
h4. Setup
The debugger used by Rails, +ruby-debug+, comes as a gem. To install it, just run:
<shell>
$ sudo gem install ruby-debug
</shell>
In case you want to download a particular version or get the source code, refer to the "project's page on rubyforge":http://rubyforge.org/projects/ruby-debug/.
Rails has had built-in support for ruby-debug since Rails 2.0. Inside any Rails application you can invoke the debugger by calling the +debugger+ method.
Here's an example:
<ruby>
class PeopleController < ApplicationController
def new
debugger
@person = Person.new
end
end
</ruby>
If you see the message in the console or logs:
<shell>
***** Debugger requested, but was not available: Start server with --debugger to enable *****
</shell>
Make sure you have started your web server with the option +--debugger+:
<shell>
~/PathTo/rails_project$ script/server --debugger
=> Booting Mongrel (use 'script/server webrick' to force WEBrick)
=> Rails 2.2.0 application starting on http://0.0.0.0:3000
=> Debugger enabled
...
</shell>
TIP: In development mode, you can dynamically +require \'ruby-debug\'+ instead of restarting the server, if it was started without +--debugger+.
In order to use Rails debugging you'll need to be running either *WEBrick* or *Mongrel*. For the moment, no alternative servers are supported.
h4. The Shell
As soon as your application calls the +debugger+ method, the debugger will be started in a debugger shell inside the terminal window where you launched your application server, and you will be placed at ruby-debug's prompt +(rdb:n)+. The _n_ is the thread number. The prompt will also show you the next line of code that is waiting to run.
If you got there by a browser request, the browser tab containing the request will be hung until the debugger has finished and the trace has finished processing the entire request.
For example:
<shell>
@posts = Post.find(:all)
(rdb:7)
</shell>
Now it's time to explore and dig into your application. A good place to start is by asking the debugger for help... so type: +help+ (You didn't see that coming, right?)
<shell>
(rdb:7) help
ruby-debug help v0.10.2
Type 'help <command-name>' for help on a specific command
Available commands:
backtrace delete enable help next quit show trace
break disable eval info p reload source undisplay
catch display exit irb pp restart step up
condition down finish list ps save thread var
continue edit frame method putl set tmate where
</shell>
TIP: To view the help menu for any command use +help <command-name>+ in active debug mode. For example: _+help var+_
The next command to learn is one of the most useful: +list+. You can also abbreviate ruby-debug commands by supplying just enough letters to distinguish them from other commands, so you can also use +l+ for the +list+ command.
This command shows you where you are in the code by printing 10 lines centered around the current line; the current line in this particular case is line 6 and is marked by +=>+.
<shell>
(rdb:7) list
[1, 10] in /PathToProject/posts_controller.rb
1 class PostsController < ApplicationController
2 # GET /posts
3 # GET /posts.xml
4 def index
5 debugger
=> 6 @posts = Post.find(:all)
7
8 respond_to do |format|
9 format.html # index.html.erb
10 format.xml { render :xml => @posts }
</shell>
If you repeat the +list+ command, this time using just +l+, the next ten lines of the file will be printed out.
<shell>
(rdb:7) l
[11, 20] in /PathTo/project/app/controllers/posts_controller.rb
11 end
12 end
13
14 # GET /posts/1
15 # GET /posts/1.xml
16 def show
17 @post = Post.find(params[:id])
18
19 respond_to do |format|
20 format.html # show.html.erb
</shell>
And so on until the end of the current file. When the end of file is reached, the +list+ command will start again from the beginning of the file and continue again up to the end, treating the file as a circular buffer.
h4. The Context
When you start debugging your application, you will be placed in different contexts as you go through the different parts of the stack.
ruby-debug creates a content when a stopping point or an event is reached. The context has information about the suspended program which enables a debugger to inspect the frame stack, evaluate variables from the perspective of the debugged program, and contains information about the place where the debugged program is stopped.
At any time you can call the +backtrace+ command (or its alias +where+) to print the backtrace of the application. This can be very helpful to know how you got where you are. If you ever wondered about how you got somewhere in your code, then +backtrace+ will supply the answer.
<shell>
(rdb:5) where
#0 PostsController.index
at line /PathTo/project/app/controllers/posts_controller.rb:6
#1 Kernel.send
at line /PathTo/project/vendor/rails/actionpack/lib/action_controller/base.rb:1175
#2 ActionController::Base.perform_action_without_filters
at line /PathTo/project/vendor/rails/actionpack/lib/action_controller/base.rb:1175
#3 ActionController::Filters::InstanceMethods.call_filters(chain#ActionController::Fil...,...)
at line /PathTo/project/vendor/rails/actionpack/lib/action_controller/filters.rb:617
...
</shell>
You move anywhere you want in this trace (thus changing the context) by using the +frame _n_+ command, where _n_ is the specified frame number.
<shell>
(rdb:5) frame 2
#2 ActionController::Base.perform_action_without_filters
at line /PathTo/project/vendor/rails/actionpack/lib/action_controller/base.rb:1175
</shell>
The available variables are the same as if you were running the code line by line. After all, that's what debugging is.
Moving up and down the stack frame: You can use +up [n]+ (+u+ for abbreviated) and +down [n]+ commands in order to change the context _n_ frames up or down the stack respectively. _n_ defaults to one. Up in this case is towards higher-numbered stack frames, and down is towards lower-numbered stack frames.
h4. Threads
The debugger can list, stop, resume and switch between running threads by using the command +thread+ (or the abbreviated +th+). This command has a handful of options:
* +thread+ shows the current thread.
* +thread list+ is used to list all threads and their statuses. The plus + character and the number indicates the current thread of execution.
* +thread stop _n_+ stop thread _n_.
* +thread resume _n_+ resumes thread _n_.
* +thread switch _n_+ switches the current thread context to _n_.
This command is very helpful, among other occasions, when you are debugging concurrent threads and need to verify that there are no race conditions in your code.
h4. Inspecting Variables
Any expression can be evaluated in the current context. To evaluate an expression, just type it!
This example shows how you can print the instance_variables defined within the current context:
<shell>
@posts = Post.find(:all)
(rdb:11) instance_variables
["@_response", "@action_name", "@url", "@_session", "@_cookies", "@performed_render", "@_flash", "@template", "@_params", "@before_filter_chain_aborted", "@request_origin", "@_headers", "@performed_redirect", "@_request"]
</shell>
As you may have figured out, all of the variables that you can access from a controller are displayed. This list is dynamically updated as you execute code. For example, run the next line using +next+ (you'll learn more about this command later in this guide).
<shell>
(rdb:11) next
Processing PostsController#index (for 127.0.0.1 at 2008-09-04 19:51:34) [GET]
Session ID: BAh7BiIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6Rmxhc2g6OkZsYXNoSGFzaHsABjoKQHVzZWR7AA==--b16e91b992453a8cc201694d660147bba8b0fd0e
Parameters: {"action"=>"index", "controller"=>"posts"}
/PathToProject/posts_controller.rb:8
respond_to do |format|
</shell>
And then ask again for the instance_variables:
<shell>
(rdb:11) instance_variables.include? "@posts"
true
</shell>
Now +@posts+ is a included in the instance variables, because the line defining it was executed.
TIP: You can also step into *irb* mode with the command +irb+ (of course!). This way an irb session will be started within the context you invoked it. But be warned: this is an experimental feature.
The +var+ method is the most convenient way to show variables and their values:
<shell>
var
(rdb:1) v[ar] const <object> show constants of object
(rdb:1) v[ar] g[lobal] show global variables
(rdb:1) v[ar] i[nstance] <object> show instance variables of object
(rdb:1) v[ar] l[ocal] show local variables
</shell>
This is a great way to inspect the values of the current context variables. For example:
<shell>
(rdb:9) var local
__dbg_verbose_save => false
</shell>
You can also inspect for an object method this way:
<shell>
(rdb:9) var instance Post.new
@attributes = {"updated_at"=>nil, "body"=>nil, "title"=>nil, "published"=>nil, "created_at"...
@attributes_cache = {}
@new_record = true
</shell>
TIP: The commands +p+ (print) and +pp+ (pretty print) can be used to evaluate Ruby expressions and display the value of variables to the console.
You can use also +display+ to start watching variables. This is a good way of tracking the values of a variable while the execution goes on.
<shell>
(rdb:1) display @recent_comments
1: @recent_comments =
</shell>
The variables inside the displaying list will be printed with their values after you move in the stack. To stop displaying a variable use +undisplay _n_+ where _n_ is the variable number (1 in the last example).
h4. Step by Step
Now you should know where you are in the running trace and be able to print the available variables. But lets continue and move on with the application execution.
Use +step+ (abbreviated +s+) to continue running your program until the next logical stopping point and return control to ruby-debug.
TIP: You can also use +step+ _n_+ and +step- _n_+ to move forward or backward _n_ steps respectively.
You may also use +next+ which is similar to step, but function or method calls that appear within the line of code are executed without stopping. As with step, you may use plus sign to move _n_ steps.
The difference between +next+ and +step+ is that +step+ stops at the next line of code executed, doing just a single step, while +next+ moves to the next line without descending inside methods.
For example, consider this block of code with an included +debugger+ statement:
<ruby>
class Author < ActiveRecord::Base
has_one :editorial
has_many :comments
def find_recent_comments(limit = 10)
debugger
@recent_comments ||= comments.find(
:all,
:conditions => ["created_at > ?", 1.week.ago],
:limit => limit
)
end
end
</ruby>
TIP: You can use ruby-debug while using script/console. Just remember to +require "ruby-debug"+ before calling the +debugger+ method.
<shell>
/PathTo/project $ script/console
Loading development environment (Rails 2.1.0)
>> require "ruby-debug"
=> []
>> author = Author.first
=> #<Author id: 1, first_name: "Bob", last_name: "Smith", created_at: "2008-07-31 12:46:10", updated_at: "2008-07-31 12:46:10">
>> author.find_recent_comments
/PathTo/project/app/models/author.rb:11
)
</shell>
With the code stopped, take a look around:
<shell>
(rdb:1) list
[6, 15] in /PathTo/project/app/models/author.rb
6 debugger
7 @recent_comments ||= comments.find(
8 :all,
9 :conditions => ["created_at > ?", 1.week.ago],
10 :limit => limit
=> 11 )
12 end
13 end
</shell>
You are at the end of the line, but... was this line executed? You can inspect the instance variables.
<shell>
(rdb:1) var instance
@attributes = {"updated_at"=>"2008-07-31 12:46:10", "id"=>"1", "first_name"=>"Bob", "las...
@attributes_cache = {}
</shell>
+@recent_comments+ hasn't been defined yet, so it's clear that this line hasn't been executed yet. Use the +next+ command to move on in the code:
<shell>
(rdb:1) next
/PathTo/project/app/models/author.rb:12
@recent_comments
(rdb:1) var instance
@attributes = {"updated_at"=>"2008-07-31 12:46:10", "id"=>"1", "first_name"=>"Bob", "las...
@attributes_cache = {}
@comments = []
@recent_comments = []
</shell>
Now you can see that the +@comments+ relationship was loaded and @recent_comments defined because the line was executed.
If you want to go deeper into the stack trace you can move single +steps+, through your calling methods and into Rails code. This is one of the best ways to find bugs in your code, or perhaps in Ruby or Rails.
h4. Breakpoints
A breakpoint makes your application stop whenever a certain point in the program is reached. The debugger shell is invoked in that line.
You can add breakpoints dynamically with the command +break+ (or just +b+). There are 3 possible ways of adding breakpoints manually:
* +break line+: set breakpoint in the _line_ in the current source file.
* +break file:line [if expression]+: set breakpoint in the _line_ number inside the _file_. If an _expression_ is given it must evaluated to _true_ to fire up the debugger.
* +break class(.|\#)method [if expression]+: set breakpoint in _method_ (. and \# for class and instance method respectively) defined in _class_. The _expression_ works the same way as with file:line.
<shell>
(rdb:5) break 10
Breakpoint 1 file /PathTo/project/vendor/rails/actionpack/lib/action_controller/filters.rb, line 10
</shell>
Use +info breakpoints _n_+ or +info break _n_+ to list breakpoints. If you supply a number, it lists that breakpoint. Otherwise it lists all breakpoints.
<shell>
(rdb:5) info breakpoints
Num Enb What
1 y at filters.rb:10
</shell>
To delete breakpoints: use the command +delete _n_+ to remove the breakpoint number _n_. If no number is specified, it deletes all breakpoints that are currently active..
<shell>
(rdb:5) delete 1
(rdb:5) info breakpoints
No breakpoints.
</shell>
You can also enable or disable breakpoints:
* +enable breakpoints+: allow a list _breakpoints_ or all of them if no list is specified, to stop your program. This is the default state when you create a breakpoint.
* +disable breakpoints+: the _breakpoints_ will have no effect on your program.
h4. Catching Exceptions
The command +catch exception-name+ (or just +cat exception-name+) can be used to intercept an exception of type _exception-name_ when there would otherwise be is no handler for it.
To list all active catchpoints use +catch+.
h4. Resuming Execution
There are two ways to resume execution of an application that is stopped in the debugger:
* +continue+ [line-specification] (or +c+): resume program execution, at the address where your script last stopped; any breakpoints set at that address are bypassed. The optional argument line-specification allows you to specify a line number to set a one-time breakpoint which is deleted when that breakpoint is reached.
* +finish+ [frame-number] (or +fin+): execute until the selected stack frame returns. If no frame number is given, the application will run until the currently selected frame returns. The currently selected frame starts out the most-recent frame or 0 if no frame positioning (e.g up, down or frame) has been performed. If a frame number is given it will run until the specified frame returns.
h4. Editing
Two commands allow you to open code from the debugger into an editor:
* +edit [file:line]+: edit _file_ using the editor specified by the EDITOR environment variable. A specific _line_ can also be given.
* +tmate _n_+ (abbreviated +tm+): open the current file in TextMate. It uses n-th frame if _n_ is specified.
h4. Quitting
To exit the debugger, use the +quit+ command (abbreviated +q+), or its alias +exit+.
A simple quit tries to terminate all threads in effect. Therefore your server will be stopped and you will have to start it again.
h4. Settings
There are some settings that can be configured in ruby-debug to make it easier to debug your code. Here are a few of the available options:
* +set reload+: Reload source code when changed.
* +set autolist+: Execute +list+ command on every breakpoint.
* +set listsize _n_+: Set number of source lines to list by default to _n_.
* +set forcestep+: Make sure the +next+ and +step+ commands always move to a new line
You can see the full list by using +help set+. Use +help set _subcommand_+ to learn about a particular +set+ command.
TIP: You can include any number of these configuration lines inside a +.rdebugrc+ file in your HOME directory. ruby-debug will read this file every time it is loaded. and configure itself accordingly.
Here's a good start for an +.rdebugrc+:
<shell>
set autolist
set forcestep
set listsize 25
</shell>
h3. Debugging Memory Leaks
A Ruby application (on Rails or not), can leak memory - either in the Ruby code or at the C code level.
In this section, you will learn how to find and fix such leaks by using Bleak House and Valgrind debugging tools.
h4. BleakHouse
"BleakHouse":http://github.com/fauna/bleak_house/tree/master is a library for finding memory leaks.
If a Ruby object does not go out of scope, the Ruby Garbage Collector won't sweep it since it is referenced somewhere. Leaks like this can grow slowly and your application will consume more and more memory, gradually affecting the overall system performance. This tool will help you find leaks on the Ruby heap.
To install it run:
<shell>
sudo gem install bleak_house
</shell>
Then setup you application for profiling. Then add the following at the bottom of config/environment.rb:
<ruby>
require 'bleak_house' if ENV['BLEAK_HOUSE']
</ruby>
Start a server instance with BleakHouse integration:
<shell>
RAILS_ENV=production BLEAK_HOUSE=1 ruby-bleak-house ./script/server
</shell>
Make sure to run a couple hundred requests to get better data samples, then press +CTRL-C+. The server will stop and Bleak House will produce a dumpfile in +/tmp+:
<shell>
** BleakHouse: working...
** BleakHouse: complete
** Bleakhouse: run 'bleak /tmp/bleak.5979.0.dump' to analyze.
</shell>
To analyze it, just run the listed command. The top 20 leakiest lines will be listed:
<shell>
191691 total objects
Final heap size 191691 filled, 220961 free
Displaying top 20 most common line/class pairs
89513 __null__:__null__:__node__
41438 __null__:__null__:String
2348 /opt/local//lib/ruby/site_ruby/1.8/rubygems/specification.rb:557:Array
1508 /opt/local//lib/ruby/gems/1.8/specifications/gettext-1.90.0.gemspec:14:String
1021 /opt/local//lib/ruby/gems/1.8/specifications/heel-0.2.0.gemspec:14:String
951 /opt/local//lib/ruby/site_ruby/1.8/rubygems/version.rb:111:String
935 /opt/local//lib/ruby/site_ruby/1.8/rubygems/specification.rb:557:String
834 /opt/local//lib/ruby/site_ruby/1.8/rubygems/version.rb:146:Array
...
</shell>
This way you can find where your application is leaking memory and fix it.
If "BleakHouse":http://github.com/fauna/bleak_house/tree/master doesn't report any heap growth but you still have memory growth, you might have a broken C extension, or real leak in the interpreter. In that case, try using Valgrind to investigate further.
h4. Valgrind
"Valgrind":http://valgrind.org/ is a Linux-only application for detecting C-based memory leaks and race conditions.
There are Valgrind tools that can automatically detect many memory management and threading bugs, and profile your programs in detail. For example, a C extension in the interpreter calls +malloc()+ but is doesn't properly call +free()+, this memory won't be available until the app terminates.
For further information on how to install Valgrind and use with Ruby, refer to "Valgrind and Ruby":http://blog.evanweaver.com/articles/2008/02/05/valgrind-and-ruby/ by Evan Weaver.
h3. Plugins for Debugging
There are some Rails plugins to help you to find errors and debug your application. Here is a list of useful plugins for debugging:
* "Footnotes":http://github.com/drnic/rails-footnotes/tree/master: Every Rails page has footnotes that give request information and link back to your source via TextMate.
* "Query Trace":http://github.com/ntalbott/query_trace/tree/master: Adds query origin tracing to your logs.
* "Query Stats":http://github.com/dan-manges/query_stats/tree/master: A Rails plugin to track database queries.
* "Query Reviewer":http://code.google.com/p/query-reviewer/: This rails plugin not only runs "EXPLAIN" before each of your select queries in development, but provides a small DIV in the rendered output of each page with the summary of warnings for each query that it analyzed.
* "Exception Notifier":http://github.com/rails/exception_notification/tree/master: Provides a mailer object and a default set of templates for sending email notifications when errors occur in a Rails application.
* "Exception Logger":http://github.com/defunkt/exception_logger/tree/master: Logs your Rails exceptions in the database and provides a funky web interface to manage them.
h3. References
* "ruby-debug Homepage":http://www.datanoise.com/ruby-debug
* "Article: Debugging a Rails application with ruby-debug":http://www.sitepoint.com/article/debug-rails-app-ruby-debug/
* "ruby-debug Basics screencast":http://brian.maybeyoureinsane.net/blog/2007/05/07/ruby-debug-basics-screencast/
* "Ryan Bate's ruby-debug screencast":http://railscasts.com/episodes/54-debugging-with-ruby-debug
* "Ryan Bate's stack trace screencast":http://railscasts.com/episodes/24-the-stack-trace
* "Ryan Bate's logger screencast":http://railscasts.com/episodes/56-the-logger
* "Debugging with ruby-debug":http://bashdb.sourceforge.net/ruby-debug.html
* "ruby-debug cheat sheet":http://cheat.errtheblog.com/s/rdebug/
* "Ruby on Rails Wiki: How to Configure Logging":http://wiki.rubyonrails.org/rails/pages/HowtoConfigureLogging
* "Bleak House Documentation":http://blog.evanweaver.com/files/doc/fauna/bleak_house/files/README.html
h3. Changelog
"Lighthouse ticket":http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/5
* November 3, 2008: Accepted for publication. Added RJS, memory leaks and plugins chapters by "Emilio Tagua":credits.html#miloops
* October 19, 2008: Copy editing pass by "Mike Gunderloy":credits.html#mgunderloy
* September 16, 2008: initial version by "Emilio Tagua":credits.html#miloops

View File

@ -0,0 +1,770 @@
h2. Rails form helpers
Forms in web applications are an essential interface for user input. However, form markup can quickly become tedious to write and maintain because of form control naming and their numerous attributes. Rails deals away with these complexities by providing view helpers for generating form markup. However, since they have different use-cases, developers are required to know all the differences between similar helper methods before putting them to use.
In this guide you will:
* Create search forms and similar kind of generic forms not representing any specific model in your application
* Make model-centric forms for creation and editing of specific database records
* Generate select boxes from multiple types of data
* Understand the date and time helpers Rails provides
* Learn what makes a file upload form different
* Find out where to look for complex forms
endprologue.
NOTE: This guide is not intended to be a complete documentation of available form helpers and their arguments. Please visit "the Rails API documentation":http://api.rubyonrails.org/ for a complete reference.
h3. Dealing With Basic Forms
The most basic form helper is +form_tag+.
<erb>
<% form_tag do %>
Form contents
<% end %>
</erb>
When called without arguments like this, it creates a form element that has the current page as its action and "post" as its method (some line breaks added for readability):
Sample output from +form_tag+:
<html>
<form action="/home/index" method="post">
<div style="margin:0;padding:0">
<input name="authenticity_token" type="hidden" value="f755bb0ed134b76c432144748a6d4b7a7ddf2b71" />
</div>
Form contents
</form>
</html>
If you carefully observe this output, you can see that the helper generated something you didn't specify: a +div+ element with a hidden input inside. This is a security feature of Rails called *cross-site request forgery protection* and form helpers generate it for every form whose action is not "get" (provided that this security feature is enabled). You can read more about this in the "Ruby On Rails Security Guide":./security.html#_cross_site_reference_forgery_csrf.
NOTE: Throughout this guide, this +div+ with the hidden input will be stripped away to have clearer code samples.
h4. A Generic search form
Probably the most minimal form often seen on the web is a search form with a single text input for search terms. This form consists of:
# a form element with "GET" method,
# a label for the input,
# a text input element, and
# a submit element.
IMPORTANT: Always use "GET" as the method for search forms. This allows users are able to bookmark a specific search and get back to it, more generally Rails encourages you to use the right HTTP verb for an action.
To create this form you will use +form_tag+, +label_tag+, +text_field_tag+ and +submit_tag+, respectively.
A basic search form
<html>
<% form_tag(search_path, :method => "get") do %>
<%= label_tag(:q, "Search for:") %>
<%= text_field_tag(:q) %>
<%= submit_tag("Search") %>
<% end %>
</html>
TIP: +search_path+ can be a named route specified in "routes.rb": <pre>map.search "search", :controller => "search"</pre>
The above view code will result in the following markup:
<html>
<form action="/search" method="get">
<label for="q">Search for:</label>
<input id="q" name="q" type="text" />
<input name="commit" type="submit" value="Search" />
</form>
</html>
Besides +text_field_tag+ and +submit_tag+, there is a similar helper for _every_ form control in HTML.
TIP: For every form input, an ID attribute is generated from its name ("q" in the example). These IDs can be very useful for CSS styling or manipulation of form controls with JavaScript.
h4. Multiple hashes in form helper calls
By now you've seen that the +form_tag+ helper accepts 2 arguments: the path for the action and an options hash. This hash specifies the method of form submission and HTML options such as the form element's class.
As with the +link_to+ helper, the path argument doesn't have to be given a string. It can be a hash of URL parameters that Rails' routing mechanism will turn into a valid URL. Still, you cannot simply write this:
A bad way to pass multiple hashes as method arguments:
<ruby>
form_tag(:controller => "people", :action => "search", :method => "get", :class => "nifty_form")
# => <form action="/people/search?method=get&class=nifty_form" method="post">
</ruby>
Here you wanted to pass two hashes, but the Ruby interpreter sees only one hash, so Rails will construct a URL with extraneous parameters. The solution is to delimit the first hash (or both hashes) with curly brackets:
The correct way of passing multiple hashes as arguments:
<ruby>
form_tag({:controller => "people", :action => "search"}, :method => "get", :class => "nifty_form")
# => <form action="/people/search" method="get" class="nifty_form">
</ruby>
This is a common pitfall when using form helpers, since many of them accept multiple hashes. So in future, if a helper produces unexpected output, make sure that you have delimited the hash parameters properly.
WARNING: Do not delimit the second hash without doing so with the first hash, otherwise your method invocation will result in an +expecting tASSOC+ syntax error.
h4. Helpers for generating form elements
Rails provides a series of helpers for generating form elements such as checkboxes, text fields, radio buttons and so. These basic helpers, with names ending in _tag such as +text_field_tag+, +check_box_tag+ just generate a single +&lt;input&gt;+ element. The first parameter to these is always the name of the input. In the controller, this name will be the key in the +params+ hash used to get the value entered by the user. For example if the form contains
<erb>
<%= text_field_tag(:query) %>
</erb>
then the controller code should use
<ruby>
params[:query]
</ruby>
to retrieve the value entered by the user. When naming inputs be aware that Rails uses certain conventions that control whether values are at the top level of the +params+ hash, inside an array or a nested hash and so on. You can read more about them in the parameter_names section. For details on the precise usage of these helpers, please refer to the "API documentation":http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html.
h5. Checkboxes
Checkboxes are form controls that give the user a set of options they can enable or disable:
<erb>
<%= check_box_tag(:pet_dog) %>
<%= label_tag(:pet_dog, "I own a dog") %>
<%= check_box_tag(:pet_cat) %>
<%= label_tag(:pet_cat, "I own a cat") %>
output:
<input id="pet_dog" name="pet_dog" type="checkbox" value="1" />
<label for="pet_dog">I own a dog</label>
<input id="pet_cat" name="pet_cat" type="checkbox" value="1" />
<label for="pet_cat">I own a cat</label>
</erb>
The second parameter to +check_box_tag+ is the value of the input. This is the value that will be submitted by the browser if the checkbox is ticked (i.e. the value that will be present in the +params+ hash). With the above form you would check the value of +params[:pet_dog]+ and +params[:pet_cat]+ to see which pets the user owns.
h5. Radio buttons
Radio buttons, while similar to checkboxes, are controls that specify a set of options in which they are mutually exclusive (user can only pick one):
<erb>
<%= radio_button_tag(:age, "child") %>
<%= label_tag(:age_child, "I am younger than 21") %>
<%= radio_button_tag(:age, "adult") %>
<%= label_tag(:age_adult, "I'm over 21") %>
output:
<input id="age_child" name="age" type="radio" value="child" />
<label for="age_child">I am younger than 21</label>
<input id="age_adult" name="age" type="radio" value="adult" />
<label for="age_adult">I'm over 21</label>
</erb>
As with +check_box_tag+ the second parameter to +radio_button_tag+ is the value of the input. Because these two radio buttons share the same name (age) the user will only be able to select one and +params[:age]+ will contain either "child" or "adult".
IMPORTANT: Always use labels for each checkbox and radio button. They associate text with a specific option and provide a larger clickable region.
h4. Other helpers of interest
Other form controls worth mentioning are the text area, password input and hidden input:
<erb>
<%= text_area_tag(:message, "Hi, nice site", :size => "24x6") %>
<%= password_field_tag(:password) %>
<%= hidden_field_tag(:parent_id, "5") %>
output:
<textarea id="message" name="message" cols="24" rows="6">Hi, nice site</textarea>
<input id="password" name="password" type="password" />
<input id="parent_id" name="parent_id" type="hidden" value="5" />
</erb>
Hidden inputs are not shown to the user, but they hold data like any textual input. Values inside them can be changed with JavaScript.
TIP: If you're using password input fields (for any purpose), you might want to prevent their values showing up in application logs by activating +filter_parameter_logging(:password)+ in your ApplicationController.
h3. Dealing With Model Objects
h4. Model object helpers
A particularly common task for a form is editing or creating a model object. While the +*_tag+ helpers can certainly be used for this task they are somewhat verbose as for each tag you would have to ensure the correct parameter name is used and set the default value of the input appropriately. Rails provides helpers tailored to this task. These helpers lack the <notextile>_tag</notextile> suffix, for example +text_field+, +text_area+.
For these helpers the first argument is the name of an instance variable and the second is the name of a method (usually an attribute) to call on that object. Rails will set the value of the input control to the return value of that method for the object and set an appropriate input name. If your controller has defined +@person+ and that person's name is Henry then a form containing:
<erb>
<%= text_field(:person, :name) %>
</erb>
will produce output similar to
<erb>
<input id="person_name" name="person[name]" type="text" value="Henry"/>
</erb>
Upon form submission the value entered by the user will be stored in +params[:person][:name]+. The +params[:person]+ hash is suitable for passing to +Person.new+ or, if +@person+ is an instance of Person, +@person.update_attributes+. While the name of an attribute is the most common second parameter to these helpers this is not compulsory. In the example above, as long as person objects have a +name+ and a +name=+ method Rails will be happy.
WARNING: You must pass the name of an instance variable, i.e. +:person+ or +"person"+, not an actual instance of your model object.
Rails provides helpers for displaying the validation errors associated with a model object. These are covered in detail by the "Active Record Validations and Callbacks":./activerecord_validations_callbacks.html#_using_the_tt_errors_tt_collection_in_your_view_templates guide.
h4. Binding a form to an object
While this is an increase in comfort it is far from perfect. If Person has many attributes to edit then we would be repeating the name of the edited object many times. What we want to do is somehow bind a form to a model object which is exactly what +form_for+ does.
Assume we have a controller for dealing with articles:
articles_controller.rb:
<ruby>
def new
@article = Article.new
end
</ruby>
The corresponding view using +form_for+ looks like this
articles/new.html.erb:
<erb>
<% form_for :article, @article, :url => { :action => "create" }, :html => {:class => "nifty_form"} do |f| %>
<%= f.text_field :title %>
<%= f.text_area :body, :size => "60x12" %>
<%= submit_tag "Create" %>
<% end %>
</erb>
There are a few things to note here:
# +:article+ is the name of the model and +@article+ is the actual object being edited.
# There is a single hash of options. Routing options are passed inside +:url+ hash, HTML options are passed in the +:html+ hash.
# The +form_for+ method yields a *form builder* object (the +f+ variable).
# Methods to create form controls are called *on* the form builder object +f+
The resulting HTML is:
<html>
<form action="/articles/create" method="post" class="nifty_form">
<input id="article_title" name="article[title]" size="30" type="text" />
<textarea id="article_body" name="article[body]" cols="60" rows="12"></textarea>
<input name="commit" type="submit" value="Create" />
</form>
</html>
The name passed to +form_for+ controls the key used in +params+ to access the form's values. Here the name is +article+ and so all the inputs have names of the form +article[attribute_name]+. Accordingly, in the +create+ action +params[:article]+ will be a hash with keys +:title+ and +:body+. You can read more about the significance of input names in the <<parameter_names,parameter names>> section.
The helper methods called on the form builder are identical to the model object helpers except that it is not necessary to specify which object is being edited since this is already managed by the form builder.
You can create a similar binding without actually creating +&lt;form&gt;+ tags with the +fields_for+ helper. This is useful for editing additional model objects with the same form. For example if you had a Person model with an associated ContactDetail model you could create a form for creating both like so:
<erb>
<% form_for :person, @person, :url => { :action => "create" } do |person_form| %>
<%= person_form.text_field :name %>
<% fields_for @person.contact_detail do |contact_details_form| %>
<%= contact_details_form.text_field :phone_number %>
<% end %>
<% end %>
</erb>
which produces the following output:
<html>
<form action="/people/create" class="new_person" id="new_person" method="post">
<input id="person_name" name="person[name]" size="30" type="text" />
<input id="contact_detail_phone_number" name="contact_detail[phone_number]" size="30" type="text" />
</form>
</html>
The object yielded by +fields_for+ is a form builder like the one yielded by +form_for+ (in fact +form_for+ calls +fields_for+ internally).
h4. Relying on record identification
The Article model is directly available to users of the application, so -- following the best practices for developing with Rails -- you should declare it *a resource*.
When dealing with RESTful resources, calls to +form_for+ can get significantly easier if you rely on *record identification*. In short, you can just pass the model instance and have Rails figure out model name and the rest:
<ruby>
## Creating a new article
# long-style:
form_for(:article, @article, :url => articles_path)
# same thing, short-style (record identification gets used):
form_for(@article)
## Editing an existing article
# long-style:
form_for(:article, @article, :url => article_path(@article), :method => "put")
# short-style:
form_for(@article)
</ruby>
Notice how the short-style +form_for+ invocation is conveniently the same, regardless of the record being new or existing. Record identification is smart enough to figure out if the record is new by asking +record.new_record?+. It also selects the correct path to submit to and the name based on the class of the object.
Rails will also automatically set the +class+ and +id+ of the form appropriately: a form creating an article would have +id+ and +class+ +new_article+. If you were editing the article with id 23 the +class+ would be set to +edit_article+ and the id to +edit_article_23+. These attributes will be omitted for brevity in the rest of this guide.
WARNING: When you're using STI (single-table inheritance) with your models, you can't rely on record identification on a subclass if only their parent class is declared a resource. You will have to specify the model name, +:url+ and +:method+ explicitly.
h5. Dealing with namespaces
If you have created namespaced routes +form_for+ has a nifty shorthand for that too. If your application has an admin namespace then
<ruby>
form_for [:admin, @article]
</ruby>
will create a form that submits to the articles controller inside the admin namespace (submitting to +admin_article_path(@article)+ in the case of an update). If you have several levels of namespacing then the syntax is similar:
<ruby>
form_for [:admin, :management, @article]
</ruby>
For more information on Rails' routing system and the associated conventions, please see the "routing guide":./routing_outside_in.html.
h4. How do forms with PUT or DELETE methods work?
Rails framework encourages RESTful design of your applications, which means you'll be making a lot of "PUT" and "DELETE" requests (besides "GET" and "POST"). Still, most browsers _don't support_ methods other than "GET" and "POST" when it comes to submitting forms.
Rails works around this issue by emulating other methods over POST with a hidden input named +"_method"+ that is set to reflect the desired method:
<ruby>
form_tag(search_path, :method => "put")
</ruby>
output:
<html>
<form action="/search" method="post">
<div style="margin:0;padding:0">
<input name="_method" type="hidden" value="put" />
<input name="authenticity_token" type="hidden" value="f755bb0ed134b76c432144748a6d4b7a7ddf2b71" />
</div>
...
</html>
When parsing POSTed data, Rails will take into account the special +_method+ parameter and acts as if the HTTP method was the one specified inside it ("PUT" in this example).
h3. Making select boxes with ease
Select boxes in HTML require a significant amount of markup (one +OPTION+ element for each option to choose from), therefore it makes the most sense for them to be dynamically generated.
Here is what the markup might look like:
<html>
<select name="city_id" id="city_id">
<option value="1">Lisbon</option>
<option value="2">Madrid</option>
...
<option value="12">Berlin</option>
</select>
</html>
Here you have a list of cities whose names are presented to the user. Internally the application only wants to handle their IDs so they are used as the options' value attribute. Let's see how Rails can help out here.
h4. The select and options tag
The most generic helper is +select_tag+, which -- as the name implies -- simply generates the +SELECT+ tag that encapsulates an options string:
<erb>
<%= select_tag(:city_id, '<option value="1">Lisbon</option>...') %>
</erb>
This is a start, but it doesn't dynamically create the option tags. You can generate option tags with the +options_for_select+ helper:
<erb>
<%= options_for_select([['Lisbon', 1], ['Madrid', 2], ...]) %>
output:
<option value="1">Lisbon</option>
<option value="2">Madrid</option>
...
</erb>
The first argument to +options_for_select+ is a nested array where each element has two elements: option text (city name) and option value (city id). The option value is what will be submitted to your controller. Often this will be the id of a corresponding database object but this does not have to be the case.
Knowing this, you can combine +select_tag+ and +options_for_select+ to achieve the desired, complete markup:
<erb>
<%= select_tag(:city_id, options_for_select(...)) %>
</erb>
+options_for_select+ allows you to pre-select an option by passing its value.
<erb>
<%= options_for_select([['Lisbon', 1], ['Madrid', 2], ...], 2) %>
output:
<option value="1">Lisbon</option>
<option value="2" selected="selected">Madrid</option>
...
</erb>
Whenever Rails sees that the internal value of an option being generated matches this value, it will add the +selected+ attribute to that option.
TIP: The second argument to +options_for_select+ must be exactly equal to the desired internal value. In particular if the value is the integer 2 you cannot pass "2" to +options_for_select+ -- you must pass 2. Be aware of values extracted from the +params+ hash as they are all strings.
h4. Select boxes for dealing with models
In most cases form controls will be tied to a specific database model and as you might expect Rails provides helpers tailored for that purpose. Consistent with other form helpers, when dealing with models you drop the +<notextile>_tag</notextile>+ suffix from +select_tag+:
<ruby>
# controller:
@person = Person.new(:city_id => 2)
</ruby>
<erb>
# view:
<%= select(:person, :city_id, [['Lisbon', 1], ['Madrid', 2], ...]) %>
</erb>
Notice that the third parameter, the options array, is the same kind of argument you pass to +options_for_select+. One advantage here is that you don't have to worry about pre-selecting the correct city if the user already has one -- Rails will do this for you by reading from the +@person.city_id+ attribute.
As with other helpers, if you were to use +select+ helper on a form builder scoped to +@person+ object, the syntax would be:
<erb>
# select on a form builder
<%= f.select(:city_id, ...) %>
</erb>
WARNING: If you are using +select+ (or similar helpers such as +collection_select+, +select_tag+) to set a +belongs_to+ association you must pass the name of the foreign key (in the example above +city_id+), not the name of association itself. If you specify +city+ instead of +city_id+ Active Record will raise an error along the lines of <pre> ActiveRecord::AssociationTypeMismatch: City(#17815740) expected, got String(#1138750) </pre> when you pass the +params+ hash to +Person.new+ or +update_attributes+. Another way of looking at this is that form helpers only edit attributes. You should also be aware of the potential security ramifications of allowing users to edit foreign keys directly. You may wish to consider the use of +attr_protected+ and +attr_accessible+. For further details on this, see the "Ruby On Rails Security Guide":security.html#_mass_assignment.
h4. Option tags from a collection of arbitrary objects
Generating options tags with +options_for_select+ requires that you create an array containing the text and value for each option. But what if you had a City model (perhaps an Active Record one) and you wanted to generate option tags from a collection of those objects? One solution would be to make a nested array by iterating over them:
<erb>
<% cities_array = City.all.map { |city| [city.name, city.id] } %>
<%= options_for_select(cities_array) %>
</erb>
This is a perfectly valid solution, but Rails provides a less verbose alternative: +options_from_collection_for_select+. This helper expects a collection of arbitrary objects and two additional arguments: the names of the methods to read the option *value* and *text* from, respectively:
<erb>
<%= options_from_collection_for_select(City.all, :id, :name) %>
</erb>
As the name implies, this only generates option tags. To generate a working select box you would need to use it in conjunction with +select_tag+, just as you would with +options_for_select+. When working with model objects, just as +select+ combines +select_tag+ and +options_for_select+, +collection_select+ combines +select_tag+ with +options_from_collection_for_select+.
<erb>
<%= collection_select(:person, :city_id, City.all, :id, :name) %>
</erb>
To recap, +options_from_collection_for_select+ is to +collection_select+ what +options_for_select+ is to +select+.
NOTE: Pairs passed to +options_for_select+ should have the name first and the id second, however with +options_from_collection_for_select+ the first argument is the value method and the second the text method.
h4. Time zone and country select
To leverage time zone support in Rails, you have to ask your users what time zone they are in. Doing so would require generating select options from a list of pre-defined TimeZone objects using +collection_select+, but you can simply use the +time_zone_select+ helper that already wraps this:
<erb>
<%= time_zone_select(:person, :time_zone) %>
</erb>
There is also +time_zone_options_for_select+ helper for a more manual (therefore more customizable) way of doing this. Read the API documentation to learn about the possible arguments for these two methods.
Rails _used_ to have a +country_select+ helper for choosing countries but this has been extracted to the "country_select plugin":http://github.com/rails/country_select/tree/master. When using this do be aware that the exclusion or inclusion of certain names from the list can be somewhat controversial (and was the reason this functionality was extracted from rails).
h3. Using Date and Time Form Helpers
The date and time helpers differ from all the other form helpers in two important respects:
# Dates and times are not representable by a single input element. Instead you have several, one for each component (year, month, day etc.) and so there is no single value in your +params+ hash with your date or time.
# Other helpers use the +_tag+ suffix to indicate whether a helper is a barebones helper or one that operates on model objects. With dates and times, +select_date+, +select_time+ and +select_datetime+ are the barebones helpers, +date_select+, +time_select+ and +datetime_select+ are the equivalent model object helpers.
Both of these families of helpers will create a series of select boxes for the different components (year, month, day etc.).
h4. Barebones helpers
The +select_*+ family of helpers take as their first argument an instance of Date, Time or DateTime that is used as the currently selected value. You may omit this parameter, in which case the current date is used. For example
<erb>
<%= select_date Date.today, :prefix => :start_date %>
</erb>
outputs (with actual option values omitted for brevity)
<html>
<select id="start_date_year" name="start_date[year]"> ... </select>
<select id="start_date_month" name="start_date[month]"> ... </select>
<select id="start_date_day" name="start_date[day]"> ... </select>
</html>
The above inputs would result in +params[:start_date]+ being a hash with keys +:year+, +:month+, +:day+. To get an actual Time or Date object you would have to extract these values and pass them to the appropriate constructor, for example
<ruby>
Date.civil(params[:start_date][:year].to_i, params[:start_date][:month].to_i, params[:start_date][:day].to_i)
</ruby>
The +:prefix+ option is the key used to retrieve the hash of date components from the +params+ hash. Here it was set to +start_date+, if omitted it will default to +date+.
h4. Model object helpers
+select_date+ does not work well with forms that update or create Active Record objects as Active Record expects each element of the +params+ hash to correspond to one attribute.
The model object helpers for dates and times submit parameters with special names, when Active Record sees parameters with such names it knows they must be combined with the other parameters and given to a constructor appropriate to the column type. For example:
<erb>
<%= date_select :person, :birth_date %>
</erb>
outputs (with actual option values omitted for brevity)
<html>
<select id="person_birth_date_1i" name="person[birth_date(1i)]"> ... </select>
<select id="person_birth_date_2i" name="person[birth_date(2i)]"> ... </select>
<select id="person_birth_date_3i" name="person[birth_date(3i)]"> ... </select>
</html>
which results in a +params+ hash like
<ruby>
{:person => {'birth_date(1i)' => '2008', 'birth_date(2i)' => '11', 'birth_date(3i)' => '22'}}
</ruby>
When this is passed to +Person.new+ (or +update_attributes+), Active Record spots that these parameters should all be used to construct the +birth_date+ attribute and uses the suffixed information to determine in which order it should pass these parameters to functions such as +Date.civil+.
h4. Common options
Both families of helpers use the same core set of functions to generate the individual select tags and so both accept largely the same options. In particular, by default Rails will generate year options 5 years either side of the current year. If this is not an appropriate range, the +:start_year+ and +:end_year+ options override this. For an exhaustive list of the available options, refer to the "API documentation":http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html.
As a rule of thumb you should be using +date_select+ when working with model objects and +select_date+ in others cases, such as a search form which filters results by date.
NOTE: In many cases the built in date pickers are clumsy as they do not aid the user in working out the relationship between the date and the day of the week.
h4. Individual components
Occasionally you need to display just a single date component such as a year or a month. Rails provides a series of helpers for this, one for each component +select_year+, +select_month+, +select_day+, +select_hour+, +select_minute+, +select_second+. These helpers are fairly straightforward. By default they will generate a input named after the time component (for example "year" for +select_year+, "month" for +select_month+ etc.) although this can be overriden with the +:field_name+ option. The +:prefix+ option works in the same way that it does for +select_date+ and +select_time+ and has the same default value.
The first parameter specifies which value should be selected and can either be an instance of a Date, Time or DateTime, in which case the relevant component will be extracted, or a numerical value. For example
<erb>
<%= select_year(2009) %>
<%= select_year(Time.now) %>
</erb>
will produce the same output if the current year is 2009 and the value chosen by the user can be retrieved by +params[:date][:year]+.
h3. Uploading Files
A common task is uploading some sort of file, whether it's a picture of a person or a CSV file containing data to process. The most important thing to remember with file uploads is that the form's encoding *MUST* be set to "multipart/form-data". If you forget to do this the file will not be uploaded. This can be done by passing +:multi_part => true+ as an HTML option. This means that in the case of +form_tag+ it must be passed in the second options hash and in the case of +form_for+ inside the +:html+ hash.
The following two forms both upload a file.
<erb>
<% form_tag({:action => :upload}, :multipart => true) do %>
<%= file_field_tag 'picture' %>
<% end %>
<% form_for @person, :html => {:multipart => true} do |f| %>
<%= f.file_field :picture %>
<% end %>
</erb>
Rails provides the usual pair of helpers: the barebones +file_field_tag+ and the model oriented +file_field+. The only difference with other helpers is that you cannot set a default value for file inputs as this would have no meaning. As you would expect in the first case the uploaded file is in +params[:picture]+ and in the second case in +params[:person][:picture]+.
h4. What gets uploaded
The object in the +params+ hash is an instance of a subclass of IO. Depending on the size of the uploaded file it may in fact be a StringIO or an instance of File backed by a temporary file. In both cases the object will have an +original_filename+ attribute containing the name the file had on the user's computer and a +content_type+ attribute containing the MIME type of the uploaded file. The following snippet saves the uploaded content in +#\{Rails.root\}/public/uploads+ under the same name as the original file (assuming the form was the one in the previous example).
<ruby>
def upload
uploaded_io = params[:person][:picture]
File.open(Rails.root.join('public', 'uploads', uploaded_io.original_filename), 'w') do |file|
file.write(uploaded_io.read)
end
end
</ruby>
Once a file has been uploaded there are a multitude of potential tasks, ranging from where to store the files (on disk, Amazon S3, etc) and associating them with models to resizing image files and generating thumbnails. The intricacies of this are beyond the scope of this guide, but there are several plugins designed to assist with these. Two of the better known ones are "Attachment-Fu":http://github.com/technoweenie/attachment_fu and "Paperclip":http://www.thoughtbot.com/projects/paperclip.
NOTE: If the user has not selected a file the corresponding parameter will be an empty string.
h4. Dealing with Ajax
Unlike other forms making an asynchronous file upload form is not as simple as replacing +form_for+ with +remote_form_for+. With an Ajax form the serialization is done by JavaScript running inside the browser and since JavaScript cannot read files from your hard drive the file cannot be uploaded. The most common workaround is to use an invisible iframe that serves as the target for the form submission.
h3. Customising Form Builders
As mentioned previously the object yielded by +form_for+ and +fields_for+ is an instance of FormBuilder (or a subclass thereof). Form builders encapsulate the notion of displaying form elements for a single object. While you can of course write helpers for your forms in the usual way you can also subclass FormBuilder and add the helpers there. For example
<erb>
<% form_for @person do |f| %>
<%= text_field_with_label f, :first_name %>
<% end %>
</erb>
can be replaced with
<erb>
<% form_for @person, :builder => LabellingFormBuilder do |f| %>
<%= f.text_field :first_name %>
<% end %>
</erb>
by defining a LabellingFormBuilder class similar to the following:
<ruby>
class LabellingFormBuilder < FormBuilder
def text_field(attribute, options={})
label(attribute) + text_field(attribute, options)
end
end
</ruby>
If you reuse this frequently you could define a +labeled_form_for+ helper that automatically applies the +:builder => LabellingFormBuilder+ option.
The form builder used also determines what happens when you do
<erb>
<%= render :partial => f %>
</erb>
If +f+ is an instance of FormBuilder then this will render the +form+ partial, setting the partial's object to the form builder. If the form builder is of class LabellingFormBuilder then the +labelling_form+ partial would be rendered instead.
h3. Understanding Parameter Naming Conventions
As you've seen in the previous sections, values from forms can be at the top level of the +params+ hash or nested in another hash. For example in a standard +create+
action for a Person model, +params[:model]+ would usually be a hash of all the attributes for the person to create. The +params+ hash can also contain arrays, arrays of hashes and so on.
Fundamentally HTML forms don't know about any sort of structured data, all they generate is name-value pairs, where pairs are just plain strings. The arrays and hashes you see in your application are the result of some parameter naming conventions that Rails uses.
TIP: You may find you can try out examples in this section faster by using the console to directly invoke Rails' parameter parser. For example <pre> ActionController::UrlEncodedPairParser.parse_query_parameters "name=fred&phone=0123456789" # => {"name"=>"fred", "phone"=>"0123456789"} </pre>
h4. Basic structures
The two basic structures are arrays and hashes. Hashes mirror the syntax used for accessing the value in +params+. For example if a form contains
<html>
<input id="person_name" name="person[name]" type="text" value="Henry"/>
</html>
the +params+ hash will contain
<erb>
{'person' => {'name' => 'Henry'}}
</erb>
and +params["name"]+ will retrieve the submitted value in the controller.
Hashes can be nested as many levels as required, for example
<html>
<input id="person_address_city" name="person[address][city]" type="text" value="New York"/>
</html>
will result in the +params+ hash being
<ruby>
{'person' => {'address' => {'city' => 'New York'}}}
</ruby>
Normally Rails ignores duplicate parameter names. If the parameter name contains [] then they will be accumulated in an array. If you wanted people to be able to input multiple phone numbers, your could place this in the form:
<html>
<input name="person[phone_number][]" type="text"/>
<input name="person[phone_number][]" type="text"/>
<input name="person[phone_number][]" type="text"/>
</html>
This would result in +params[:person][:phone_number]+ being an array.
h4. Combining them
We can mix and match these two concepts. For example, one element of a hash might be an array as in the previous example, or you can have an array of hashes. For example a form might let you create any number of addresses by repeating the following form fragment
<html>
<input name="addresses[][line1]" type="text"/>
<input name="addresses[][line2]" type="text"/>
<input name="addresses[][city]" type="text"/>
</html>
This would result in +params[:addresses]+ being an array of hashes with keys +line1+, +line2+ and +city+. Rails decides to start accumulating values in a new hash whenever it encounters an input name that already exists in the current hash.
There's a restriction, however, while hashes can be nested arbitrarily, only one level of "arrayness" is allowed. Arrays can be usually replaced by hashes, for example instead of having an array of model objects one can have a hash of model objects keyed by their id, an array index or some other parameter.
WARNING: Array parameters do not play well with the +check_box+ helper. According to the HTML specification unchecked checkboxes submit no value. However it is often convenient for a checkbox to always submit a value. The +check_box+ helper fakes this by creating a second hidden input with the same name. If the checkbox is unchecked only the hidden input is submitted and if it is checked then both are submitted but the value submitted by the checkbox takes precedence. When working with array parameters this duplicate submission will confuse Rails since duplicate input names are how it decides when to start a new array element. It is preferable to either use +check_box_tag+ or to use hashes instead of arrays.
h4. Using form helpers
The previous sections did not use the Rails form helpers at all. While you can craft the input names yourself and pass them directly to helpers such as +text_field_tag+ Rails also provides higher level support. The two tools at your disposal here are the name parameter to +form_for+ and +fields_for+ and the +:index+ option that helpers take.
You might want to render a form with a set of edit fields for each of a person's addresses. For example:
<erb>
<% form_for @person do |person_form| %>
<%= person_form.text_field :name %>
<% for address in @person.addresses %>
<% person_form.fields_for address, :index => address do |address_form|%>
<%= address_form.text_field :city %>
<% end %>
<% end %>
<% end %>
</erb>
Assuming the person had two addresses, with ids 23 and 45 this would create output similar to this:
<html>
<form action="/people/1" class="edit_person" id="edit_person_1" method="post">
<input id="person_name" name="person[name]" size="30" type="text" />
<input id="person_address_23_city" name="person[address][23][city]" size="30" type="text" />
<input id="person_address_45_city" name="person[address][45][city]" size="30" type="text" />
</form>
</html>
This will result in a +params+ hash that looks like
<ruby>
{'person' => {'name' => 'Bob', 'address' => {'23' => {'city' => 'Paris'}, '45' => {'city' => 'London'}}}}
</ruby>
Rails knows that all these inputs should be part of the person hash because you called +fields_for+ on the first form builder. By specifying an +:index+ option you're telling rails that instead of naming the inputs +person[address][city]+ it should insert that index surrounded by [] between the address and the city. If you pass an Active Record object as we did then Rails will call +to_param+ on it, which by default returns the database id. This is often useful as it is then easy to locate which Address record should be modified. You can pass numbers with some other significance, strings or even +nil+ (which will result in an array parameter being created).
To create more intricate nestings, you can specify the first part of the input name (+person[address]+ in the previous example) explicitly, for example
<erb>
<% fields_for 'person[address][primary]', address, :index => address do |address_form| %>
<%= address_form.text_field :city %>
<% end %>
</erb>
will create inputs like
<html>
<input id="person_address_primary_1_city" name="person[address][primary][1][city]" size="30" type="text" value="bologna" />
</html>
As a general rule the final input name is the concatenation of the name given to +fields_for+/+form_for+, the index value and the name of the attribute. You can also pass an +:index+ option directly to helpers such as +text_field+, but it is usually less repetitive to specify this at the form builder level rather than on individual input controls.
As a shortcut you can append [] to the name and omit the +:index+ option. This is the same as specifing +:index => address+ so
<erb>
<% fields_for 'person[address][primary][]', address do |address_form| %>
<%= address_form.text_field :city %>
<% end %>
</erb>
produces exactly the same output as the previous example.
h3. Building Complex forms
Many apps grow beyond simple forms editing a single object. For example when creating a Person you might want to allow the user to (on the same form) create multiple address records (home, work, etc.). When later editing that person the user should be able to add, remove or amend addresses as necessary. While this guide has shown you all the pieces necessary to handle this, Rails does not yet have a standard end-to-end way of accomplishing this, but many have come up with viable approaches. These include:
* Ryan Bates' series of railscasts on "complex forms":http://railscasts.com/episodes/75
* Handle Multiple Models in One Form from "Advanced Rails Recipes":http://media.pragprog.com/titles/fr_arr/multiple_models_one_form.pdf
* Eloy Duran's "nested_params":http://github.com/alloy/complex-form-examples/tree/alloy-nested_params plugin
* Lance Ivy's "nested_assignment":http://github.com/cainlevy/nested_assignment/tree/master plugin and "sample application":http://github.com/cainlevy/complex-form-examples/tree/cainlevy
* James Golick's "attribute_fu":http://github.com/giraffesoft/attribute_fu/tree plugin
h3. Changelog
"Lighthouse ticket":http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/1
h3. Authors
* Mislav Marohnić <mislav.marohnic@gmail.com>
* "Frederick Cheung":credits.html#fcheung

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,884 @@
h2. Rails Internationalization (I18n) API
The Ruby I18n (shorthand for _internationalization_) gem which is shipped with Ruby on Rails (starting from Rails 2.2) provides an easy-to-use and extensible framework for *translating your application to a single custom language* other than English or for *providing multi-language support* in your application.
The process of "internationalization" usually means to abstract all strings and other locale specific bits (such as date or currency formats) out of your application. The process of "localization" means to provide translations and localized formats for these bits. [1]
So, in the process of _internationalizing_ your Rails application you have to:
* Ensure you have support for i18n
* Tell Rails where to find locale dictionaries
* Tell Rails how to set, preserve and switch locale
In the process of _localizing_ your application you'll probably want to do following three things:
* Replace or supplement Rails' default locale -- eg. date and time formats, month names, ActiveRecord model names, etc
* Abstract texts in your application into keyed dictionaries -- eg. flash messages, static texts in your views, etc
* Store the resulting dictionaries somewhere
This guide will walk you through the I18n API and contains a tutorial how to internationalize a Rails application from the start.
endprologue.
NOTE: The Ruby I18n framework provides you with all neccessary means for internationalization/localization of your Rails application. You may, however, use any of various plugins and extensions available, which add additional functionality or features. See Rails "I18n Wiki":http://rails-i18n.org/wiki for more information.
h3. How I18n in Ruby on Rails works
Internationalization is a complex problem. Natural languages differ in so many ways (eg. in pluralization rules) that it is hard to provide tools for solving all problems at once. For that reason the Rails I18n API focuses on:
* providing support for English and similar languages out of the box
* making it easy to customize and extend everything for other languages
As part of this solution, *every static string in the Rails framework* -- eg. Active Record validation messages, time and date formats -- *has been internationalized*, so _localization_ of a Rails application means "over-riding" these defaults.
h4. The overall architecture of the library
Thus, the Ruby I18n gem is split into two parts:
* The public API of the i18n framework -- a Ruby module with public methods and definitions how the library works
* A default backend (which is intentionally named _Simple_ backend) that implements these methods
As a user you should always only access the public methods on the I18n module, but it is useful to know about the capabilities of the backend.
NOTE: It is possible (or even desirable) to swap the shipped Simple backend with a more powerful one, which would store translation data in a relational database, GetText dictionary, or similar. See section "Using different backends":#usingdifferentbackends below.
h4. The public I18n API
The most important methods of the I18n API are:
<ruby>
translate # Lookup text translations
localize # Localize Date and Time objects to local formats
</ruby>
These have the aliases #t and #l so you can use them like this:
<ruby>
I18n.t 'store.title'
I18n.l Time.now
</ruby>
There are also attribute readers and writers for the following attributes:
<ruby>
load_path # Announce your custom translation files
locale # Get and set the current locale
default_locale # Get and set the default locale
exception_handler # Use a different exception_handler
backend # Use a different backend
</ruby>
So, let's internationalize a simple Rails application from the ground up in the next chapters!
h3. Setup the Rails application for internationalization
There are just a few, simple steps to get up and running with I18n support for your application.
h4. Configure the I18n module
Following the _convention over configuration_ philosophy, Rails will set-up your application with reasonable defaults. If you need different settings, you can overwrite them easily.
Rails adds all +.rb+ and +.yml+ files from +config/locales+ directory to your *translations load path*, automatically.
See the default +en.yml+ locale in this directory, containing a sample pair of translation strings:
<ruby>
en:
hello: "Hello world"
</ruby>
This means, that in the +:en+ locale, the key _hello_ will map to _Hello world_ string. Every string inside Rails is internationalized in this way, see for instance Active Record validation messages in the "+activerecord/lib/active_record/locale/en.yml+":http://github.com/rails/rails/blob/master/activerecord/lib/active_record/locale/en.yml file or time and date formats in the "+activesupport/lib/active_support/locale/en.yml+":http://github.com/rails/rails/blob/master/activesupport/lib/active_support/locale/en.yml file. You can use YAML or standard Ruby Hashes to store translations in the default (Simple) backend.
The I18n library will use *English* as a *default locale*, ie. if you don't set a different locale, +:en+ will be used for looking up translations.
NOTE: The i18n library takes *pragmatic approach* to locale keys (after "some discussion":http://groups.google.com/group/rails-i18n/browse_thread/thread/14dede2c7dbe9470/80eec34395f64f3c?hl=en), including only the _locale_ ("language") part, like +:en+, +:pl+, not the _region_ part, like +:en-US+ or +:en-UK+, which are traditionally used for separating "languages" and "regional setting" or "dialects". (For instance, in the +:en-US+ locale you would have $ as a currency symbol, while in +:en-UK+, you would have £. Also, insults would be different in American and British English :) Reason for this pragmatic approach is that most of the time, you usually care about making your application available in different "languages", and working with locales is much simpler this way. However, nothing stops you from separating regional and other settings in the traditional way. In this case, you could eg. inherit from the default +en+ locale and then provide UK specific settings in a +:en-UK+ dictionary.
The *translations load path* (+I18n.load_path+) is just a Ruby Array of paths to your translation files that will be loaded automatically and available in your application. You can pick whatever directory and translation file naming scheme makes sense for you.
NOTE: The backend will lazy-load these translations when a translation is looked up for the first time. This makes it possible to just swap the backend with something else even after translations have already been announced.
The default +environment.rb+ files has instruction how to add locales from another directory and how to set different default locale. Just uncomment and edit the specific lines.
<ruby>
# The internationalization framework can be changed
# to have another default locale (standard is :en) or more load paths.
# All files from config/locales/*.rb,yml are added automatically.
# config.i18n.load_path << Dir[File.join(RAILS_ROOT, 'my', 'locales', '*.{rb,yml}')]
# config.i18n.default_locale = :de
</ruby>
h4. Optional: custom I18n configuration setup
For the sake of completeness, let's mention that if you do not want to use the +environment.rb+ file for some reason, you can always wire up things manually, too.
To tell the I18n library where it can find your custom translation files you can specify the load path anywhere in your application - just make sure it gets run before any translations are actually looked up. You might also want to change the default locale. The simplest thing possible is to put the following into an *initializer*:
<ruby>
# in config/initializer/locale.rb
# tell the I18n library where to find your translations
I18n.load_path << Dir[ File.join(RAILS_ROOT, 'lib', 'locale', '*.{rb,yml}') ]
# set default locale to something else then :en
I18n.default_locale = :pt
</ruby>
h4. Setting and passing the locale
If you want to translate your Rails application to a *single language other than English* (the default locale), you can set I18n.default_locale to your locale in +environment.rb+ or an initializer as shown above, and it will persist through the requests.
However, you would probably like to *provide support for more locales* in your application. In such case, you need to set and pass the locale between requests.
WARNING: You may be tempted to store choosed locale in a _session_ or a _cookie_. *Do not do so*. The locale should be transparent and a part of the URL. This way you don't break people's basic assumptions about the web itself: if you send a URL of some page to a friend, she should see the same page, same content. A fancy word for this would be that you're being "_RESTful_":http://en.wikipedia.org/wiki/Representational_State_Transfer. Read more about RESTful approach in "Stefan Tilkov's articles":http://www.infoq.com/articles/rest-introduction. There may be some exceptions to this rule, which are discussed below.
The _setting part_ is easy. You can set locale in a +before_filter+ in the ApplicationController like this:
<ruby>
before_filter :set_locale
def set_locale
# if params[:locale] is nil then I18n.default_locale will be used
I18n.locale = params[:locale]
end
</ruby>
This requires you to pass the locale as a URL query parameter as in +http://example.com/books?locale=pt+. (This is eg. Google's approach). So +http://localhost:3000?locale=pt+ will load the Portugese localization, whereas +http://localhost:3000?locale=de+ would load the German localization, and so on. You may skip the next section and head over to the *Internationalize your application* section, if you want to try things out by manually placing locale in the URL and reloading the page.
Of course, you probably don't want to manually include locale in every URL all over your application, or want the URLs look differently, eg. the usual +http://example.com/pt/books+ versus +http://example.com/en/books+. Let's discuss the different options you have.
IMPORTANT: Following examples rely on having locales loaded into your application available as an array of strings like +["en", "es", "gr"]+. This is not inclued in current version of Rails 2.2 -- forthcoming Rails version 2.3 will contain easy accesor +available_locales+. (See "this commit":http://github.com/svenfuchs/i18n/commit/411f8fe7 and background at "Rails I18n Wiki":http://rails-i18n.org/wiki/pages/i18n-available_locales.)
So, for having available locales easily available in Rails 2.2, we have to include this support manually in an initializer, like this:
<ruby>
# config/initializers/available_locales.rb
#
# Get loaded locales conveniently
# See http://rails-i18n.org/wiki/pages/i18n-available_locales
module I18n
class << self
def available_locales; backend.available_locales; end
end
module Backend
class Simple
def available_locales; translations.keys.collect { |l| l.to_s }.sort; end
end
end
end
# You need to "force-initialize" loaded locales
I18n.backend.send(:init_translations)
AVAILABLE_LOCALES = I18n.backend.available_locales
RAILS_DEFAULT_LOGGER.debug "* Loaded locales: #{AVAILABLE_LOCALES.inspect}"
</ruby>
You can then wrap the constant for easy access in ApplicationController:
<ruby>
class ApplicationController < ActionController::Base
def available_locales; AVAILABLE_LOCALES; end
end
</ruby>
h4. Setting locale from the domain name
One option you have is to set the locale from the domain name where your application runs. For example, we want +www.example.com+ to load English (or default) locale, and +www.example.es+ to load Spanish locale. Thus the _top-level domain name_ is used for locale setting. This has several advantages:
* Locale is an _obvious_ part of the URL
* People intuitively grasp in which language the content will be displayed
* It is very trivial to implement in Rails
* Search engines seem to like that content in different languages lives at different, inter-linked domains
You can implement it like this in your ApplicationController:
<ruby>
before_filter :set_locale
def set_locale
I18n.locale = extract_locale_from_uri
end
# Get locale from top-level domain or return nil if such locale is not available
# You have to put something like:
# 127.0.0.1 application.com
# 127.0.0.1 application.it
# 127.0.0.1 application.pl
# in your /etc/hosts file to try this out locally
def extract_locale_from_tld
parsed_locale = request.host.split('.').last
(available_locales.include? parsed_locale) ? parsed_locale : nil
end
</ruby>
We can also set the locale from the _subdomain_ in very similar way:
<ruby>
# Get locale code from request subdomain (like http://it.application.local:3000)
# You have to put something like:
# 127.0.0.1 gr.application.local
# in your /etc/hosts file to try this out locally
def extract_locale_from_subdomain
parsed_locale = request.subdomains.first
(available_locales.include? parsed_locale) ? parsed_locale : nil
end
</ruby>
If your application includes a locale switching menu, you would then have something like this in it:
<ruby>
link_to("Deutsch", "#{APP_CONFIG[:deutsch_website_url]}#{request.env['REQUEST_URI']}")
</ruby>
assuming you would set +APP_CONFIG[:deutsch_website_url]+ to some value like +http://www.application.de+.
This solution has aforementioned advantages, however, you may not be able or may not want to provide different localizations ("language versions") on different domains. The most obvious solution would be to include locale code in the URL params (or request path).
h4. Setting locale from the URL params
Most usual way of setting (and passing) the locale would be to include it in URL params, as we did in the +I18n.locale = params[:locale]+ _before_filter_ in the first example. We would like to have URLs like +www.example.com/books?locale=ja+ or +www.example.com/ja/books+ in this case.
This approach has almost the same set of advantages as setting the locale from domain name: namely that it's RESTful and in accord with rest of the World Wide Web. It does require a little bit more work to implement, though.
Getting the locale from +params+ and setting it accordingly is not hard; including it in every URL and thus *passing it through the requests* is. To include an explicit option in every URL (eg. +link_to( books_url(:locale => I18n.locale) )+) would be tedious and probably impossible, of course.
Rails contains infrastructure for "centralizing dynamic decisions about the URLs" in its "+*ApplicationController#default_url_options*+":http://api.rubyonrails.org/classes/ActionController/Base.html#M000515, which is useful precisely in this scenario: it enables us to set "defaults" for "+url_for+":http://api.rubyonrails.org/classes/ActionController/Base.html#M000503 and helper methods dependent on it (by implementing/overriding this method).
We can include something like this in our ApplicationController then:
<ruby>
# app/controllers/application_controller.rb
def default_url_options(options={})
logger.debug "default_url_options is passed options: #{options.inspect}\n"
{ :locale => I18n.locale }
end
</ruby>
Every helper method dependent on +url_for+ (eg. helpers for named routes like +root_path+ or +root_url+, resource routes like +books_path+ or +books_url+, etc.) will now *automatically include the locale in the query string*, like this: +http://localhost:3001/?locale=ja+.
You may be satisfied with this. It does impact the readability of URLs, though, when the locale "hangs" at the end of every URL in your application. Moreover, from the architectural standpoint, locale is usually hierarchically above the other parts of application domain: and URLs should reflect this.
You probably want URLs look like this: +www.example.com/en/books+ (which loads English locale) and +www.example.com/nl/books+ (which loads Netherlands locale). This is achievable with the "over-riding +default_url_options+" strategy from above: you just have to set up your routes with "+path_prefix+":http://api.rubyonrails.org/classes/ActionController/Resources.html#M000354 option in this way:
<ruby>
# config/routes.rb
map.resources :books, :path_prefix => '/:locale'
</ruby>
Now, when you call +books_path+ method you should get +"/en/books"+ (for the default locale). An URL like +http://localhost:3001/nl/books+ should load the Netherlands locale, then, and following calls to +books_path+ should return +"/nl/books"+ (because the locale changed).
Of course, you need to take special care of root URL (usually "homepage" or "dashboard") of your application. An URL like +http://localhost:3001/nl+ will not work automatically, because the +map.root :controller => "dashboard"+ declaration in your +routes.rb+ doesn't take locale into account. (And rightly so. There's only one "root" URL.)
You would probably need to map URLs like these:
<ruby>
# config/routes.rb
map.dashboard '/:locale', :controller => "dashboard"
</ruby>
Do take special care about the *order of your routes*, so this route declaration does not "eat" other ones. (You may want to add it directly before the +map.root+ declaration.)
IMPORTANT: This solution has currently one rather big *downside*. Due to the _default_url_options_ implementation, you have to pass the +:id+ option explicitely, like this: +link_to 'Show', book_url(:id => book)+ and not depend on Rails' magic in code like +link_to 'Show', book+. If this should be a problem, have a look on two plugins which simplify working with routes in this way: Sven Fuchs's "_routing_filter_":http://github.com/svenfuchs/routing-filter/tree/master and Raul Murciano's "_translate_routes_":http://github.com/raul/translate_routes/tree/master. See also the page "How to encode the current locale in the URL":http://rails-i18n.org/wiki/pages/how-to-encode-the-current-locale-in-the-url in the Rails i18n Wiki.
h4. Setting locale from the client supplied information
In specific cases, it would make sense to set locale from client supplied information, ie. not from URL. This information may come for example from users' preffered language (set in their browser), can be based on users' geographical location inferred from their IP, or users can provide it simply by choosing locale in your application interface and saving it to their profile. This approach is more suitable for web-based applications or services, not for websites -- see the box about _sessions_, _cookies_ and RESTful architecture above.
h5. Using Accept-Language
One source of client supplied information would be an +Accept-Language+ HTTP header. People may "set this in their browser":http://www.w3.org/International/questions/qa-lang-priorities or other clients (such as _curl_).
A trivial implementation of using +Accept-Language+ header would be:
<ruby>
def set_locale
logger.debug "* Accept-Language: #{request.env['HTTP_ACCEPT_LANGUAGE']}"
I18n.locale = extract_locale_from_accept_language_header
logger.debug "* Locale set to '#{I18n.locale}'"
end
private
def extract_locale_from_accept_language_header
request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[a-z]{2}/).first
end
</ruby>
Of course, in production environment you would need much robust code, and could use a plugin such as Iaian Hecker's "http_accept_language":http://github.com/iain/http_accept_language or even Rack middleware such as Ryan Tomayko's "locale":http://github.com/rtomayko/rack-contrib/blob/master/lib/rack/locale.rb.
h5. Using GeoIP (or similar) database
Another way of choosing the locale from client's information would be to use a database for mapping client IP to region, such as "GeoIP Lite Country":http://www.maxmind.com/app/geolitecountry. The mechanics of the code would be very similar to the code above -- you would need to query database for user's IP, and lookup your preffered locale for the country/region/city returned.
h5. User profile
You can also provide users of your application with means to set (and possibly over-ride) locale in your application interface, as well. Again, mechanics for this approach would be very similar to the code above -- you'd probably let users choose a locale from a dropdown list and save it to their profile in database. Then you'd set the locale to this value.
h3. Internationalizing your application
OK! Now you've initialized I18n support for your Ruby on Rails application and told it which locale should be used and how to preserve it between requests. With that in place, you're now ready for the really interesting stuff.
Let's _internationalize_ our application, ie. abstract every locale-specific parts, and that _localize_ it, ie. provide neccessary translations for these abstracts.
You most probably have something like this in one of your applications:
<ruby>
# config/routes.rb
ActionController::Routing::Routes.draw do |map|
map.root :controller => 'home', :action => 'index'
end
# app/controllers/home_controller.rb
class HomeController < ApplicationController
def index
flash[:notice] = "Hello flash!"
end
end
# app/views/home/index.html.erb
<h1>Hello world!</h1>
<p><%= flash[:notice] %></p>
</ruby>
!images/i18n/demo_untranslated.png(rails i18n demo untranslated)!
h4. Adding Translations
Obviously there are *two strings that are localized to English*. In order to internationalize this code, *replace these strings* with calls to Rails' +#t+ helper with a key that makes sense for the translation:
<ruby>
# app/controllers/home_controller.rb
class HomeController < ApplicationController
def index
flash[:notice] = t(:hello_flash)
end
end
# app/views/home/index.html.erb
<h1><%=t :hello_world %></h1>
<p><%= flash[:notice] %></p>
</ruby>
When you now render this view, it will show an error message which tells you that the translations for the keys +:hello_world+ and +:hello_flash+ are missing.
!images/i18n/demo_translation_missing.png(rails i18n demo translation missing)!
NOTE: Rails adds a +t+ (+translate+) helper method to your views so that you do not need to spell out +I18n.t+ all the time. Additionally this helper will catch missing translations and wrap the resulting error message into a +<span class="translation_missing">+.
So let's add the missing translations into the dictionary files (i.e. do the "localization" part):
<ruby>
# config/locale/en.yml
en:
hello_world: Hello World
hello_flash: Hello Flash
# config/locale/pirate.yml
pirate:
hello_world: Ahoy World
hello_flash: Ahoy Flash
</ruby>
There you go. Because you haven't changed the default_locale, I18n will use English. Your application now shows:
!images/i18n/demo_translated_en.png(rails i18n demo translated to english)!
And when you change the URL to pass the pirate locale (+http://localhost:3000?locale=pirate+), you'll get:
!images/i18n/demo_translated_pirate.png(rails i18n demo translated to pirate)!
NOTE: You need to restart the server when you add new locale files.
You may use YAML (+.yml+) or plain Ruby (+.rb+) files for storing your translations in SimpleStore. YAML is the preffered option among Rails developers, has one big disadvantage, though. YAML is very sensitive to whitespace and special characters, so the application may not load your dictionary properly. Ruby files will crash your application on first request, so you may easily find what's wrong. (If you encounter any "weird issues" with YAML dictionaries, try putting the relevant portion of your dictionary into Ruby file.)
h4. Adding Date/Time formats
OK! Now let's add a timestamp to the view, so we can demo the *date/time localization* feature as well. To localize the time format you pass the Time object to +I18n.l+ or (preferably) use Rails' +#l+ helper. You can pick a format by passing the +:format+ option -- by default the +:default+ format is used.
<ruby>
# app/views/home/index.html.erb
<h1><%=t :hello_world %></h1>
<p><%= flash[:notice] %></p
<p><%= l Time.now, :format => :short %></p>
</ruby>
And in our pirate translations file let's add a time format (it's already there in Rails' defaults for English):
<ruby>
# config/locale/pirate.yml
pirate:
time:
formats:
short: "arrrround %H'ish"
</ruby>
So that would give you:
!images/i18n/demo_localized_pirate.png(rails i18n demo localized time to pirate)!
TIP: Right now you might need to add some more date/time formats in order to make the I18n backend work as expected. Of course, there's a great chance that somebody already did all the work by *translating Rails's defaults for your locale*. See the "rails-i18n repository at Github":http://github.com/svenfuchs/rails-i18n/tree/master/rails/locale for an archive of various locale files. When you put such file(s) in +config/locale/+ directory, they will automatically ready for use.
h4. Organization of locale files
When you are using the default SimpleStore, shipped with the i18n library, you store dictionaries in plain-text files on the disc. Putting translations for all parts of your application in one file per locale could be hard to manage. You can store these files in a hierarchy which makes sense to you.
For example, your +config/locale+ directory could look like this:
<pre>
|-defaults
|---es.rb
|---en.rb
|-models
|---book
|-----es.rb
|-----en.rb
|-views
|---defaults
|-----es.rb
|-----en.rb
|---books
|-----es.rb
|-----en.rb
|---users
|-----es.rb
|-----en.rb
|---navigation
|-----es.rb
|-----en.rb
</pre>
This way, you can separate model and model attribute names from text inside views, and all of this from the "defaults" (eg. date and time formats).
Other stores for the i18n library could provide different means of such separation.
Do check the "Rails i18n Wiki":http://rails-i18n.org/wiki for list of tools available for managing translations.
h3. Overview of the I18n API features
You should have good understanding of using the i18n library now, knowing all neccessary aspects of internationalizing a basic Rails application. In the following chapters, we'll cover it's features in more depth.
Covered are features like these:
* looking up translations
* interpolating data into translations
* pluralizing translations
* localizing dates, numbers, currency etc.
h4. Looking up translations
h5. Basic lookup, scopes and nested keys
Translations are looked up by keys which can be both Symbols or Strings, so these calls are equivalent:
<ruby>
I18n.t :message
I18n.t 'message'
</ruby>
+translate+ also takes a +:scope+ option which can contain one or many additional keys that will be used to specify a “namespace” or scope for a translation key:
<ruby>
I18n.t :invalid, :scope => [:active_record, :error_messages]
</ruby>
This looks up the +:invalid+ message in the Active Record error messages.
Additionally, both the key and scopes can be specified as dot separated keys as in:
<ruby>
I18n.translate :"active_record.error_messages.invalid"
</ruby>
Thus the following calls are equivalent:
<ruby>
I18n.t 'active_record.error_messages.invalid'
I18n.t 'error_messages.invalid', :scope => :active_record
I18n.t :invalid, :scope => 'active_record.error_messages'
I18n.t :invalid, :scope => [:active_record, :error_messages]
</ruby>
h5. Defaults
When a default option is given its value will be returned if the translation is missing:
<ruby>
I18n.t :missing, :default => 'Not here'
# => 'Not here'
</ruby>
If the default value is a Symbol it will be used as a key and translated. One can provide multiple values as default. The first one that results in a value will be returned.
E.g. the following first tries to translate the key +:missing+ and then the key +:also_missing.+ As both do not yield a result the string "Not here" will be returned:
<ruby>
I18n.t :missing, :default => [:also_missing, 'Not here']
# => 'Not here'
</ruby>
h5. Bulk and namespace lookup
To lookup multiple translations at once an array of keys can be passed:
<ruby>
I18n.t [:odd, :even], :scope => 'active_record.error_messages'
# => ["must be odd", "must be even"]
</ruby>
Also, a key can translate to a (potentially nested) hash as grouped translations. E.g. one can receive all Active Record error messages as a Hash with:
<ruby>
I18n.t 'active_record.error_messages'
# => { :inclusion => "is not included in the list", :exclusion => ... }
</ruby>
h4. Interpolation
In many cases you want to abstract your translations so that *variables can be interpolated into the translation*. For this reason the I18n API provides an interpolation feature.
All options besides +:default+ and +:scope+ that are passed to +#translate+ will be interpolated to the translation:
<ruby>
I18n.backend.store_translations :en, :thanks => 'Thanks {{name}}!'
I18n.translate :thanks, :name => 'Jeremy'
# => 'Thanks Jeremy!'
</ruby>
If a translation uses +:default+ or +:scope+ as a interpolation variable an I+18n::ReservedInterpolationKey+ exception is raised. If a translation expects an interpolation variable but it has not been passed to +#translate+ an +I18n::MissingInterpolationArgument+ exception is raised.
h4. Pluralization
In English there's only a singular and a plural form for a given string, e.g. "1 message" and "2 messages". Other languages ("Arabic":http://www.unicode.org/cldr/data/charts/supplemental/language_plural_rules.html#ar, "Japanese":http://www.unicode.org/cldr/data/charts/supplemental/language_plural_rules.html#ja, "Russian":http://www.unicode.org/cldr/data/charts/supplemental/language_plural_rules.html#ru and many more) have different grammars that have additional or less "plural forms":http://www.unicode.org/cldr/data/charts/supplemental/language_plural_rules.html. Thus, the I18n API provides a flexible pluralization feature.
The +:count+ interpolation variable has a special role in that it both is interpolated to the translation and used to pick a pluralization from the translations according to the pluralization rules defined by CLDR:
<ruby>
I18n.backend.store_translations :en, :inbox => {
:one => '1 message',
:other => '{{count}} messages'
}
I18n.translate :inbox, :count => 2
# => '2 messages'
</ruby>
The algorithm for pluralizations in +:en+ is as simple as:
<ruby>
entry[count == 1 ? 0 : 1]
</ruby>
I.e. the translation denoted as +:one+ is regarded as singular, the other is used as plural (including the count being zero).
If the lookup for the key does not return an Hash suitable for pluralization an +18n::InvalidPluralizationData+ exception is raised.
h4. Setting and passing a locale
The locale can be either set pseudo-globally to +I18n.locale+ (which uses +Thread.current+ like, e.g., +Time.zone+) or can be passed as an option to +#translate+ and +#localize+.
If no locale is passed +I18n.locale+ is used:
<ruby>
I18n.locale = :de
I18n.t :foo
I18n.l Time.now
</ruby>
Explicitely passing a locale:
<ruby>
I18n.t :foo, :locale => :de
I18n.l Time.now, :locale => :de
</ruby>
+I18n.locale+ defaults to +I18n.default_locale+ which defaults to :+en+. The default locale can be set like this:
<ruby>
I18n.default_locale = :de
</ruby>
h3. How to store your custom translations
The shipped Simple backend allows you to store translations in both plain Ruby and YAML format. [2]
For example a Ruby Hash providing translations can look like this:
<ruby>
{
:pt => {
:foo => {
:bar => "baz"
}
}
}
</ruby>
The equivalent YAML file would look like this:
<ruby>
pt:
foo:
bar: baz
</ruby>
As you see in both cases the toplevel key is the locale. +:foo+ is a namespace key and +:bar+ is the key for the translation "baz".
Here is a "real" example from the ActiveSupport +en.yml+ translations YAML file:
<ruby>
en:
date:
formats:
default: "%Y-%m-%d"
short: "%b %d"
long: "%B %d, %Y"
</ruby>
So, all of the following equivalent lookups will return the +:short+ date format +"%B %d"+:
<ruby>
I18n.t 'date.formats.short'
I18n.t 'formats.short', :scope => :date
I18n.t :short, :scope => 'date.formats'
I18n.t :short, :scope => [:date, :formats]
</ruby>
Generally we recommend using YAML as a format for storing translations. There are cases though where you want to store Ruby lambdas as part of your locale data, e.g. for special date.
h4. Translations for Active Record models
You can use the methods +Model.human_name+ and +Model.human_attribute_name(attribute)+ to transparently lookup translations for your model and attribute names.
For example when you add the following translations:
<ruby>
en:
activerecord:
models:
user: Dude
attributes:
user:
login: "Handle"
# will translate User attribute "login" as "Handle"
</ruby>
Then +User.human_name+ will return "Dude" and +User.human_attribute_name(:login)+ will return "Handle".
h5. Error message scopes
Active Record validation error messages can also be translated easily. Active Record gives you a couple of namespaces where you can place your message translations in order to provide different messages and translation for certain models, attributes and/or validations. It also transparently takes single table inheritance into account.
This gives you quite powerful means to flexibly adjust your messages to your application's needs.
Consider a User model with a +validates_presence_of+ validation for the name attribute like this:
<ruby>
class User < ActiveRecord::Base
validates_presence_of :name
end
</ruby>
The key for the error message in this case is +:blank+. Active Record will lookup this key in the namespaces:
<ruby>
activerecord.errors.messages.models.[model_name].attributes.[attribute_name]
activerecord.errors.messages.models.[model_name]
activerecord.errors.messages
</ruby>
Thus, in our example it will try the following keys in this order and return the first result:
<ruby>
activerecord.errors.messages.models.user.attributes.name.blank
activerecord.errors.messages.models.user.blank
activerecord.errors.messages.blank
</ruby>
When your models are additionally using inheritance then the messages are looked up for the inherited model class names are looked up.
For example, you might have an Admin model inheriting from User:
<ruby>
class Admin < User
validates_presence_of :name
end
</ruby>
Then Active Record will look for messages in this order:
<ruby>
activerecord.errors.models.admin.attributes.title.blank
activerecord.errors.models.admin.blank
activerecord.errors.models.user.attributes.title.blank
activerecord.errors.models.user.blank
activerecord.errors.messages.blank
</ruby>
This way you can provide special translations for various error messages at different points in your models inheritance chain and in the attributes, models or default scopes.
h5. Error message interpolation
The translated model name, translated attribute name, and value are always available for interpolation.
So, for example, instead of the default error message +"can not be blank"+ you could use the attribute name like this : +"Please fill in your {{attribute}}"+.
* +count+, where available, can be used for pluralization if present:
| validation | with option | message | interpolation|
| validates_confirmation_of | - | :confirmation | -|
| validates_acceptance_of | - | :accepted | -|
| validates_presence_of | - | :blank | -|
| validates_length_of | :within, :in | :too_short | count|
| validates_length_of | :within, :in | :too_long | count|
| validates_length_of | :is | :wrong_length | count|
| validates_length_of | :minimum | :too_short | count|
| validates_length_of | :maximum | :too_long | count|
| validates_uniqueness_of | - | :taken | -|
| validates_format_of | - | :invalid | -|
| validates_inclusion_of | - | :inclusion | -|
| validates_exclusion_of | - | :exclusion | -|
| validates_associated | - | :invalid | -|
| validates_numericality_of | - | :not_a_number | -|
| validates_numericality_of | :greater_than | :greater_than | count|
| validates_numericality_of | :greater_than_or_equal_to | :greater_than_or_equal_to | count|
| validates_numericality_of | :equal_to | :equal_to | count|
| validates_numericality_of | :less_than | :less_than | count|
| validates_numericality_of | :less_than_or_equal_to | :less_than_or_equal_to | count|
| validates_numericality_of | :odd | :odd | -|
| validates_numericality_of | :even | :even | -|
h5. Translations for the Active Record error_messages_for helper
If you are using the Active Record +error_messages_for+ helper you will want to add translations for it.
Rails ships with the following translations:
<yaml>
en:
activerecord:
errors:
template:
header:
one: "1 error prohibited this {{model}} from being saved"
other: "{{count}} errors prohibited this {{model}} from being saved"
body: "There were problems with the following fields:"
</yaml>
h4. Overview of other built-in methods that provide I18n support
Rails uses fixed strings and other localizations, such as format strings and other format information in a couple of helpers. Here's a brief overview.
h5. ActionView helper methods
* +distance_of_time_in_words+ translates and pluralizes its result and interpolates the number of seconds, minutes, hours and so on. See "datetime.distance_in_words":http://github.com/rails/rails/blob/master/actionpack/lib/action_view/locale/en.yml#L51 translations.
* +datetime_select+ and +select_month+ use translated month names for populating the resulting select tag. See "date.month_names":http://github.com/rails/rails/blob/master/activesupport/lib/active_support/locale/en.yml#L15 for translations. +datetime_select+ also looks up the order option from "date.order":http://github.com/rails/rails/blob/master/activesupport/lib/active_support/locale/en.yml#L18 (unless you pass the option explicitely). All date select helpers translate the prompt using the translations in the "datetime.prompts":http://github.com/rails/rails/blob/master/actionpack/lib/action_view/locale/en.yml#L83 scope if applicable.
* The +number_to_currency+, +number_with_precision+, +number_to_percentage+, +number_with_delimiter+ and +humber_to_human_size+ helpers use the number format settings located in the "number":http://github.com/rails/rails/blob/master/actionpack/lib/action_view/locale/en.yml#L2 scope.
h5. Active Record methods
* +human_name+ and +human_attribute_name+ use translations for model names and attribute names if available in the "activerecord.models":http://github.com/rails/rails/blob/master/activerecord/lib/active_record/locale/en.yml#L43 scope. They also support translations for inherited class names (e.g. for use with STI) as explained above in "Error message scopes".
* +ActiveRecord::Errors#generate_message+ (which is used by Active Record validations but may also be used manually) uses +human_name+ and +human_attribute_name+ (see above). It also translates the error message and supports translations for inherited class names as explained above in "Error message scopes".
*+ ActiveRecord::Errors#full_messages+ prepends the attribute name to the error message using a separator that will be looked up from "activerecord.errors.format.separator":http://github.com/rails/rails/blob/master/actionpack/lib/action_view/locale/en.yml#L91 (and defaults to +' '+).
h5. ActiveSupport methods
* +Array#to_sentence+ uses format settings as given in the "support.array":http://github.com/rails/rails/blob/master/activesupport/lib/active_support/locale/en.yml#L30 scope.
h3. Customize your I18n setup
h4. Using different backends
For several reasons the shipped Simple backend only does the "simplest thing that ever could work" _for Ruby on Rails_ [3] ... which means that it is only guaranteed to work for English and, as a side effect, languages that are very similar to English. Also, the simple backend is only capable of reading translations but can not dynamically store them to any format.
That does not mean you're stuck with these limitations though. The Ruby I18n gem makes it very easy to exchange the Simple backend implementation with something else that fits better for your needs. E.g. you could exchange it with Globalize's Static backend:
<ruby>
I18n.backend = Globalize::Backend::Static.new
</ruby>
h4. Using different exception handlers
The I18n API defines the following exceptions that will be raised by backends when the corresponding unexpected conditions occur:
<ruby>
MissingTranslationData # no translation was found for the requested key
InvalidLocale # the locale set to I18n.locale is invalid (e.g. nil)
InvalidPluralizationData # a count option was passed but the translation data is not suitable for pluralization
MissingInterpolationArgument # the translation expects an interpolation argument that has not been passed
ReservedInterpolationKey # the translation contains a reserved interpolation variable name (i.e. one of: scope, default)
UnknownFileType # the backend does not know how to handle a file type that was added to I18n.load_path
</ruby>
The I18n API will catch all of these exceptions when they were thrown in the backend and pass them to the default_exception_handler method. This method will re-raise all exceptions except for +MissingTranslationData+ exceptions. When a +MissingTranslationData+ exception has been caught it will return the exceptions error message string containing the missing key/scope.
The reason for this is that during development you'd usually want your views to still render even though a translation is missing.
In other contexts you might want to change this behaviour though. E.g. the default exception handling does not allow to catch missing translations during automated tests easily. For this purpose a different exception handler can be specified. The specified exception handler must be a method on the I18n module:
<ruby>
module I18n
def just_raise_that_exception(*args)
raise args.first
end
end
I18n.exception_handler = :just_raise_that_exception
</ruby>
This would re-raise all caught exceptions including +MissingTranslationData+.
Another example where the default behaviour is less desirable is the Rails TranslationHelper which provides the method +#t+ (as well as +#translate+). When a +MissingTranslationData+ exception occurs in this context the helper wraps the message into a span with the CSS class +translation_missing+.
To do so the helper forces +I18n#translate+ to raise exceptions no matter what exception handler is defined by setting the +:raise+ option:
<ruby>
I18n.t :foo, :raise => true # always re-raises exceptions from the backend
</ruby>
h3. Conclusion
At this point you hopefully have a good overview about how I18n support in Ruby on Rails works and are ready to start translating your project.
If you find anything missing or wrong in this guide please file a ticket on "our issue tracker":http://i18n.lighthouseapp.com/projects/14948-rails-i18n/overview. If you want to discuss certain portions or have questions please sign up to our "mailinglist":http://groups.google.com/group/rails-i18n.
h3. Contributing to Rails I18n
I18n support in Ruby on Rails was introduced in the release 2.2 and is still evolving. The project follows the good Ruby on Rails development tradition of evolving solutions in plugins and real applications first and then cherry-picking the best bread of most widely useful features second for inclusion to the core.
Thus we encourage everybody to experiment with new ideas and features in plugins or other libraries and make them available to the community. (Don't forget to announce your work on our "mailinglist":http://groups.google.com/group/rails-i18n!)
If you find your own locale (language) missing from our "example translations data":http://github.com/svenfuchs/rails-i18n/tree/master/rails/locale repository for Ruby on Rails, please "_fork_":http://github.com/guides/fork-a-project-and-submit-your-modifications the repository, add your data and send a "pull request":http://github.com/guides/pull-requests.
h3. Resources
* "rails-i18n.org":http://rails-i18n.org - Homepage of the rails-i18n project. You can find lots of useful resources on the "wiki":http://rails-i18n.org/wiki.
* "rails-i18n Google group":http://groups.google.com/group/rails-i18n - The project's mailing list.
* "Github: rails-i18n":http://github.com/svenfuchs/rails-i18n/tree/master - Code repository for the rails-i18n project. Most importantly you can find lots of "example translations":http://github.com/svenfuchs/rails-i18n/tree/master/rails/locale for Rails that should work for your application in most cases.
* "Lighthouse: rails-i18n":http://i18n.lighthouseapp.com/projects/14948-rails-i18n/overview - Issue tracker for the rails-i18n project.
* "Github: i18n":http://github.com/svenfuchs/i18n/tree/master - Code repository for the i18n gem.
* "Lighthouse: i18n":http://i18n.lighthouseapp.com/projects/14947-ruby-i18n/overview - Issue tracker for the i18n gem.
h3. Authors
* "Sven Fuchs":http://www.workingwithrails.com/person/9963-sven-fuchs (initial author)
* "Karel Minařík":http://www.workingwithrails.com/person/7476-karel-mina-k
If you found this guide useful please consider recommending its authors on "workingwithrails":http://www.workingwithrails.com.
h3. Footnotes
fn1. Or, to quote "Wikipedia":http://en.wikipedia.org/wiki/Internationalization_and_localization: _"Internationalization is the process of designing a software application so that it can be adapted to various languages and regions without engineering changes. Localization is the process of adapting software for a specific region or language by adding locale-specific components and translating text."_
fn2. Other backends might allow or require to use other formats, e.g. a GetText backend might allow to read GetText files.
fn3. One of these reasons is that we don't want to any unnecessary load for applications that do not need any I18n capabilities, so we need to keep the I18n library as simple as possible for English. Another reason is that it is virtually impossible to implement a one-fits-all solution for all problems related to I18n for all existing languages. So a solution that allows us to exchange the entire implementation easily is appropriate anyway. This also makes it much easier to experiment with custom features and extensions.
h3. Changelog
"Lighthouse ticket":http://rails.lighthouseapp.com/projects/16213/tickets/23

View File

@ -0,0 +1,112 @@
<% content_for :header_section do %>
h2. Ruby on Rails guides
These guides are designed to make you immediately productive with Rails, and to help you understand how all of the pieces fit together. There are two different versions of the Guides site, and you should be sure to use the one that applies to your situation:
* "Current Release version":http://guides.rubyonrails.org - based on Rails 2.3
* "Edge version":http://guides.rails.info - based on the current Rails "master branch":http://github.com/rails/rails/tree/master
<% end %>
<% content_for :index_section do %>
<div id="subCol">
<dl>
<dd class="warning">Rails Guides are a result of the ongoing "Guides hackfest":http://hackfest.rubyonrails.org and a work in progress.</dd>
<dd class="ticket">Guides marked with this icon are currently being worked on. While they might still be useful to you, they may contain incomplete information and even errors. You can help by reviewing them and posting your comments and corrections at the respective Lighthouse ticket.</dd>
</dl>
</div>
<% end %>
h3. Start Here
<dl>
<% guide('Getting Started with Rails', 'getting_started.html') do %>
Everything you need to know to install Rails and create your first application.
<% end %>
</dl>
h3. Models
<dl>
<% guide("Rails Database Migrations", 'migrations.html') do %>
This guide covers how you can use Active Record migrations to alter your database in a structured and organized manner.
<% end %>
<% guide("Active Record Validations and Callbacks", 'activerecord_validations_callbacks.html', :ticket => 26) do %>
This guide covers how you can use Active Record validations and callbacks.
<% end %>
<% guide("Active Record Associations", 'association_basics.html') do %>
This guide covers all the associations provided by Active Record.
<% end %>
<% guide("Active Record Query Interface", 'active_record_querying.html', :ticket => 16) do %>
This guide covers the database query interface provided by Active Record.
<% end %>
</dl>
h3. Views
<dl>
<% guide("Layouts and Rendering in Rails", 'layouts_and_rendering.html') do %>
This guide covers the basic layout features of Action Controller and Action View, including rendering and redirecting, using content_for blocks, and working with partials.
<% end %>
<% guide("Action View Form Helpers", 'form_helpers.html', :ticket => 1) do %>
Guide to using built in Form helpers.
<% end %>
</dl>
h3. Controllers
<dl>
<% guide("Action Controller Overview", 'action_controller_overview.html') do %>
This guide covers how controllers work and how they fit into the request cycle in your application. It includes sessions, filters, and cookies, data streaming, and dealing with exceptions raised by a request, among other topics.
<% end %>
<% guide("Rails Routing from the Outside In", 'routing.html') do %>
This guide covers the user-facing features of Rails routing. If you want to understand how to use routing in your own Rails applications, start here.
<% end %>
</dl>
h3. Digging Deeper
<dl>
<% guide("Rails Internationalization API", 'i18n.html', :ticket => 23) do %>
This guide covers how to build a plugin to extend the functionality of Rails.
<% end %>
<% guide("Action Mailer Basics", 'action_mailer_basics.html', :ticket => 25) do %>
This guide describes how to use Action Mailer to send and receive emails.
<% end %>
<% guide("Testing Rails Applications", 'testing.html', :ticket => 8) do %>
This is a rather comprehensive guide to doing both unit and functional tests in Rails. It covers everything from “What is a test?” to the testing APIs. Enjoy.
<% end %>
<% guide("Securing Rails Applications", 'security.html') do %>
This guide describes common security problems in web applications and how to avoid them with Rails.
<% end %>
<% guide("Debugging Rails Applications", 'debugging_rails_applications.html') do %>
This guide describes how to debug Rails applications. It covers the different ways of achieving this and how to understand what is happening "behind the scenes" of your code.
<% end %>
<% guide("Performance Testing Rails Applications", 'performance_testing.html') do %>
This guide covers the various ways of performance testing a Ruby on Rails application.
<% end %>
<% guide("The Basics of Creating Rails Plugins", 'plugins.html') do %>
This guide covers how to build a plugin to extend the functionality of Rails.
<% end %>
<% guide("Configuring Rails Applications", 'configuring.html') do %>
This guide covers the basic configuration settings for a Rails application.
<% end %>
<% guide("Rails Command Line Tools and Rake tasks", 'command_line.html') do %>
This guide covers the command line tools and rake tasks provided by Rails.
<% end %>
</dl>

View File

@ -0,0 +1,99 @@
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title><%= yield(:page_title) || 'Ruby on Rails guides' %></title>
<link rel="stylesheet" type="text/css" href="files/stylesheets/style.css" />
<link rel="stylesheet" type="text/css" href="files/stylesheets/syntax.css" />
<link rel="stylesheet" type="text/css" href="files/stylesheets/print.css" media="print" />
<script type="text/javascript" src="files/javascripts/guides.js"></script>
<script type="text/javascript" src="files/javascripts/code_highlighter.js"></script>
<script type="text/javascript" src="files/javascripts/highlighters.js"></script>
</head>
<body class="guide">
<div id="topNav">
<div class="wrapper">
<strong>More at <a href="http://www.rubyonrails.org/">rubyonrails.org:</a> </strong>
<a href="http://www.rubyonrails.org/">Overview</a> |
<a href="http://www.rubyonrails.org/download">Download</a> |
<a href="http://www.rubyonrails.org/deploy">Deploy</a> |
<a href="http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/overview">Code</a> |
<a href="http://www.rubyonrails.org/screencasts">Screencasts</a> |
<a href="http://www.rubyonrails.org/documentation">Documentation</a> |
<a href="http://www.rubyonrails.org/ecosystem">Ecosystem</a> |
<a href="http://www.rubyonrails.org/community">Community</a> |
<a href="http://weblog.rubyonrails.org/">Blog</a>
</div>
</div>
<div id="header">
<div class="wrapper clearfix">
<h1><a href="index.html" title="Return to home page">Guides.rubyonrails.org</a></h1>
<p class="hide"><a href="#mainCol">Skip navigation</a>.</p>
<ul class="nav">
<li><a href="index.html">Home</a></li>
<li class="index"><a href="#" onclick="guideMenu();" id="guidesMenu">Guides Index</a>
<div id="guides" class="clearfix" style="display: none;">
<hr />
<dl class="L">
<dt>Start Here</dt>
<dd><a href="getting_started.html">Getting Started with Rails</a></dd>
<dt>Models</dt>
<dd><a href="migrations.html">Rails Database Migrations</a></dd>
<dd><a href="activerecord_validations_callbacks.html">Active Record Validations and Callbacks</a></dd>
<dd><a href="association_basics.html">Active Record Associations</a></dd>
<dd><a href="active_record_querying.html">Active Record Query Interface</a></dd>
<dt>Views</dt>
<dd><a href="layouts_and_rendering.html">Layouts and Rendering in Rails</a></dd>
<dd><a href="form_helpers.html">Action View Form Helpers</a></dd>
<dt>Controllers</dt>
<dd><a href="action_controller_overview.html">Action Controller Overview</a></dd>
<dd><a href="routing.html">Rails Routing from the Outside In</a></dd>
</dl>
<dl class="R">
<dt>Digging Deeper</dt>
<dd><a href="i18n.html">Rails Internationalization API</a></dd>
<dd><a href="action_mailer_basics.html">Action Mailer Basics</a></dd>
<dd><a href="testing.html">Testing Rails Applications</a></dd>
<dd><a href="security.html">Securing Rails Applications</a></dd>
<dd><a href="debugging_rails_applications.html">Debugging Rails Applications</a></dd>
<dd><a href="performance_testing.html">Performance Testing Rails Applications</a></dd>
<dd><a href="plugins.html">The Basics of Creating Rails Plugins</a></dd>
<dd><a href="configuring.html">Configuring Rails Applications</a></dd>
</dl>
</div>
</li>
<li><a href="http://hackfest.rubyonrails.org">Contribute</a></li>
<li><a href="credits.html">Credits</a></li>
</ul>
</div>
</div>
<hr class="hide" />
<div id="feature">
<div class="wrapper">
<%= yield :header_section %>
<%= yield :index_section %>
</div>
</div>
<div id="container">
<div class="wrapper">
<div id="mainCol">
<%= yield %>
</div>
</div>
</div>
<hr class="hide" />
<div id="footer">
<div class="wrapper">
<p>Authors who have contributed to complete guides are listed <a href="credits.html">here</a>.<br />This work is licensed under a <a href="http://creativecommons.org/licenses/by-sa/3.0">Creative Commons Attribution-Share Alike 3.0</a> License</a></p>
<p>"Rails", "Ruby on Rails", and the Rails logo are trademarks of David Heinemeier Hansson. All rights reserved.</p>
</div>
</div>
</body>
</html>

View File

@ -0,0 +1,957 @@
h2. Layouts and Rendering in Rails
This guide covers the basic layout features of Action Controller and Action View. By referring to this guide, you will be able to:
* Use the various rendering methods built in to Rails
* Create layouts with multiple content sections
* Use partials to DRY up your views
* Use nested layouts (sub-templates)
endprologue.
h3. Overview: How the Pieces Fit Together
This guide focuses on the interaction between Controller and View in the Model-View-Controller triangle. As you know, the Controller is responsible for orchestrating the whole process of handling a request in Rails, though it normally hands off any heavy code to the Model. But then, when it's time to send a response back to the user, the Controller hands things off to the View. It's that handoff that is the subject of this guide.
In broad strokes, this involves deciding what should be sent as the response and calling an appropriate method to create that response. If the response is a full-blown view, Rails also does some extra work to wrap the view in a layout and possibly to pull in partial views. You'll see all of those paths later in this guide.
h3. Creating Responses
From the controller's point of view, there are three ways to create an HTTP response:
* Call +render+ to create a full response to send back to the browser
* Call +redirect_to+ to send an HTTP redirect status code to the browser
* Call +head+ to create a response consisting solely of HTTP headers to send back to the browser
I'll cover each of these methods in turn. But first, a few words about the very easiest thing that the controller can do to create a response: nothing at all.
h4. Rendering by Default: Convention Over Configuration in Action
You've heard that Rails promotes "convention over configuration." Default rendering is an excellent example of this. By default, controllers in Rails automatically render views with names that correspond to actions. For example, if you have this code in your +BooksController+ class:
<ruby>
def show
@book = Book.find(params[:id])
end
</ruby>
Rails will automatically render +app/views/books/show.html.erb+ after running the method. In fact, if you have the default catch-all route in place (+map.connect ':controller/:action/:id'+), Rails will even render views that don't have any code at all in the controller. For example, if you have the default route in place and a request comes in for +/books/sale_list+, Rails will render +app/views/books/sale_list.html.erb+ in response.
NOTE: The actual rendering is done by subclasses of +ActionView::TemplateHandlers+. This guide does not dig into that process, but it's important to know that the file extension on your view controls the choice of template handler. In Rails 2, the standard extensions are +.erb+ for ERB (HTML with embedded Ruby), +.rjs+ for RJS (javascript with embedded ruby) and +.builder+ for Builder (XML generator). You'll also find +.rhtml+ used for ERB templates and +.rxml+ for Builder templates, but those extensions are now formally deprecated and will be removed from a future version of Rails.
h4. Using render
In most cases, the +ActionController::Base#render+ method does the heavy lifting of rendering your application's content for use by a browser. There are a variety of ways to customize the behavior of +render+. You can render the default view for a Rails template, or a specific template, or a file, or inline code, or nothing at all. You can render text, JSON, or XML. You can specify the content type or HTTP status of the rendered response as well.
TIP: If you want to see the exact results of a call to +render+ without needing to inspect it in a browser, you can call +render_to_string+. This method takes exactly the same options as +render+, but it returns a string instead of sending a response back to the browser.
h5. Rendering Nothing
Perhaps the simplest thing you can do with +render+ is to render nothing at all:
<ruby>
render :nothing => true
</ruby>
This will send an empty response to the browser (though it will include any status headers you set with the :status option, discussed below).
TIP: You should probably be using the +head+ method, discussed later in this guide, instead of +render :nothing+. This provides additional flexibility and makes it explicit that you're only generating HTTP headers.
h5. Rendering an Action's View
If you want to render the view that corresponds to a different action within the same template, you can use +render+ with the name of the view:
<ruby>
def update
@book = Book.find(params[:id])
if @book.update_attributes(params[:book])
redirect_to(@book)
else
render "edit"
end
end
end
</ruby>
If the call to +update_attributes+ fails, calling the +update+ action in this controller will render the +edit.html.erb+ template belonging to the same controller.
If you prefer, you can use a symbol instead of a string to specify the action to render:
<ruby>
def update
@book = Book.find(params[:id])
if @book.update_attributes(params[:book])
redirect_to(@book)
else
render :edit
end
end
end
</ruby>
To be explicit, you can use +render+ with the +:action+ option (though this is no longer necessary as of Rails 2.3):
<ruby>
def update
@book = Book.find(params[:id])
if @book.update_attributes(params[:book])
redirect_to(@book)
else
render :action => "edit"
end
end
end
</ruby>
WARNING: Using +render+ with +:action+ is a frequent source of confusion for Rails newcomers. The specified action is used to determine which view to render, but Rails does _not_ run any of the code for that action in the controller. Any instance variables that you require in the view must be set up in the current action before calling +render+.
h5. Rendering an Action's Template from Another Controller
What if you want to render a template from an entirely different controller from the one that contains the action code? You can also do that with +render+, which accepts the full path (relative to +app/views+) of the template to render. For example, if you're running code in an +AdminProductsController+ that lives in +app/controllers/admin+, you can render the results of an action to a template in +app/views/products+ this way:
<ruby>
render 'products/show'
</ruby>
Rails knows that this view belongs to a different controller because of the embedded slash character in the string. If you want to be explicit, you can use the +:template+ option (which was required on Rails 2.2 and earlier):
<ruby>
render :template => 'products/show'
</ruby>
h5. Rendering an Arbitrary File
The +render+ method can also use a view that's entirely outside of your application (perhaps you're sharing views between two Rails applications):
<ruby>
render "/u/apps/warehouse_app/current/app/views/products/show"
</ruby>
Rails determines that this is a file render because of the leading slash character. To be explicit, you can use the +:file+ option (which was required on Rails 2.2 and earlier):
<ruby>
render :file => "/u/apps/warehouse_app/current/app/views/products/show"
</ruby>
The +:file+ option takes an absolute file-system path. Of course, you need to have rights to the view that you're using to render the content.
NOTE: By default, the file is rendered without using the current layout. If you want Rails to put the file into the current layout, you need to add the +:layout => true+ option.
TIP: If you're running on Microsoft Windows, you should use the +:file+ option to render a file, because Windows filenames do not have the same format as Unix filenames.
h5. Using render with :inline
The +render+ method can do without a view completely, if you're willing to use the +:inline+ option to supply ERB as part of the method call. This is perfectly valid:
<ruby>
render :inline => "<% products.each do |p| %><p><%= p.name %><p><% end %>"
</ruby>
WARNING: There is seldom any good reason to use this option. Mixing ERB into your controllers defeats the MVC orientation of Rails and will make it harder for other developers to follow the logic of your project. Use a separate erb view instead.
By default, inline rendering uses ERb. You can force it to use Builder instead with the +:type+ option:
<ruby>
render :inline => "xml.p {'Horrid coding practice!'}", :type => :builder
</ruby>
h5. Using render with :update
You can also render javascript-based page updates inline using the +:update+ option to +render+:
<ruby>
render :update do |page|
page.replace_html 'warning', "Invalid options supplied"
end
</ruby>
WARNING: Placing javascript updates in your controller may seem to streamline small updates, but it defeats the MVC orientation of Rails and will make it harder for other developers to follow the logic of your project. I recommend using a separate rjs template instead, no matter how small the update.
h5. Rendering Text
You can send plain text - with no markup at all - back to the browser by using the +:text+ option to +render+:
<ruby>
render :text => "OK"
</ruby>
TIP: Rendering pure text is most useful when you're responding to AJAX or web service requests that are expecting something other than proper HTML.
NOTE: By default, if you use the +:text+ option, the file is rendered without using the current layout. If you want Rails to put the text into the current layout, you need to add the +:layout => true+ option
h5. Rendering JSON
JSON is a javascript data format used by many AJAX libraries. Rails has built-in support for converting objects to JSON and rendering that JSON back to the browser:
<ruby>
render :json => @product
</ruby>
TIP: You don't need to call +to_json+ on the object that you want to render. If you use the +:json+ option, +render+ will automatically call +to_json+ for you.
h5. Rendering XML
Rails also has built-in support for converting objects to XML and rendering that XML back to the caller:
<ruby>
render :xml => @product
</ruby>
TIP: You don't need to call +to_xml+ on the object that you want to render. If you use the +:xml+ option, +render+ will automatically call +to_xml+ for you.
h5. Rendering Vanilla JavaScript
Rails can render vanilla JavaScript (as an alternative to using +update+ with n +.rjs+ file):
<ruby>
render :js => "alert('Hello Rails');"
</ruby>
This will send the supplied string to the browser with a MIME type of +text/javascript+.
h5. Options for render
Calls to the +render+ method generally accept four options:
* +:content_type+
* +:layout+
* +:status+
* +:location+
h6. The :content_type Option
By default, Rails will serve the results of a rendering operation with the MIME content-type of +text/html+ (or +application/json+ if you use the +:json+ option, or +application/xml+ for the +:xml+ option.). There are times when you might like to change this, and you can do so by setting the +:content_type+ option:
<ruby>
render :file => filename, :content_type => 'application/rss'
</ruby>
h6. The :layout Option
With most of the options to +render+, the rendered content is displayed as part of the current layout. You'll learn more about layouts and how to use them later in this guide.
You can use the +:layout+ option to tell Rails to use a specific file as the layout for the current action:
<ruby>
render :layout => 'special_layout'
</ruby>
You can also tell Rails to render with no layout at all:
<ruby>
render :layout => false
</ruby>
h6. The :status Option
Rails will automatically generate a response with the correct HTML status code (in most cases, this is +200 OK+). You can use the +:status+ option to change this:
<ruby>
render :status => 500
render :status => :forbidden
</ruby>
Rails understands either numeric status codes or symbols for status codes. You can find its list of status codes in +actionpack/lib/action_controller/status_codes.rb+. You can also see there how it maps symbols to status codes in that file.
h6. The :location Option
You can use the +:location+ option to set the HTTP +Location+ header:
<ruby>
render :xml => photo, :location => photo_url(photo)
</ruby>
h5. Finding Layouts
To find the current layout, Rails first looks for a file in +app/views/layouts+ with the same base name as the controller. For example, rendering actions from the +PhotosController+ class will use +/app/views/layouts/photos.html.erb+. If there is no such controller-specific layout, Rails will use +/app/views/layouts/application.html.erb+. If there is no +.erb+ layout, Rails will use a +.builder+ layout if one exists. Rails also provides several ways to more precisely assign specific layouts to individual controllers and actions.
h6. Specifying Layouts on a per-Controller Basis
You can override the automatic layout conventions in your controllers by using the +layout+ declaration in the controller. For example:
<ruby>
class ProductsController < ApplicationController
layout "inventory"
#...
end
</ruby>
With this declaration, all methods within +ProductsController+ will use +app/views/layouts/inventory.html.erb+ for their layout.
To assign a specific layout for the entire application, use a declaration in your +ApplicationController+ class:
<ruby>
class ApplicationController < ActionController::Base
layout "main"
#...
end
</ruby>
With this declaration, all views in the entire application will use +app/views/layouts/main.html.erb+ for their layout.
h6. Choosing Layouts at Runtime
You can use a symbol to defer the choice of layout until a request is processed:
<ruby>
class ProductsController < ApplicationController
layout :products_layout
def show
@product = Product.find(params[:id])
end
private
def products_layout
@current_user.special? ? "special" : "products"
end
end
</ruby>
Now, if the current user is a special user, they'll get a special layout when viewing a product. You can even use an inline method to determine the layout:
<ruby>
class ProductsController < ApplicationController
layout proc{ |controller| controller.
# ...
end
</ruby>
h6. Conditional Layouts
Layouts specified at the controller level support +:only+ and +:except+ options that take either a method name or an array of method names:
<ruby>
class ProductsController < ApplicationController
layout "inventory", :only => :index
layout "product", :except => [:index, :rss]
#...
end
</ruby>
With those declarations, the +inventory+ layout would be used only for the +index+ method, the +product+ layout would be used for everything else except the +rss+ method, and the +rss+ method will have its layout determined by the automatic layout rules.
h6. Layout Inheritance
Layouts are shared downwards in the hierarchy, and more specific layouts always override more general ones. For example:
* +application_controller.rb+
<ruby>
class ApplicationController < ActionController::Base
layout "main"
#...
end
</ruby>
* +posts_controller.rb+
<ruby>
class PostsController < ApplicationController
# ...
end
</ruby>
* +special_posts_controller.rb+
<ruby>
class SpecialPostsController < PostsController
layout "special"
# ...
end
</ruby>
* +old_posts_controller.rb+
<ruby>
class OldPostsController < SpecialPostsController
layout nil
def show
@post = Post.find(params[:id])
end
def index
@old_posts = Post.older
render :layout => "old"
end
# ...
end
</ruby>
In this application:
* In general, views will be rendered in the +main+ layout
* +PostsController#index+ will use the +main+ layout
* +SpecialPostsController#index+ will use the +special+ layout
* +OldPostsController#show+ will use no layout at all
* +OldPostsController#index+ will use the +old+ layout
h5. Avoiding Double Render Errors
Sooner or later, most Rails developers will see the error message "Can only render or redirect once per action". While this is annoying, it's relatively easy to fix. Usually it happens because of a fundamental misunderstanding of the way that +render+ works.
For example, here's some code that will trigger this error:
<ruby>
def show
@book = Book.find(params[:id])
if @book.special?
render :action => "special_show"
end
end
</ruby>
If +@book.special?+ evaluates to +true+, Rails will start the rendering process to dump the +@book+ variable into the +special_show+ view. But this will _not_ stop the rest of the code in the +show+ action from running, and when Rails hits the end of the action, it will start to render the +show+ view - and throw an error. The solution is simple: make sure that you only have one call to +render+ or +redirect+ in a single code path. One thing that can help is +and return+. Here's a patched version of the method:
<ruby>
def show
@book = Book.find(params[:id])
if @book.special?
render :action => "special_show" and return
end
end
</ruby>
h4. Using redirect_to
Another way to handle returning responses to a HTTP request is with +redirect_to+. As you've seen, +render+ tells Rails which view (or other asset) to use in constructing a response. The +redirect_to+ method does something completely different: it tells the browser to send a new request for a different URL. For example, you could redirect from wherever you are in your code to the index of photos in your application with this call:
<ruby>
redirect_to photos_path
</ruby>
You can use +redirect_to+ with any arguments that you could use with +link_to+ or +url_for+. In addition, there's a special redirect that sends the user back to the page they just came from:
<ruby>
redirect_to :back
</ruby>
h5. Getting a Different Redirect Status Code
Rails uses HTTP status code 302 (permanent redirect) when you call +redirect_to+. If you'd like to use a different status code (perhaps 301, temporary redirect), you can do so by using the +:status+ option:
<ruby>
redirect_to photos_path, :status => 301
</ruby>
Just like the +:status+ option for +render+, +:status+ for +redirect_to+ accepts both numeric and symbolic header designations.
h5. The Difference Between render and redirect
Sometimes inexperienced developers conceive of +redirect_to+ as a sort of +goto+ command, moving execution from one place to another in your Rails code. This is _not_ correct. Your code stops running and waits for a new request for the browser. It just happens that you've told the browser what request it should make next, by sending back a HTTP 302 status code.
Consider these actions to see the difference:
<ruby>
def index
@books = Book.find(:all)
end
def show
@book = Book.find(params[:id])
if @book.nil?
render :action => "index" and return
end
end
</ruby>
With the code in this form, there will be likely be a problem if the +@book+ variable is +nil+. Remember, a +render :action+ doesn't run any code in the target action, so nothing will set up the +@books+ variable that the +index+ view is presumably depending on. One way to fix this is to redirect instead of rendering:
<ruby>
def index
@books = Book.find(:all)
end
def show
@book = Book.find(params[:id])
if @book.nil?
redirect_to :action => "index" and return
end
end
</ruby>
With this code, the browser will make a fresh request for the index page, the code in the +index+ method will run, and all will be well.
h4. Using head To Build Header-Only Responses
The +head+ method exists to let you send back responses to the browser that have only headers. It provides a more obvious alternative to calling +render :nothing+. The +head+ method takes one response, which is interpreted as a hash of header names and values. For example, you can return only an error header:
<ruby>
head :bad_request
</ruby>
Or you can use other HTTP headers to convey additional information:
<ruby>
head :created, :location => photo_path(@photo)
</ruby>
h3. Structuring Layouts
When Rails renders a view as a response, it does so by combining the view with the current layout (using the rules for finding the current layout that were covered earlier in this guide). Within a layout, you have access to three tools for combining different bits of output to form the overall response:
* Asset tags
* +yield+ and +content_for+
* Partials
I'll discuss each of these in turn.
h4. Asset Tags
Asset tags provide methods for generating HTML that links views to assets like images, javascript, stylesheets, and feeds. There are four types of include tag:
* auto_discovery_link_tag
* javascript_include_tag
* stylesheet_link_tag
* image_tag
You can use these tags in layouts or other views, although the tags other than +image_tag+ are most commonly used in the +<head>+ section of a layout.
WARNING: The asset tags do _not_ verify the existence of the assets at the specified locations; they simply assume that you know what you're doing and generate the link.
h5. Linking to Feeds with auto_discovery_link_tag
The +auto_discovery_link_tag helper builds HTML that most browsers and newsreaders can use to detect the presences of RSS or ATOM feeds. It takes the type of the link (+:rss+ or +:atom+), a hash of options that are passed through to url_for, and a hash of options for the tag:
<ruby>
<%= auto_discovery_link_tag(:rss, {:action => "feed"}, {:title => "RSS Feed"}) %>
</ruby>
There are three tag options available for +auto_discovery_link_tag+:
* +:rel+ specifies the +rel+ value in the link (defaults to "alternate")
* +:type+ specifies an explicit MIME type. Rails will generate an appropriate MIME type automatically
* +:title+ specifies the title of the link
h5. Linking to Javascript Files with javascript_include_tag
The +javascript_include_tag+ helper returns an HTML +script+ tag for each source provided. Rails looks in +public/javascripts+ for these files by default, but you can specify a full path relative to the document root, or a URL, if you prefer. For example, to include +public/javascripts/main.js+:
<ruby>
<%= javascript_include_tag "main" %>
</ruby>
To include +public/javascripts/main.js+ and +public/javascripts/columns.js+:
<ruby>
<%= javascript_include_tag "main", "columns" %>
</ruby>
To include +public/javascripts/main.js+ and +public/photos/columns.js+:
<ruby>
<%= javascript_include_tag "main", "/photos/columns" %>
</ruby>
To include +http://example.com/main.js+:
<ruby>
<%= javascript_include_tag "http://example.com/main.js" %>
</ruby>
The +defaults+ option loads the Prototype and Scriptaculous libraries:
<ruby>
<%= javascript_include_tag :defaults %>
</ruby>
The +all+ option loads every javascript file in +public/javascripts+, starting with the Prototype and Scriptaculous libraries:
<ruby>
<%= javascript_include_tag :all %>
</ruby>
You can supply the +:recursive+ option to load files in subfolders of +public/javascripts+ as well:
<ruby>
<%= javascript_include_tag :all, :recursive => true %>
</ruby>
If you're loading multiple javascript files, you can create a better user experience by combining multiple files into a single download. To make this happen in production, specify +:cache => true+ in your +javascript_include_tag+:
<ruby>
<%= javascript_include_tag "main", "columns", :cache => true %>
</ruby>
By default, the combined file will be delivered as +javascripts/all.js+. You can specify a location for the cached asset file instead:
<ruby>
<%= javascript_include_tag "main", "columns", :cache => 'cache/main/display' %>
</ruby>
You can even use dynamic paths such as "cache/#{current_site}/main/display"+.
h5. Linking to CSS Files with stylesheet_link_tag
The +stylesheet_link_tag+ helper returns an HTML +<link>+ tag for each source provided. Rails looks in +public/stylesheets+ for these files by default, but you can specify a full path relative to the document root, or a URL, if you prefer. For example, to include +public/stylesheets/main.cs+:
<ruby>
<%= stylesheet_link_tag "main" %>
</ruby>
To include +public/stylesheets/main.css+ and +public/stylesheets/columns.css+:
<ruby>
<%= stylesheet_link_tag "main", "columns" %>
</ruby>
To include +public/stylesheets/main.css+ and +public/photos/columns.css+:
<ruby>
<%= stylesheet_link_tag "main", "/photos/columns" %>
</ruby>
To include +http://example.com/main.cs+:
<ruby>
<%= stylesheet_link_tag "http://example.com/main.cs" %>
</ruby>
By default, +stylesheet_link_tag+ creates links with +media="screen" rel="stylesheet" type="text/css"+. You can override any of these defaults by specifying an appropriate option (:media, :rel, or :type):
<ruby>
<%= stylesheet_link_tag "main_print", media => "print" %>
</ruby>
The +all+ option links every CSS file in +public/stylesheets+:
<ruby>
<%= stylesheet_link_tag :all %>
</ruby>
You can supply the +:recursive+ option to link files in subfolders of +public/stylesheets+ as well:
<ruby>
<%= stylesheet_link_tag :all, :recursive => true %>
</ruby>
If you're loading multiple CSS files, you can create a better user experience by combining multiple files into a single download. To make this happen in production, specify +:cache => true+ in your +stylesheet_link_tag+:
<ruby>
<%= stylesheet_link_tag "main", "columns", :cache => true %>
</ruby>
By default, the combined file will be delivered as +stylesheets/all.css+. You can specify a location for the cached asset file instead:
<ruby>
<%= stylesheet_link_tag "main", "columns", :cache => 'cache/main/display' %>
</ruby>
You can even use dynamic paths such as "cache/#{current_site}/main/display"+.
h5. Linking to Images with image_tag
The +image_tag+ helper builds an HTML +<image>+ tag to the specified file. By default, files are loaded from +public/images+. If you don't specify an extension, .png is assumed by default:
<ruby>
<%= image_tag "header" %>
</ruby>
You can supply a path to the image if you like:
<ruby>
<%= image_tag "icons/delete.gif" %>
</ruby>
You can supply a hash of additional HTML options:
<ruby>
<%= image_tag "icons/delete.gif", :height => 45 %>
</ruby>
There are also three special options you can use with +image_tag+:
* +:alt+ specifies the alt text for the image (which defaults to the file name of the file, capitalized and with no extension)
* +:size+ specifies both width and height, in the format "{width}x{height}" (for example, "150x125")
* +:mouseover+ sets an alternate image to be used when the onmouseover event is fired.
h4. Understanding yield
Within the context of a layout, +yield+ identifies a section where content from the view should be inserted. The simplest way to use this is to have a single +yield+, into which the entire contents of the view currently being rendered is inserted:
<erb>
<html>
<head>
</head>
<body>
<%= yield %>
<hbody>
</html>
</erb>
You can also create a layout with multiple yielding regions:
<erb>
<html>
<head>
<%= yield :head %>
</head>
<body>
<%= yield %>
<hbody>
</html>
</erb>
The main body of the view will always render into the unnamed +yield+. To render content into a named +yield+, you use the +content_for+ method.
h4. Using content_for
The +content_for+ method allows you to insert content into a +yield+ block in your layout. You only use +content_for+ to insert content in named yields. For example, this view would work with the layout that you just saw:
<erb>
<% content_for :head do %>
<title>A simple page</title>
<% end %>
<p>Hello, Rails!</p>
</erb>
The result of rendering this page into the supplied layout would be this HTML:
<erb>
<html>
<head>
<title>A simple page</title>
</head>
<body>
<p>Hello, Rails!</p>
<hbody>
</html>
</erb>
The +content_for+ method is very helpful when your layout contains distinct regions such as sidebars and footers that should get their own blocks of content inserted. It's also useful for inserting tags that load page-specific javascript or css files into the header of an otherwise-generic layout.
h4. Using Partials
Partial templates - usually just called "partials" - are another device for breaking apart the rendering process into more manageable chunks. With a partial, you can move the code for rendering a particular piece of a response to its own file.
h5. Naming Partials
To render a partial as part of a view, you use the +render+ method within the view, and include the +:partial+ option:
<ruby>
<%= render :partial => "menu" %>
</ruby>
This will render a file named +_menu.html.erb+ at that point within the view being rendered. Note the leading underscore character: partials are named with a leading underscore to distinguish them from regular views, even though they are referred to without the underscore. This holds true even when you're pulling in a partial from another folder:
<ruby>
<%= render :partial => "shared/menu" %>
</ruby>
That code will pull in the partial from +app/views/shared/_menu.html.erb+.
h5. Using Partials to Simplify Views
One way to use partials is to treat them as the equivalent of subroutines: as a way to move details out of a view so that you can grasp what's going on more easily. For example, you might have a view that looked like this:
<erb>
<%= render :partial => "shared/ad_banner" %>
<h1>Products</h1>
<p>Here are a few of our fine products:</p>
...
<%= render :partial => "shared/footer" %>
</erb>
Here, the +_ad_banner.html.erb+ and +_footer.html.erb+ partials could contain content that is shared among many pages in your application. You don't need to see the details of these sections when you're concentrating on a particular page.
TIP: For content that is shared among all pages in your application, you can use partials directly from layouts.
h5. Partial Layouts
A partial can use its own layout file, just as a view can use a layout. For example, you might call a partial like this:
<erb>
<%= render :partial => "link_area", :layout => "graybar" %>
</erb>
This would look for a partial named +_link_area.html.erb+ and render it using the layout +_graybar.html.erb+. Note that layouts for partials follow the same leading-underscore naming as regular partials, and are placed in the same folder with the partial that they belong to (not in the master +layouts+ folder).
h5. Passing Local Variables
You can also pass local variables into partials, making them even more powerful and flexible. For example, you can use this technique to reduce duplication between new and edit pages, while still keeping a bit of distinct content:
* +new.html.erb+
<erb>
<h1>New zone</h1>
<%= error_messages_for :zone %>
<%= render :partial => "form", :locals => { :button_label => "Create zone", :zone => @zone } %>
</erb>
* +edit.html.erb+
<erb>
<h1>Editing zone</h1>
<%= error_messages_for :zone %>
<%= render :partial => "form", :locals => { :button_label => "Update zone", :zone => @zone } %>
</erb>
* +_form.html.erb+
<erb>
<% form_for(zone) do |f| %>
<p>
<b>Zone name</b><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.submit button_label %>
</p>
<% end %>
</erb>
Although the same partial will be rendered into both views, the label on the submit button is controlled by a local variable passed into the partial.
Every partial also has a local variable with the same name as the partial (minus the underscore). You can pass an object in to this local variable via the +:object+ option:
<erb>
<%= render :partial => "customer", :object => @new_customer %>
</erb>
Within the +customer+ partial, the +@customer+ variable will refer to +@new_customer+ from the parent view.
WARNING: In previous versions of Rails, the default local variable would look for an instance variable with the same name as the partial in the parent. This behavior is deprecated in Rails 2.2 and will be removed in a future version.
If you have an instance of a model to render into a partial, you can use a shorthand syntax:
<erb>
<%= render :partial => @customer %>
</erb>
Assuming that the +@customer+ instance variable contains an instance of the +Customer+ model, this will use +_customer.html.erb+ to render it.
h5. Rendering Collections
Partials are very useful in rendering collections. When you pass a collection to a partial via the +:collection+ option, the partial will be inserted once for each member in the collection:
* +index.html.erb+
<erb>
<h1>Products</h1>
<%= render :partial => "product", :collection => @products %>
</erb>
* +_product.html.erb+
<erb>
<p>Product Name: <%= product.name %></p>
</erb>
When a partial is called with a pluralized collection, then the individual instances of the partial have access to the member of the collection being rendered via a variable named after the partial. In this case, the partial is +_product+, and within the +_product+ partial, you can refer to +product+ to get the instance that is being rendered. To use a custom local variable name within the partial, specify the +:as+ option in the call to the partial:
<erb>
<%= render :partial => "product", :collection => @products, :as => :item %>
</erb>
With this change, you can access an instance of the +@products+ collection as the +item+ local variable within the partial.
TIP: Rails also makes a counter variable available within a partial called by the collection, named after the member of the collection followed by +_counter+. For example, if you're rendering +@products+, within the partial you can refer to +product_counter+ to tell you how many times the partial has been rendered.
You can also specify a second partial to be rendered between instances of the main partial by using the +:spacer_template+ option:
<erb>
<%= render :partial => "product", :collection => @products, :spacer_template => "product_ruler" %>
</erb>
Rails will render the +_product_ruler+ partial (with no data passed in to it) between each pair of +_product+ partials.
There's also a shorthand syntax available for rendering collections. For example, if +@products+ is a collection of products, you can render the collection this way:
* +index.html.erb+
<erb>
<h1>Products</h1>
<%= render :partial => @products %>
</erb>
* +_product.html.erb+
<erb>
<p>Product Name: <%= product.name %></p>
</erb>
Rails determines the name of the partial to use by looking at the model name in the collection. In fact, you can even create a heterogeneous collection and render it this way, and Rails will choose the proper partial for each member of the collection:
* +index.html.erb+
<erb>
<h1>Contacts</h1>
<%= render :partial => [customer1, employee1, customer2, employee2] %>
</erb>
* +_customer.html.erb+
<erb>
<p>Name: <%= customer.name %></p>
</erb>
* +_employee.html.erb+
<erb>
<p>Name: <%= employee.name %></p>
</erb>
In this case, Rails will use the customer or employee partials as appropriate for each member of the collection.
h4. Using Nested Layouts
You may find that your application requires a layout that differs slightly from your regular application layout to support one particular controller. Rather than repeating the main layout and editing it, you can accomplish this by using nested layouts (sometimes called sub-templates). Here's an example:
Suppose you have the follow +ApplicationController+ layout:
* +app/views/layouts/application.erb+
<erb>
<html>
<head>
<title><%= @page_title %><title>
<% stylesheet_tag 'layout' %>
<style type="text/css"><%= yield :stylesheets %></style>
<head>
<body>
<div id="top_menu">Top menu items here</div>
<div id="menu">Menu items here</div>
<div id="main"><%= yield %></div>
</body>
</html>
</erb>
On pages generated by +NewsController+, you want to hide the top menu and add a right menu:
* +app/views/layouts/news.erb+
<erb>
<% content_for :stylesheets do %>
#top_menu {display: none}
#right_menu {float: right; background-color: yellow; color: black}
<% end -%>
<% content_for :main %>
<div id="right_menu">Right menu items here</div>
<%= yield %>
<% end -%>
<% render :file => 'layouts/application' %>
</erb>
NOTE: In versions of Rails before Rails 2.3, you should use +render 'layouts/applications'+ instead of +render :file => 'layouts/applications'+
That's it. The News views will use the new layout, hiding the top menu and adding a new right menu inside the "content" div.
There are several ways of getting similar results with differents sub-templating schemes using this technique. Note that there is no limit in nesting levels. One can use the +ActionView::render+ method via +render 'layouts/news'+ to base a new layout on the News layout.
h3. Changelog
"Lighthouse ticket":http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/15
* December 27, 2008: Merge patch from Rodrigo Rosenfeld Rosas covering subtemplates
* December 27, 2008: Information on new rendering defaults by "Mike Gunderloy":credits.html#mgunderloy
* November 9, 2008: Added partial collection counter by "Mike Gunderloy":credits.html#mgunderloy
* November 1, 2008: Added +:js+ option for +render+ by "Mike Gunderloy":credits.html#mgunderloy
* October 16, 2008: Ready for publication by "Mike Gunderloy":credits.html#mgunderloy
* October 4, 2008: Additional info on partials (+:object+, +:as+, and +:spacer_template+) by "Mike Gunderloy":credits.html#mgunderloy (not yet approved for publication)
* September 28, 2008: First draft by "Mike Gunderloy":credits.html#mgunderloy (not yet approved for publication)

Some files were not shown because too many files have changed in this diff Show More