railties 3.0.10 → 3.0.11
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +12 -1
- data/guides/output/2_2_release_notes.html +565 -0
- data/guides/output/2_3_release_notes.html +713 -0
- data/guides/output/3_0_release_notes.html +652 -0
- data/guides/output/3_1_release_notes.html +670 -0
- data/guides/output/action_controller_overview.html +925 -0
- data/guides/output/action_mailer_basics.html +658 -0
- data/guides/output/action_view_overview.html +1471 -0
- data/guides/output/active_model_basics.html +349 -0
- data/guides/output/active_record_basics.html +364 -0
- data/guides/output/active_record_querying.html +1272 -0
- data/guides/output/active_record_validations_callbacks.html +1292 -0
- data/guides/output/active_resource_basics.html +252 -0
- data/guides/output/active_support_core_extensions.html +3374 -0
- data/guides/output/ajax_on_rails.html +412 -0
- data/guides/output/api_documentation_guidelines.html +317 -0
- data/guides/output/asset_pipeline.html +691 -0
- data/guides/output/association_basics.html +1742 -0
- data/guides/output/caching_with_rails.html +533 -0
- data/guides/output/command_line.html +662 -0
- data/guides/output/configuring.html +811 -0
- data/guides/output/contribute.html +216 -0
- data/guides/output/contributing_to_ruby_on_rails.html +465 -0
- data/guides/output/credits.html +210 -0
- data/guides/output/debugging_rails_applications.html +791 -0
- data/guides/output/engines.html +673 -0
- data/guides/output/form_helpers.html +850 -0
- data/guides/output/generators.html +725 -0
- data/guides/output/getting_started.html +1980 -0
- data/guides/output/i18n.html +1054 -0
- data/guides/output/images/belongs_to.png +0 -0
- data/guides/output/images/book_icon.gif +0 -0
- data/guides/output/images/bullet.gif +0 -0
- data/guides/output/images/challenge.png +0 -0
- data/guides/output/images/chapters_icon.gif +0 -0
- data/guides/output/images/check_bullet.gif +0 -0
- data/guides/output/images/credits_pic_blank.gif +0 -0
- data/guides/output/images/csrf.png +0 -0
- data/guides/output/images/customized_error_messages.png +0 -0
- data/guides/output/images/edge_badge.png +0 -0
- data/guides/output/images/error_messages.png +0 -0
- data/guides/output/images/feature_tile.gif +0 -0
- data/guides/output/images/footer_tile.gif +0 -0
- data/guides/output/images/fxn.png +0 -0
- data/guides/output/images/grey_bullet.gif +0 -0
- data/guides/output/images/habtm.png +0 -0
- data/guides/output/images/has_many.png +0 -0
- data/guides/output/images/has_many_through.png +0 -0
- data/guides/output/images/has_one.png +0 -0
- data/guides/output/images/has_one_through.png +0 -0
- data/guides/output/images/header_backdrop.png +0 -0
- data/guides/output/images/header_tile.gif +0 -0
- data/guides/output/images/i18n/demo_html_safe.png +0 -0
- data/guides/output/images/i18n/demo_localized_pirate.png +0 -0
- data/guides/output/images/i18n/demo_translated_en.png +0 -0
- data/guides/output/images/i18n/demo_translated_pirate.png +0 -0
- data/guides/output/images/i18n/demo_translation_missing.png +0 -0
- data/guides/output/images/i18n/demo_untranslated.png +0 -0
- data/guides/output/images/icons/README +5 -0
- data/guides/output/images/icons/callouts/1.png +0 -0
- data/guides/output/images/icons/callouts/10.png +0 -0
- data/guides/output/images/icons/callouts/11.png +0 -0
- data/guides/output/images/icons/callouts/12.png +0 -0
- data/guides/output/images/icons/callouts/13.png +0 -0
- data/guides/output/images/icons/callouts/14.png +0 -0
- data/guides/output/images/icons/callouts/15.png +0 -0
- data/guides/output/images/icons/callouts/2.png +0 -0
- data/guides/output/images/icons/callouts/3.png +0 -0
- data/guides/output/images/icons/callouts/4.png +0 -0
- data/guides/output/images/icons/callouts/5.png +0 -0
- data/guides/output/images/icons/callouts/6.png +0 -0
- data/guides/output/images/icons/callouts/7.png +0 -0
- data/guides/output/images/icons/callouts/8.png +0 -0
- data/guides/output/images/icons/callouts/9.png +0 -0
- data/guides/output/images/icons/caution.png +0 -0
- data/guides/output/images/icons/example.png +0 -0
- data/guides/output/images/icons/home.png +0 -0
- data/guides/output/images/icons/important.png +0 -0
- data/guides/output/images/icons/next.png +0 -0
- data/guides/output/images/icons/note.png +0 -0
- data/guides/output/images/icons/prev.png +0 -0
- data/guides/output/images/icons/tip.png +0 -0
- data/guides/output/images/icons/up.png +0 -0
- data/guides/output/images/icons/warning.png +0 -0
- data/guides/output/images/jaimeiniesta.jpg +0 -0
- data/guides/output/images/nav_arrow.gif +0 -0
- data/guides/output/images/polymorphic.png +0 -0
- data/guides/output/images/posts_index.png +0 -0
- data/guides/output/images/radar.png +0 -0
- data/guides/output/images/rails_guides_logo.gif +0 -0
- data/guides/output/images/rails_logo_remix.gif +0 -0
- data/guides/output/images/rails_welcome.png +0 -0
- data/guides/output/images/session_fixation.png +0 -0
- data/guides/output/images/tab_grey.gif +0 -0
- data/guides/output/images/tab_info.gif +0 -0
- data/guides/output/images/tab_note.gif +0 -0
- data/guides/output/images/tab_red.gif +0 -0
- data/guides/output/images/tab_yellow.gif +0 -0
- data/guides/output/images/tab_yellow.png +0 -0
- data/guides/output/images/validation_error_messages.png +0 -0
- data/guides/output/images/vijaydev.jpg +0 -0
- data/guides/output/index.html +300 -0
- data/guides/output/initialization.html +1087 -0
- data/guides/output/javascripts/guides.js +7 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushAS3.js +59 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushAppleScript.js +75 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushBash.js +59 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushCSharp.js +65 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushColdFusion.js +100 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushCpp.js +97 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushCss.js +91 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushDelphi.js +55 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushDiff.js +41 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushErlang.js +52 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushGroovy.js +67 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushJScript.js +52 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushJava.js +57 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushJavaFX.js +58 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushPerl.js +72 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushPhp.js +88 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushPlain.js +33 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushPowerShell.js +74 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushPython.js +64 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushRuby.js +55 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushSass.js +94 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushScala.js +51 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushSql.js +66 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushVb.js +56 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushXml.js +69 -0
- data/guides/output/javascripts/syntaxhighlighter/shCore.js +17 -0
- data/guides/output/layout.html +312 -0
- data/guides/output/layouts_and_rendering.html +1257 -0
- data/guides/output/migrations.html +751 -0
- data/guides/output/nested_model_forms.html +350 -0
- data/guides/output/performance_testing.html +858 -0
- data/guides/output/plugins.html +590 -0
- data/guides/output/rails_application_templates.html +368 -0
- data/guides/output/rails_on_rack.html +408 -0
- data/guides/output/routing.html +1246 -0
- data/guides/output/ruby_on_rails_guides_guidelines.html +218 -0
- data/guides/output/security.html +968 -0
- data/guides/output/stylesheets/fixes.css +16 -0
- data/guides/output/stylesheets/main.css +445 -0
- data/guides/output/stylesheets/print.css +52 -0
- data/guides/output/stylesheets/reset.css +43 -0
- data/guides/output/stylesheets/style.css +13 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCore.css +226 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCoreDefault.css +328 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCoreDjango.css +331 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCoreEclipse.css +339 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCoreEmacs.css +324 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCoreFadeToGrey.css +328 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCoreMDUltra.css +324 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCoreMidnight.css +324 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCoreRDark.css +324 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeDefault.css +117 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeDjango.css +120 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeEclipse.css +128 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeEmacs.css +113 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeFadeToGrey.css +117 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeMDUltra.css +113 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeMidnight.css +113 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeRDark.css +113 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeRailsGuides.css +116 -0
- data/guides/output/testing.html +1182 -0
- data/lib/rails/generators/rails/app/app_generator.rb +1 -1
- data/lib/rails/generators/rails/app/templates/public/javascripts/rails.js +86 -75
- data/lib/rails/tasks/documentation.rake +1 -1
- data/lib/rails/version.rb +1 -1
- metadata +457 -293
@@ -0,0 +1,1471 @@
|
|
1
|
+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
|
2
|
+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
3
|
+
|
4
|
+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
5
|
+
<head>
|
6
|
+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
|
7
|
+
|
8
|
+
<title>Ruby on Rails Guides: Action View Overview</title>
|
9
|
+
|
10
|
+
<link rel="stylesheet" type="text/css" href="stylesheets/style.css" />
|
11
|
+
<link rel="stylesheet" type="text/css" href="stylesheets/print.css" media="print" />
|
12
|
+
|
13
|
+
<link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shCore.css" />
|
14
|
+
<link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shThemeRailsGuides.css" />
|
15
|
+
|
16
|
+
<link rel="stylesheet" type="text/css" href="stylesheets/fixes.css" />
|
17
|
+
</head>
|
18
|
+
<body class="guide">
|
19
|
+
<div id="topNav">
|
20
|
+
<div class="wrapper">
|
21
|
+
<strong>More at <a href="http://rubyonrails.org/">rubyonrails.org:</a> </strong>
|
22
|
+
<a href="http://rubyonrails.org/">Overview</a> |
|
23
|
+
<a href="http://rubyonrails.org/download">Download</a> |
|
24
|
+
<a href="http://rubyonrails.org/deploy">Deploy</a> |
|
25
|
+
<a href="https://github.com/rails/rails">Code</a> |
|
26
|
+
<a href="http://rubyonrails.org/screencasts">Screencasts</a> |
|
27
|
+
<a href="http://rubyonrails.org/documentation">Documentation</a> |
|
28
|
+
<a href="http://rubyonrails.org/ecosystem">Ecosystem</a> |
|
29
|
+
<a href="http://rubyonrails.org/community">Community</a> |
|
30
|
+
<a href="http://weblog.rubyonrails.org/">Blog</a>
|
31
|
+
</div>
|
32
|
+
</div>
|
33
|
+
<div id="header">
|
34
|
+
<div class="wrapper clearfix">
|
35
|
+
<h1><a href="index.html" title="Return to home page">Guides.rubyonrails.org</a></h1>
|
36
|
+
<p class="hide"><a href="#mainCol">Skip navigation</a>.</p>
|
37
|
+
<ul class="nav">
|
38
|
+
<li><a href="index.html">Home</a></li>
|
39
|
+
<li class="index"><a href="index.html" onclick="guideMenu(); return false;" id="guidesMenu">Guides Index</a>
|
40
|
+
<div id="guides" class="clearfix" style="display: none;">
|
41
|
+
<hr />
|
42
|
+
<dl class="L">
|
43
|
+
<dt>Start Here</dt>
|
44
|
+
<dd><a href="getting_started.html">Getting Started with Rails</a></dd>
|
45
|
+
<dt>Models</dt>
|
46
|
+
<dd><a href="migrations.html">Rails Database Migrations</a></dd>
|
47
|
+
<dd><a href="active_record_validations_callbacks.html">Active Record Validations and Callbacks</a></dd>
|
48
|
+
<dd><a href="association_basics.html">Active Record Associations</a></dd>
|
49
|
+
<dd><a href="active_record_querying.html">Active Record Query Interface</a></dd>
|
50
|
+
<dt>Views</dt>
|
51
|
+
<dd><a href="layouts_and_rendering.html">Layouts and Rendering in Rails</a></dd>
|
52
|
+
<dd><a href="form_helpers.html">Action View Form Helpers</a></dd>
|
53
|
+
<dt>Controllers</dt>
|
54
|
+
<dd><a href="action_controller_overview.html">Action Controller Overview</a></dd>
|
55
|
+
<dd><a href="routing.html">Rails Routing from the Outside In</a></dd>
|
56
|
+
</dl>
|
57
|
+
<dl class="R">
|
58
|
+
<dt>Digging Deeper</dt>
|
59
|
+
<dd><a href="active_support_core_extensions.html">Active Support Core Extensions</a></dd>
|
60
|
+
<dd><a href="i18n.html">Rails Internationalization API</a></dd>
|
61
|
+
<dd><a href="action_mailer_basics.html">Action Mailer Basics</a></dd>
|
62
|
+
<dd><a href="testing.html">Testing Rails Applications</a></dd>
|
63
|
+
<dd><a href="security.html">Securing Rails Applications</a></dd>
|
64
|
+
<dd><a href="debugging_rails_applications.html">Debugging Rails Applications</a></dd>
|
65
|
+
<dd><a href="performance_testing.html">Performance Testing Rails Applications</a></dd>
|
66
|
+
<dd><a href="configuring.html">Configuring Rails Applications</a></dd>
|
67
|
+
<dd><a href="command_line.html">Rails Command Line Tools and Rake Tasks</a></dd>
|
68
|
+
<dd><a href="caching_with_rails.html">Caching with Rails</a></dd>
|
69
|
+
<dd><a href="asset_pipeline.html">Asset Pipeline</a></dd>
|
70
|
+
|
71
|
+
<dt>Extending Rails</dt>
|
72
|
+
<dd><a href="plugins.html">The Basics of Creating Rails Plugins</a></dd>
|
73
|
+
<dd><a href="rails_on_rack.html">Rails on Rack</a></dd>
|
74
|
+
<dd><a href="generators.html">Creating and Customizing Rails Generators</a></dd>
|
75
|
+
|
76
|
+
<dt>Contributing to Ruby on Rails</dt>
|
77
|
+
<dd><a href="contributing_to_ruby_on_rails.html">Contributing to Ruby on Rails</a></dd>
|
78
|
+
<dd><a href="api_documentation_guidelines.html">API Documentation Guidelines</a></dd>
|
79
|
+
<dd><a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a></dd>
|
80
|
+
|
81
|
+
<dt>Release Notes</dt>
|
82
|
+
<dd><a href="3_1_release_notes.html">Ruby on Rails 3.1 Release Notes</a></dd>
|
83
|
+
<dd><a href="3_0_release_notes.html">Ruby on Rails 3.0 Release Notes</a></dd>
|
84
|
+
<dd><a href="2_3_release_notes.html">Ruby on Rails 2.3 Release Notes</a></dd>
|
85
|
+
<dd><a href="2_2_release_notes.html">Ruby on Rails 2.2 Release Notes</a></dd>
|
86
|
+
</dl>
|
87
|
+
</div>
|
88
|
+
</li>
|
89
|
+
<li><a href="contributing_to_ruby_on_rails.html">Contribute</a></li>
|
90
|
+
<li><a href="credits.html">Credits</a></li>
|
91
|
+
</ul>
|
92
|
+
</div>
|
93
|
+
</div>
|
94
|
+
<hr class="hide" />
|
95
|
+
|
96
|
+
<div id="feature">
|
97
|
+
<div class="wrapper">
|
98
|
+
<h2>Action View Overview</h2>
|
99
|
+
<p>In this guide you will learn:</p>
|
100
|
+
<ul>
|
101
|
+
<li>What Action View is, and how to use it with Rails</li>
|
102
|
+
<li>How to use Action View outside of Rails</li>
|
103
|
+
<li>How best to use templates, partials, and layouts</li>
|
104
|
+
<li>What helpers are provided by Action View, and how to make your own</li>
|
105
|
+
<li>How to use localized views</li>
|
106
|
+
</ul>
|
107
|
+
|
108
|
+
<div id="subCol">
|
109
|
+
<h3 class="chapter"><img src="images/chapters_icon.gif" alt="" />Chapters</h3>
|
110
|
+
<ol class="chapters">
|
111
|
+
<li><a href="#what-is-action-view">What is Action View?</a></li><li><a href="#using-action-view-with-rails">Using Action View with Rails</a></li><li><a href="#using-action-view-outside-of-rails">Using Action View outside of Rails</a></li><li><a href="#templates-partials-and-layouts">Templates, Partials and Layouts</a><ul><li><a href="#templates">Templates</a></li> <li><a href="#partials">Partials</a></li> <li><a href="#layouts">Layouts</a></li></ul></li><li><a href="#using-templates-partials-and-layouts-in-the-rails-way">Using Templates, Partials and Layouts in “The Rails Way”</a></li><li><a href="#partial-layouts">Partial Layouts</a></li><li><a href="#view-paths">View Paths</a></li><li><a href="#overview-of-all-the-helpers-provided-by-action-view">Overview of all the helpers provided by Action View</a><ul><li><a href="#activerecordhelper">ActiveRecordHelper</a></li> <li><a href="#recordtaghelper">RecordTagHelper</a></li> <li><a href="#assettaghelper">AssetTagHelper</a></li> <li><a href="#atomfeedhelper">AtomFeedHelper</a></li> <li><a href="#benchmarkhelper">BenchmarkHelper</a></li> <li><a href="#cachehelper">CacheHelper</a></li> <li><a href="#capturehelper">CaptureHelper</a></li> <li><a href="#datehelper">DateHelper</a></li> <li><a href="#debughelper">DebugHelper</a></li> <li><a href="#formhelper">FormHelper</a></li> <li><a href="#formoptionshelper">FormOptionsHelper</a></li> <li><a href="#formtaghelper">FormTagHelper</a></li> <li><a href="#javascripthelper">JavaScriptHelper</a></li> <li><a href="#numberhelper">NumberHelper</a></li></ul></li><li><a href="#localized-views">Localized Views</a></li></ol></div>
|
112
|
+
</div>
|
113
|
+
</div>
|
114
|
+
|
115
|
+
<div id="container">
|
116
|
+
<div class="wrapper">
|
117
|
+
<div id="mainCol">
|
118
|
+
<h3 id="what-is-action-view">1 What is Action View?</h3>
|
119
|
+
<p>Action View and Action Controller are the two major components of Action Pack. In Rails, web requests are handled by Action Pack, which splits the work into a controller part (performing the logic) and a view part (rendering a template). Typically, Action Controller will be concerned with communicating with the database and performing <span class="caps">CRUD</span> actions where necessary. Action View is then responsible for compiling the response.</p>
|
120
|
+
<p>Action View templates are written using embedded Ruby in tags mingled with <span class="caps">HTML</span>. To avoid cluttering the templates with boilerplate code, a number of helper classes provide common behavior for forms, dates, and strings. It’s also easy to add new helpers to your application as it evolves.</p>
|
121
|
+
<p>Note: Some features of Action View are tied to Active Record, but that doesn’t mean that Action View depends on Active Record. Action View is an independent package that can be used with any sort of backend.</p>
|
122
|
+
<h3 id="using-action-view-with-rails">2 Using Action View with Rails</h3>
|
123
|
+
<p>For each controller there is an associated directory in the <tt>app/views</tt> directory which holds the template files that make up the views associated with that controller. These files are used to display the view that results from each controller action.</p>
|
124
|
+
<p>Let’s take a look at what Rails does by default when creating a new resource using the scaffold generator:</p>
|
125
|
+
<div class="code_container">
|
126
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
127
|
+
$ rails generate scaffold post
|
128
|
+
[...]
|
129
|
+
invoke scaffold_controller
|
130
|
+
create app/controllers/posts_controller.rb
|
131
|
+
invoke erb
|
132
|
+
create app/views/posts
|
133
|
+
create app/views/posts/index.html.erb
|
134
|
+
create app/views/posts/edit.html.erb
|
135
|
+
create app/views/posts/show.html.erb
|
136
|
+
create app/views/posts/new.html.erb
|
137
|
+
create app/views/posts/_form.html.erb
|
138
|
+
[...]
|
139
|
+
</pre>
|
140
|
+
</div>
|
141
|
+
<p>There is a naming convention for views in Rails. Typically, the views share their name with the associated controller action, as you can see above.
|
142
|
+
For example, the index controller action of the <tt>posts_controller.rb</tt> will use the <tt>index.html.erb</tt> view file in the <tt>app/views/posts</tt> directory.
|
143
|
+
The complete <span class="caps">HTML</span> returned to the client is composed of a combination of this <span class="caps">ERB</span> file, a layout template that wraps it, and all the partials that the view may reference. Later on this guide you can find a more detailed documentation of each one of this three components.</p>
|
144
|
+
<h3 id="using-action-view-outside-of-rails">3 Using Action View outside of Rails</h3>
|
145
|
+
<p>Action View works well with Action Record, but it can also be used with other Ruby tools. We can demonstrate this by creating a small <a href="http://rack.rubyforge.org/">Rack</a> application that includes Action View functionality. This may be useful, for example, if you’d like access to Action View’s helpers in a Rack application.</p>
|
146
|
+
<p>Let’s start by ensuring that you have the Action Pack and Rack gems installed:</p>
|
147
|
+
<div class="code_container">
|
148
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
149
|
+
$ gem install actionpack
|
150
|
+
$ gem install rack
|
151
|
+
</pre>
|
152
|
+
</div>
|
153
|
+
<p>Now we’ll create a simple “Hello World” application that uses the <tt>titleize</tt> method provided by Active Support.</p>
|
154
|
+
<p><strong>hello_world.rb:</strong></p>
|
155
|
+
<div class="code_container">
|
156
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
157
|
+
require 'rubygems'
|
158
|
+
require 'active_support/core_ext/string/inflections'
|
159
|
+
require 'rack'
|
160
|
+
|
161
|
+
def hello_world(env)
|
162
|
+
[200, {"Content-Type" => "text/html"}, "hello world".titleize]
|
163
|
+
end
|
164
|
+
|
165
|
+
Rack::Handler::Mongrel.run method(:hello_world), :Port => 4567
|
166
|
+
</pre>
|
167
|
+
</div>
|
168
|
+
<p>We can see this all come together by starting up the application and then visiting <tt>http://localhost:4567/</tt></p>
|
169
|
+
<div class="code_container">
|
170
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
171
|
+
$ ruby hello_world.rb
|
172
|
+
</pre>
|
173
|
+
</div>
|
174
|
+
<p><span class="caps">TODO</span> needs a screenshot? I have one – not sure where to put it.</p>
|
175
|
+
<p>Notice how ‘hello world’ has been converted into ‘Hello World’ by the <tt>titleize</tt> helper method.</p>
|
176
|
+
<p>Action View can also be used with <a href="http://www.sinatrarb.com/">Sinatra</a> in the same way.</p>
|
177
|
+
<p>Let’s start by ensuring that you have the Action Pack and Sinatra gems installed:</p>
|
178
|
+
<div class="code_container">
|
179
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
180
|
+
$ gem install actionpack
|
181
|
+
$ gem install sinatra
|
182
|
+
</pre>
|
183
|
+
</div>
|
184
|
+
<p>Now we’ll create the same “Hello World” application in Sinatra.</p>
|
185
|
+
<p><strong>hello_world.rb:</strong></p>
|
186
|
+
<div class="code_container">
|
187
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
188
|
+
require 'rubygems'
|
189
|
+
require 'action_view'
|
190
|
+
require 'sinatra'
|
191
|
+
|
192
|
+
get '/' do
|
193
|
+
erb 'hello world'.titleize
|
194
|
+
end
|
195
|
+
</pre>
|
196
|
+
</div>
|
197
|
+
<p>Then, we can run the application:</p>
|
198
|
+
<div class="code_container">
|
199
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
200
|
+
$ ruby hello_world.rb
|
201
|
+
</pre>
|
202
|
+
</div>
|
203
|
+
<p>Once the application is running, you can see Sinatra and Action View working together by visiting <tt>http://localhost:4567/</tt></p>
|
204
|
+
<p><span class="caps">TODO</span> needs a screenshot? I have one – not sure where to put it.</p>
|
205
|
+
<h3 id="templates-partials-and-layouts">4 Templates, Partials and Layouts</h3>
|
206
|
+
<p>As mentioned before, the final <span class="caps">HTML</span> output is a composition of three Rails elements: <tt>Templates</tt>, <tt>Partials</tt> and <tt>Layouts</tt>.
|
207
|
+
Find below a brief overview of each one of them.</p>
|
208
|
+
<h4 id="templates">4.1 Templates</h4>
|
209
|
+
<p>Action View templates can be written in several ways. If the template file has a <tt>.erb</tt> extension then it uses a mixture of <span class="caps">ERB</span> (included in Ruby) and <span class="caps">HTML</span>. If the template file has a <tt>.builder</tt> extension then a fresh instance of <tt>Builder::XmlMarkup</tt> library is used.</p>
|
210
|
+
<p>Rails supports multiple template systems and uses a file extension to distinguish amongst them. For example, an <span class="caps">HTML</span> file using the <span class="caps">ERB</span> template system will have <tt>.html.erb</tt> as a file extension.</p>
|
211
|
+
<h5 id="erb">4.1.1 <span class="caps">ERB</span></h5>
|
212
|
+
<p>Within an <span class="caps">ERB</span> template Ruby code can be included using both <tt><% %></tt> and <tt><%= %></tt> tags. The <tt><% %></tt> are used to execute Ruby code that does not return anything, such as conditions, loops or blocks, and the <tt><%= %></tt> tags are used when you want output.</p>
|
213
|
+
<p>Consider the following loop for names:</p>
|
214
|
+
<div class="code_container">
|
215
|
+
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
|
216
|
+
<b>Names of all the people</b>
|
217
|
+
<% @people.each do |person| %>
|
218
|
+
Name: <%= person.name %><br/>
|
219
|
+
<% end %>
|
220
|
+
</pre>
|
221
|
+
</div>
|
222
|
+
<p>The loop is setup in regular embedding tags <tt><% %></tt> and the name is written using the output embedding tag <tt><%= %></tt>. Note that this is not just a usage suggestion, for Regular output functions like print or puts won’t work with <span class="caps">ERB</span> templates. So this would be wrong:</p>
|
223
|
+
<div class="code_container">
|
224
|
+
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
|
225
|
+
<%# WRONG %>
|
226
|
+
Hi, Mr. <% puts "Frodo" %>
|
227
|
+
</pre>
|
228
|
+
</div>
|
229
|
+
<p>To suppress leading and trailing whitespaces, you can use <tt><%-</tt> <tt>-%></tt> interchangeably with <tt><%</tt> and <tt>%></tt>.</p>
|
230
|
+
<h5 id="builder">4.1.2 Builder</h5>
|
231
|
+
<p>Builder templates are a more programmatic alternative to <span class="caps">ERB</span>. They are especially useful for generating <span class="caps">XML</span> content. An XmlMarkup object named <tt>xml</tt> is automatically made available to templates with a <tt>.builder</tt> extension.</p>
|
232
|
+
<p>Here are some basic examples:</p>
|
233
|
+
<div class="code_container">
|
234
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
235
|
+
xml.em("emphasized")
|
236
|
+
xml.em { xml.b("emph & bold") }
|
237
|
+
xml.a("A Link", "href"=>"http://rubyonrails.org")
|
238
|
+
xml.target("name"=>"compile", "option"=>"fast")
|
239
|
+
</pre>
|
240
|
+
</div>
|
241
|
+
<p>will produce</p>
|
242
|
+
<div class="code_container">
|
243
|
+
<pre class="brush: xml; gutter: false; toolbar: false">
|
244
|
+
<em>emphasized</em>
|
245
|
+
<em><b>emph &amp; bold</b></em>
|
246
|
+
<a href="http://rubyonrails.org">A link</a>
|
247
|
+
<target option="fast" name="compile" />
|
248
|
+
</pre>
|
249
|
+
</div>
|
250
|
+
<p>Any method with a block will be treated as an <span class="caps">XML</span> markup tag with nested markup in the block. For example, the following:</p>
|
251
|
+
<div class="code_container">
|
252
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
253
|
+
xml.div {
|
254
|
+
xml.h1(@person.name)
|
255
|
+
xml.p(@person.bio)
|
256
|
+
}
|
257
|
+
</pre>
|
258
|
+
</div>
|
259
|
+
<p>would produce something like:</p>
|
260
|
+
<div class="code_container">
|
261
|
+
<pre class="brush: xml; gutter: false; toolbar: false">
|
262
|
+
<div>
|
263
|
+
<h1>David Heinemeier Hansson</h1>
|
264
|
+
<p>A product of Danish Design during the Winter of '79...</p>
|
265
|
+
</div>
|
266
|
+
</pre>
|
267
|
+
</div>
|
268
|
+
<p>A full-length <span class="caps">RSS</span> example actually used on Basecamp:</p>
|
269
|
+
<div class="code_container">
|
270
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
271
|
+
xml.rss("version" => "2.0", "xmlns:dc" => "http://purl.org/dc/elements/1.1/") do
|
272
|
+
xml.channel do
|
273
|
+
xml.title(@feed_title)
|
274
|
+
xml.link(@url)
|
275
|
+
xml.description "Basecamp: Recent items"
|
276
|
+
xml.language "en-us"
|
277
|
+
xml.ttl "40"
|
278
|
+
|
279
|
+
for item in @recent_items
|
280
|
+
xml.item do
|
281
|
+
xml.title(item_title(item))
|
282
|
+
xml.description(item_description(item)) if item_description(item)
|
283
|
+
xml.pubDate(item_pubDate(item))
|
284
|
+
xml.guid(@person.firm.account.url + @recent_items.url(item))
|
285
|
+
xml.link(@person.firm.account.url + @recent_items.url(item))
|
286
|
+
xml.tag!("dc:creator", item.author_name) if item_has_creator?(item)
|
287
|
+
end
|
288
|
+
end
|
289
|
+
end
|
290
|
+
end
|
291
|
+
</pre>
|
292
|
+
</div>
|
293
|
+
<h5 id="template-caching">4.1.3 Template Caching</h5>
|
294
|
+
<p>By default, Rails will compile each template to a method in order to render it. When you alter a template, Rails will check the file’s modification time and recompile it in development mode.</p>
|
295
|
+
<h4 id="partials">4.2 Partials</h4>
|
296
|
+
<p>Partial templates – usually just called “partials” – are another device for breaking 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.</p>
|
297
|
+
<h5 id="naming-partials">4.2.1 Naming Partials</h5>
|
298
|
+
<p>To render a partial as part of a view, you use the <tt>render</tt> method within the view:</p>
|
299
|
+
<div class="code_container">
|
300
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
301
|
+
<%= render "menu" %>
|
302
|
+
</pre>
|
303
|
+
</div>
|
304
|
+
<p>This will render a file named <tt>_menu.html.erb</tt> at that point within the view is 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:</p>
|
305
|
+
<div class="code_container">
|
306
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
307
|
+
<%= render "shared/menu" %>
|
308
|
+
</pre>
|
309
|
+
</div>
|
310
|
+
<p>That code will pull in the partial from <tt>app/views/shared/_menu.html.erb</tt>.</p>
|
311
|
+
<h5 id="using-partials-to-simplify-views">4.2.2 Using Partials to simplify Views</h5>
|
312
|
+
<p>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:</p>
|
313
|
+
<div class="code_container">
|
314
|
+
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
|
315
|
+
<%= render "shared/ad_banner" %>
|
316
|
+
|
317
|
+
<h1>Products</h1>
|
318
|
+
|
319
|
+
<p>Here are a few of our fine products:</p>
|
320
|
+
<% @products.each do |product| %>
|
321
|
+
<%= render :partial => "product", :locals => { :product => product } %>
|
322
|
+
<% end %>
|
323
|
+
|
324
|
+
<%= render "shared/footer" %>
|
325
|
+
</pre>
|
326
|
+
</div>
|
327
|
+
<p>Here, the <tt>_ad_banner.html.erb</tt> and <tt>_footer.html.erb</tt> 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.</p>
|
328
|
+
<h5 id="the-as-and-object-options">4.2.3 The :as and :object options</h5>
|
329
|
+
<p>By default <tt>ActionView::Partials::PartialRenderer</tt> has its object in a local variable with the same name as the template. So, given</p>
|
330
|
+
<div class="code_container">
|
331
|
+
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
|
332
|
+
<%= render :partial => "product" %>
|
333
|
+
</pre>
|
334
|
+
</div>
|
335
|
+
<p>within product we’ll get <tt>@product</tt> in the local variable <tt>product</tt>, as if we had written:</p>
|
336
|
+
<div class="code_container">
|
337
|
+
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
|
338
|
+
<%= render :partial => "product", :locals => { :product => @product } %>
|
339
|
+
</pre>
|
340
|
+
</div>
|
341
|
+
<p>With the <tt>:as</tt> option we can specify a different name for said local variable. For example, if we wanted it to be <tt>item</tt> instead of product+ we’d do:</p>
|
342
|
+
<div class="code_container">
|
343
|
+
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
|
344
|
+
<%= render :partial => "product", :as => 'item' %>
|
345
|
+
</pre>
|
346
|
+
</div>
|
347
|
+
<p>The <tt>:object</tt> option can be used to directly specify which object is rendered into the partial; useful when the template’s object is elsewhere, in a different ivar or in a local variable for instance.</p>
|
348
|
+
<p>For example, instead of:</p>
|
349
|
+
<div class="code_container">
|
350
|
+
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
|
351
|
+
<%= render :partial => "product", :locals => { :product => @item } %>
|
352
|
+
</pre>
|
353
|
+
</div>
|
354
|
+
<p>you’d do:</p>
|
355
|
+
<div class="code_container">
|
356
|
+
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
|
357
|
+
<%= render :partial => "product", :object => @item %>
|
358
|
+
</pre>
|
359
|
+
</div>
|
360
|
+
<p>The <tt>:object</tt> and <tt>:as</tt> options can be used together.</p>
|
361
|
+
<h5 id="rendering-collections">4.2.4 Rendering Collections</h5>
|
362
|
+
<p>The example of partial use describes a familiar pattern where a template needs to iterate over an array and render a sub template for each of the elements. This pattern has been implemented as a single method that accepts an array and renders a partial by the same name as the elements contained within.
|
363
|
+
So the three-lined example for rendering all the products can be rewritten with a single line:</p>
|
364
|
+
<div class="code_container">
|
365
|
+
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
|
366
|
+
<%= render :partial => "product", :collection => @products %>
|
367
|
+
</pre>
|
368
|
+
</div>
|
369
|
+
<p>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 <tt>_product</tt> , and within the <tt>_product</tt> partial, you can refer to <tt>product</tt> to get the instance that is being rendered.</p>
|
370
|
+
<p>You can use a shorthand syntax for rendering collections. Assuming @products is a collection of <tt>Product</tt> instances, you can simply write the following to produce the same result:</p>
|
371
|
+
<div class="code_container">
|
372
|
+
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
|
373
|
+
<%= render @products %>
|
374
|
+
</pre>
|
375
|
+
</div>
|
376
|
+
<p>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.</p>
|
377
|
+
<h5 id="spacer-templates">4.2.5 Spacer Templates</h5>
|
378
|
+
<p>You can also specify a second partial to be rendered between instances of the main partial by using the <tt>:spacer_template</tt> option:</p>
|
379
|
+
<div class="code_container">
|
380
|
+
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
|
381
|
+
<%= render @products, :spacer_template => "product_ruler" %>
|
382
|
+
</pre>
|
383
|
+
</div>
|
384
|
+
<p>Rails will render the <tt>_product_ruler</tt> partial (with no data passed in to it) between each pair of <tt>_product</tt> partials.</p>
|
385
|
+
<h4 id="layouts">4.3 Layouts</h4>
|
386
|
+
<p><span class="caps">TODO</span>…</p>
|
387
|
+
<h3 id="using-templates-partials-and-layouts-in-the-rails-way">5 Using Templates, Partials and Layouts in “The Rails Way”</h3>
|
388
|
+
<p><span class="caps">TODO</span>…</p>
|
389
|
+
<h3 id="partial-layouts">6 Partial Layouts</h3>
|
390
|
+
<p>Partials can have their own layouts applied to them. These layouts are different than the ones that are specified globally for the entire action, but they work in a similar fashion.</p>
|
391
|
+
<p>Let’s say we’re displaying a post on a page where it should be wrapped in a <tt>div</tt> for display purposes. First, we’ll create a new <tt>Post</tt>:</p>
|
392
|
+
<div class="code_container">
|
393
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
394
|
+
Post.create(:body => 'Partial Layouts are cool!')
|
395
|
+
</pre>
|
396
|
+
</div>
|
397
|
+
<p>In the <tt>show</tt> template, we’ll render the <tt>post</tt> partial wrapped in the <tt>box</tt> layout:</p>
|
398
|
+
<p><strong>posts/show.html.erb</strong></p>
|
399
|
+
<div class="code_container">
|
400
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
401
|
+
<%= render :partial => 'post', :layout => 'box', :locals => {:post => @post} %>
|
402
|
+
</pre>
|
403
|
+
</div>
|
404
|
+
<p>The <tt>box</tt> layout simply wraps the <tt>post</tt> partial in a <tt>div</tt>:</p>
|
405
|
+
<p><strong>posts/_box.html.erb</strong></p>
|
406
|
+
<div class="code_container">
|
407
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
408
|
+
<div class='box'>
|
409
|
+
<%= yield %>
|
410
|
+
</div>
|
411
|
+
</pre>
|
412
|
+
</div>
|
413
|
+
<p>The <tt>post</tt> partial wraps the post’s <tt>body</tt> in a <tt>div</tt> with the <tt>id</tt> of the post using the <tt>div_for</tt> helper:</p>
|
414
|
+
<p><strong>posts/_post.html.erb</strong></p>
|
415
|
+
<div class="code_container">
|
416
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
417
|
+
<%= div_for(post) do %>
|
418
|
+
<p><%= post.body %></p>
|
419
|
+
<% end %>
|
420
|
+
</pre>
|
421
|
+
</div>
|
422
|
+
<p>This example would output the following:</p>
|
423
|
+
<div class="code_container">
|
424
|
+
<pre class="brush: xml; gutter: false; toolbar: false">
|
425
|
+
<div class='box'>
|
426
|
+
<div id='post_1'>
|
427
|
+
<p>Partial Layouts are cool!</p>
|
428
|
+
</div>
|
429
|
+
</div>
|
430
|
+
</pre>
|
431
|
+
</div>
|
432
|
+
<p>Note that the partial layout has access to the local <tt>post</tt> variable that was passed into the <tt>render</tt> call. However, unlike application-wide layouts, partial layouts still have the underscore prefix.</p>
|
433
|
+
<p>You can also render a block of code within a partial layout instead of calling <tt>yield</tt>. For example, if we didn’t have the <tt>post</tt> partial, we could do this instead:</p>
|
434
|
+
<p><strong>posts/show.html.erb</strong></p>
|
435
|
+
<div class="code_container">
|
436
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
437
|
+
<% render(:layout => 'box', :locals => {:post => @post}) do %>
|
438
|
+
<%= div_for(post) do %>
|
439
|
+
<p><%= post.body %></p>
|
440
|
+
<% end %>
|
441
|
+
<% end %>
|
442
|
+
</pre>
|
443
|
+
</div>
|
444
|
+
<p>If we’re using the same <tt>box</tt> partial from above, his would produce the same output as the previous example.</p>
|
445
|
+
<h3 id="view-paths">7 View Paths</h3>
|
446
|
+
<p><span class="caps">TODO</span>…</p>
|
447
|
+
<h3 id="overview-of-all-the-helpers-provided-by-action-view">8 Overview of all the helpers provided by Action View</h3>
|
448
|
+
<p>The following is only a brief overview summary of the helpers available in Action View. It’s recommended that you review the <span class="caps">API</span> Documentation, which covers all of the helpers in more detail, but this should serve as a good starting point.</p>
|
449
|
+
<h4 id="activerecordhelper">8.1 ActiveRecordHelper</h4>
|
450
|
+
<p>The Active Record Helper makes it easier to create forms for records kept in instance variables. You may also want to review the <a href="form_helpers.html">Rails Form helpers guide</a>.</p>
|
451
|
+
<h5 id="error_message_on">8.1.1 error_message_on</h5>
|
452
|
+
<p>Returns a string containing the error message attached to the method on the object if one exists.</p>
|
453
|
+
<div class="code_container">
|
454
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
455
|
+
error_message_on "post", "title"
|
456
|
+
</pre>
|
457
|
+
</div>
|
458
|
+
<h5 id="error_messages_for">8.1.2 error_messages_for</h5>
|
459
|
+
<p>Returns a string with a <span class="caps">DIV</span> containing all of the error messages for the objects located as instance variables by the names given.</p>
|
460
|
+
<div class="code_container">
|
461
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
462
|
+
error_messages_for "post"
|
463
|
+
</pre>
|
464
|
+
</div>
|
465
|
+
<h5 id="form">8.1.3 form</h5>
|
466
|
+
<p>Returns a form with inputs for all attributes of the specified Active Record object. For example, let’s say we have a <tt>@post</tt> with attributes named <tt>title</tt> of type <tt>String</tt> and <tt>body</tt> of type <tt>Text</tt>. Calling <tt>form</tt> would produce a form to creating a new post with inputs for those attributes.</p>
|
467
|
+
<div class="code_container">
|
468
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
469
|
+
form("post")
|
470
|
+
</pre>
|
471
|
+
</div>
|
472
|
+
<div class="code_container">
|
473
|
+
<pre class="brush: xml; gutter: false; toolbar: false">
|
474
|
+
<form action='/posts/create' method='post'>
|
475
|
+
<p>
|
476
|
+
<label for="post_title">Title</label><br />
|
477
|
+
<input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />
|
478
|
+
</p>
|
479
|
+
<p>
|
480
|
+
<label for="post_body">Body</label><br />
|
481
|
+
<textarea cols="40" id="post_body" name="post[body]" rows="20"></textarea>
|
482
|
+
</p>
|
483
|
+
<input name="commit" type="submit" value="Create" />
|
484
|
+
</form>
|
485
|
+
</pre>
|
486
|
+
</div>
|
487
|
+
<p>Typically, <tt>form_for</tt> is used instead of <tt>form</tt> because it doesn’t automatically include all of the model’s attributes.</p>
|
488
|
+
<h5 id="input">8.1.4 input</h5>
|
489
|
+
<p>Returns a default input tag for the type of object returned by the method.</p>
|
490
|
+
<p>For example, if <tt>@post</tt> has an attribute <tt>title</tt> mapped to a <tt>String</tt> column that holds “Hello World”:</p>
|
491
|
+
<div class="code_container">
|
492
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
493
|
+
input("post", "title") # =>
|
494
|
+
<input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />
|
495
|
+
</pre>
|
496
|
+
</div>
|
497
|
+
<h4 id="recordtaghelper">8.2 RecordTagHelper</h4>
|
498
|
+
<p>This module provides methods for generating a container tag, such as a <tt><div></tt>, for your record. This is the recommended way of creating a container for render your Active Record object, as it adds an appropriate class and id attributes to that container. You can then refer to those containers easily by following the convention, instead of having to think about which class or id attribute you should use.</p>
|
499
|
+
<h5 id="content_tag_for">8.2.1 content_tag_for</h5>
|
500
|
+
<p>Renders a container tag that relates to your Active Record Object.</p>
|
501
|
+
<p>For example, given <tt>@post</tt> is the object of <tt>Post</tt> class, you can do:</p>
|
502
|
+
<div class="code_container">
|
503
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
504
|
+
<%= content_tag_for(:tr, @post) do %>
|
505
|
+
<td><%= @post.title %></td>
|
506
|
+
<% end %>
|
507
|
+
</pre>
|
508
|
+
</div>
|
509
|
+
<p>This will generate this <span class="caps">HTML</span> output:</p>
|
510
|
+
<div class="code_container">
|
511
|
+
<pre class="brush: xml; gutter: false; toolbar: false">
|
512
|
+
<tr id="post_1234" class="post">
|
513
|
+
<td>Hello World!</td>
|
514
|
+
</tr>
|
515
|
+
</pre>
|
516
|
+
</div>
|
517
|
+
<p>You can also supply <span class="caps">HTML</span> attributes as an additional option hash. For example:</p>
|
518
|
+
<div class="code_container">
|
519
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
520
|
+
<%= content_tag_for(:tr, @post, :class => "frontpage") do %>
|
521
|
+
<td><%= @post.title %></td>
|
522
|
+
<% end %>
|
523
|
+
</pre>
|
524
|
+
</div>
|
525
|
+
<p>Will generate this <span class="caps">HTML</span> output:</p>
|
526
|
+
<div class="code_container">
|
527
|
+
<pre class="brush: xml; gutter: false; toolbar: false">
|
528
|
+
<tr id="post_1234" class="post frontpage">
|
529
|
+
<td>Hello World!</td>
|
530
|
+
</tr>
|
531
|
+
</pre>
|
532
|
+
</div>
|
533
|
+
<p>You can pass a collection of Active Record objects. This method will loop through your objects and create a container for each of them. For example, given <tt>@posts</tt> is an array of two <tt>Post</tt> objects:</p>
|
534
|
+
<div class="code_container">
|
535
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
536
|
+
<%= content_tag_for(:tr, @posts) do |post| %>
|
537
|
+
<td><%= post.title %></td>
|
538
|
+
<% end %>
|
539
|
+
</pre>
|
540
|
+
</div>
|
541
|
+
<p>Will generate this <span class="caps">HTML</span> output:</p>
|
542
|
+
<div class="code_container">
|
543
|
+
<pre class="brush: xml; gutter: false; toolbar: false">
|
544
|
+
<tr id="post_1234" class="post">
|
545
|
+
<td>Hello World!</td>
|
546
|
+
</tr>
|
547
|
+
<tr id="post_1235" class="post">
|
548
|
+
<td>Ruby on Rails Rocks!</td>
|
549
|
+
</tr>
|
550
|
+
</pre>
|
551
|
+
</div>
|
552
|
+
<h5 id="div_for">8.2.2 div_for</h5>
|
553
|
+
<p>This is actually a convenient method which calls <tt>content_tag_for</tt> internally with <tt>:div</tt> as the tag name. You can pass either an Active Record object or a collection of objects. For example:</p>
|
554
|
+
<div class="code_container">
|
555
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
556
|
+
<%= div_for(@post, :class => "frontpage") do %>
|
557
|
+
<td><%= @post.title %></td>
|
558
|
+
<% end %>
|
559
|
+
</pre>
|
560
|
+
</div>
|
561
|
+
<p>Will generate this <span class="caps">HTML</span> output:</p>
|
562
|
+
<div class="code_container">
|
563
|
+
<pre class="brush: xml; gutter: false; toolbar: false">
|
564
|
+
<div id="post_1234" class="post frontpage">
|
565
|
+
<td>Hello World!</td>
|
566
|
+
</div>
|
567
|
+
</pre>
|
568
|
+
</div>
|
569
|
+
<h4 id="assettaghelper">8.3 AssetTagHelper</h4>
|
570
|
+
<p>This module provides methods for generating <span class="caps">HTML</span> that links views to assets such as images, JavaScript files, stylesheets, and feeds.</p>
|
571
|
+
<p>By default, Rails links to these assets on the current host in the public folder, but you can direct Rails to link to assets from a dedicated assets server by setting <tt>ActionController::Base.asset_host</tt> in the application configuration, typically in <tt>config/environments/production.rb</tt>. For example, let’s say your asset host is <tt>assets.example.com</tt>:</p>
|
572
|
+
<div class="code_container">
|
573
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
574
|
+
ActionController::Base.asset_host = "assets.example.com"
|
575
|
+
image_tag("rails.png") # => <img src="http://assets.example.com/images/rails.png" alt="Rails" />
|
576
|
+
</pre>
|
577
|
+
</div>
|
578
|
+
<h5 id="register_javascript_expansion">8.3.1 register_javascript_expansion</h5>
|
579
|
+
<p>Register one or more JavaScript files to be included when symbol is passed to javascript_include_tag. This method is typically intended to be called from plugin initialization to register JavaScript files that the plugin installed in <tt>public/javascripts</tt>.</p>
|
580
|
+
<div class="code_container">
|
581
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
582
|
+
ActionView::Helpers::AssetTagHelper.register_javascript_expansion :monkey => ["head", "body", "tail"]
|
583
|
+
|
584
|
+
javascript_include_tag :monkey # =>
|
585
|
+
<script type="text/javascript" src="/javascripts/head.js"></script>
|
586
|
+
<script type="text/javascript" src="/javascripts/body.js"></script>
|
587
|
+
<script type="text/javascript" src="/javascripts/tail.js"></script>
|
588
|
+
</pre>
|
589
|
+
</div>
|
590
|
+
<h5 id="register_stylesheet_expansion">8.3.2 register_stylesheet_expansion</h5>
|
591
|
+
<p>Register one or more stylesheet files to be included when symbol is passed to <tt>stylesheet_link_tag</tt>. This method is typically intended to be called from plugin initialization to register stylesheet files that the plugin installed in <tt>public/stylesheets</tt>.</p>
|
592
|
+
<div class="code_container">
|
593
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
594
|
+
ActionView::Helpers::AssetTagHelper.register_stylesheet_expansion :monkey => ["head", "body", "tail"]
|
595
|
+
|
596
|
+
stylesheet_link_tag :monkey # =>
|
597
|
+
<link href="/stylesheets/head.css" media="screen" rel="stylesheet" type="text/css" />
|
598
|
+
<link href="/stylesheets/body.css" media="screen" rel="stylesheet" type="text/css" />
|
599
|
+
<link href="/stylesheets/tail.css" media="screen" rel="stylesheet" type="text/css" />
|
600
|
+
</pre>
|
601
|
+
</div>
|
602
|
+
<h5 id="auto_discovery_link_tag">8.3.3 auto_discovery_link_tag</h5>
|
603
|
+
<p>Returns a link tag that browsers and news readers can use to auto-detect an <span class="caps">RSS</span> or <span class="caps">ATOM</span> feed.</p>
|
604
|
+
<div class="code_container">
|
605
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
606
|
+
auto_discovery_link_tag(:rss, "http://www.example.com/feed.rss", {:title => "RSS Feed"}) # =>
|
607
|
+
<link rel="alternate" type="application/rss+xml" title="RSS Feed" href="http://www.example.com/feed" />
|
608
|
+
</pre>
|
609
|
+
</div>
|
610
|
+
<h5 id="image_path">8.3.4 image_path</h5>
|
611
|
+
<p>Computes the path to an image asset in the <tt>public/images</tt> directory. Full paths from the document root will be passed through. Used internally by <tt>image_tag</tt> to build the image path.</p>
|
612
|
+
<div class="code_container">
|
613
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
614
|
+
image_path("edit.png") # => /images/edit.png
|
615
|
+
</pre>
|
616
|
+
</div>
|
617
|
+
<h5 id="image_tag">8.3.5 image_tag</h5>
|
618
|
+
<p>Returns an html image tag for the source. The source can be a full path or a file that exists in your <tt>public/images</tt> directory.</p>
|
619
|
+
<div class="code_container">
|
620
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
621
|
+
image_tag("icon.png") # => <img src="/images/icon.png" alt="Icon" />
|
622
|
+
</pre>
|
623
|
+
</div>
|
624
|
+
<h5 id="javascript_include_tag">8.3.6 javascript_include_tag</h5>
|
625
|
+
<p>Returns an html script tag for each of the sources provided. You can pass in the filename (<tt>.js</tt> extension is optional) of JavaScript files that exist in your <tt>public/javascripts</tt> directory for inclusion into the current page or you can pass the full path relative to your document root.</p>
|
626
|
+
<div class="code_container">
|
627
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
628
|
+
javascript_include_tag "common" # =>
|
629
|
+
<script type="text/javascript" src="/javascripts/common.js"></script>
|
630
|
+
</pre>
|
631
|
+
</div>
|
632
|
+
<p>If the application does not use the asset pipeline, to include the jQuery JavaScript library in your application, pass <tt>:defaults</tt> as the source. When using <tt>:defaults</tt>, if an <tt>application.js</tt> file exists in your <tt>public/javascripts</tt> directory, it will be included as well.</p>
|
633
|
+
<div class="code_container">
|
634
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
635
|
+
javascript_include_tag :defaults
|
636
|
+
</pre>
|
637
|
+
</div>
|
638
|
+
<p>You can also include all JavaScript files in the <tt>public/javascripts</tt> directory using <tt>:all</tt> as the source.</p>
|
639
|
+
<div class="code_container">
|
640
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
641
|
+
javascript_include_tag :all
|
642
|
+
</pre>
|
643
|
+
</div>
|
644
|
+
<p>You can also cache multiple JavaScript files into one file, which requires less <span class="caps">HTTP</span> connections to download and can better be compressed by gzip (leading to faster transfers). Caching will only happen if <tt>ActionController::Base.perform_caching</tt> is set to true (which is the case by default for the Rails production environment, but not for the development environment).</p>
|
645
|
+
<div class="code_container">
|
646
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
647
|
+
javascript_include_tag :all, :cache => true # =>
|
648
|
+
<script type="text/javascript" src="/javascripts/all.js"></script>
|
649
|
+
</pre>
|
650
|
+
</div>
|
651
|
+
<h5 id="javascript_path">8.3.7 javascript_path</h5>
|
652
|
+
<p>Computes the path to a JavaScript asset in the <tt>public/javascripts</tt> directory. If the source filename has no extension, <tt>.js</tt> will be appended. Full paths from the document root will be passed through. Used internally by <tt>javascript_include_tag</tt> to build the script path.</p>
|
653
|
+
<div class="code_container">
|
654
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
655
|
+
javascript_path "common" # => /javascripts/common.js
|
656
|
+
</pre>
|
657
|
+
</div>
|
658
|
+
<h5 id="stylesheet_link_tag">8.3.8 stylesheet_link_tag</h5>
|
659
|
+
<p>Returns a stylesheet link tag for the sources specified as arguments. If you don’t specify an extension, <tt>.css</tt> will be appended automatically.</p>
|
660
|
+
<div class="code_container">
|
661
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
662
|
+
stylesheet_link_tag "application" # =>
|
663
|
+
<link href="/stylesheets/application.css" media="screen" rel="stylesheet" type="text/css" />
|
664
|
+
</pre>
|
665
|
+
</div>
|
666
|
+
<p>You can also include all styles in the stylesheet directory using :all as the source:</p>
|
667
|
+
<div class="code_container">
|
668
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
669
|
+
stylesheet_link_tag :all
|
670
|
+
</pre>
|
671
|
+
</div>
|
672
|
+
<p>You can also cache multiple stylesheets into one file, which requires less <span class="caps">HTTP</span> connections and can better be compressed by gzip (leading to faster transfers). Caching will only happen if ActionController::Base.perform_caching is set to true (which is the case by default for the Rails production environment, but not for the development environment).</p>
|
673
|
+
<div class="code_container">
|
674
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
675
|
+
stylesheet_link_tag :all, :cache => true
|
676
|
+
<link href="/stylesheets/all.css" media="screen" rel="stylesheet" type="text/css" />
|
677
|
+
</pre>
|
678
|
+
</div>
|
679
|
+
<h5 id="stylesheet_path">8.3.9 stylesheet_path</h5>
|
680
|
+
<p>Computes the path to a stylesheet asset in the <tt>public/stylesheets</tt> directory. If the source filename has no extension, .css will be appended. Full paths from the document root will be passed through. Used internally by stylesheet_link_tag to build the stylesheet path.</p>
|
681
|
+
<div class="code_container">
|
682
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
683
|
+
stylesheet_path "application" # => /stylesheets/application.css
|
684
|
+
</pre>
|
685
|
+
</div>
|
686
|
+
<h4 id="atomfeedhelper">8.4 AtomFeedHelper</h4>
|
687
|
+
<h5 id="atom_feed">8.4.1 atom_feed</h5>
|
688
|
+
<p>This helper makes building an <span class="caps">ATOM</span> feed easy. Here’s a full usage example:</p>
|
689
|
+
<p><strong>config/routes.rb</strong></p>
|
690
|
+
<div class="code_container">
|
691
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
692
|
+
resources :posts
|
693
|
+
</pre>
|
694
|
+
</div>
|
695
|
+
<p><strong>app/controllers/posts_controller.rb</strong></p>
|
696
|
+
<div class="code_container">
|
697
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
698
|
+
def index
|
699
|
+
@posts = Post.all
|
700
|
+
|
701
|
+
respond_to do |format|
|
702
|
+
format.html
|
703
|
+
format.atom
|
704
|
+
end
|
705
|
+
end
|
706
|
+
</pre>
|
707
|
+
</div>
|
708
|
+
<p><strong>app/views/posts/index.atom.builder</strong></p>
|
709
|
+
<div class="code_container">
|
710
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
711
|
+
atom_feed do |feed|
|
712
|
+
feed.title("Posts Index")
|
713
|
+
feed.updated((@posts.first.created_at))
|
714
|
+
|
715
|
+
@posts.each do |post|
|
716
|
+
feed.entry(post) do |entry|
|
717
|
+
entry.title(post.title)
|
718
|
+
entry.content(post.body, :type => 'html')
|
719
|
+
|
720
|
+
entry.author do |author|
|
721
|
+
author.name(post.author_name)
|
722
|
+
end
|
723
|
+
end
|
724
|
+
end
|
725
|
+
end
|
726
|
+
</pre>
|
727
|
+
</div>
|
728
|
+
<h4 id="benchmarkhelper">8.5 BenchmarkHelper</h4>
|
729
|
+
<h5 id="benchmark">8.5.1 benchmark</h5>
|
730
|
+
<p>Allows you to measure the execution time of a block in a template and records the result to the log. Wrap this block around expensive operations or possible bottlenecks to get a time reading for the operation.</p>
|
731
|
+
<div class="code_container">
|
732
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
733
|
+
<% benchmark "Process data files" do %>
|
734
|
+
<%= expensive_files_operation %>
|
735
|
+
<% end %>
|
736
|
+
</pre>
|
737
|
+
</div>
|
738
|
+
<p>This would add something like “Process data files (0.34523)” to the log, which you can then use to compare timings when optimizing your code.</p>
|
739
|
+
<h4 id="cachehelper">8.6 CacheHelper</h4>
|
740
|
+
<h5 id="cache">8.6.1 cache</h5>
|
741
|
+
<p>A method for caching fragments of a view rather than an entire action or page. This technique is useful caching pieces like menus, lists of news topics, static <span class="caps">HTML</span> fragments, and so on. This method takes a block that contains the content you wish to cache. See <tt>ActionController::Caching::Fragments</tt> for more information.</p>
|
742
|
+
<div class="code_container">
|
743
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
744
|
+
<% cache do %>
|
745
|
+
<%= render "shared/footer" %>
|
746
|
+
<% end %>
|
747
|
+
</pre>
|
748
|
+
</div>
|
749
|
+
<h4 id="capturehelper">8.7 CaptureHelper</h4>
|
750
|
+
<h5 id="capture">8.7.1 capture</h5>
|
751
|
+
<p>The <tt>capture</tt> method allows you to extract part of a template into a variable. You can then use this variable anywhere in your templates or layout.</p>
|
752
|
+
<div class="code_container">
|
753
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
754
|
+
<% @greeting = capture do %>
|
755
|
+
<p>Welcome! The date and time is <%= Time.now %></p>
|
756
|
+
<% end %>
|
757
|
+
</pre>
|
758
|
+
</div>
|
759
|
+
<p>The captured variable can then be used anywhere else.</p>
|
760
|
+
<div class="code_container">
|
761
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
762
|
+
<html>
|
763
|
+
<head>
|
764
|
+
<title>Welcome!</title>
|
765
|
+
</head>
|
766
|
+
<body>
|
767
|
+
<%= @greeting %>
|
768
|
+
</body>
|
769
|
+
</html>
|
770
|
+
</pre>
|
771
|
+
</div>
|
772
|
+
<h5 id="content_for">8.7.2 content_for</h5>
|
773
|
+
<p>Calling <tt>content_for</tt> stores a block of markup in an identifier for later use. You can make subsequent calls to the stored content in other templates or the layout by passing the identifier as an argument to <tt>yield</tt>.</p>
|
774
|
+
<p>For example, let’s say we have a standard application layout, but also a special page that requires certain Javascript that the rest of the site doesn’t need. We can use <tt>content_for</tt> to include this Javascript on our special page without fattening up the rest of the site.</p>
|
775
|
+
<p><strong>app/views/layouts/application.html.erb</strong></p>
|
776
|
+
<div class="code_container">
|
777
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
778
|
+
<html>
|
779
|
+
<head>
|
780
|
+
<title>Welcome!</title>
|
781
|
+
<%= yield :special_script %>
|
782
|
+
</head>
|
783
|
+
<body>
|
784
|
+
<p>Welcome! The date and time is <%= Time.now %></p>
|
785
|
+
</body>
|
786
|
+
</html>
|
787
|
+
</pre>
|
788
|
+
</div>
|
789
|
+
<p><strong>app/views/posts/special.html.erb</strong></p>
|
790
|
+
<div class="code_container">
|
791
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
792
|
+
<p>This is a special page.</p>
|
793
|
+
|
794
|
+
<% content_for :special_script do %>
|
795
|
+
<script type="text/javascript">alert('Hello!')</script>
|
796
|
+
<% end %>
|
797
|
+
</pre>
|
798
|
+
</div>
|
799
|
+
<h4 id="datehelper">8.8 DateHelper</h4>
|
800
|
+
<h5 id="date_select">8.8.1 date_select</h5>
|
801
|
+
<p>Returns a set of select tags (one for year, month, and day) pre-selected for accessing a specified date-based attribute.</p>
|
802
|
+
<div class="code_container">
|
803
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
804
|
+
date_select("post", "published_on")
|
805
|
+
</pre>
|
806
|
+
</div>
|
807
|
+
<h5 id="datetime_select">8.8.2 datetime_select</h5>
|
808
|
+
<p>Returns a set of select tags (one for year, month, day, hour, and minute) pre-selected for accessing a specified datetime-based attribute.</p>
|
809
|
+
<div class="code_container">
|
810
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
811
|
+
datetime_select("post", "published_on")
|
812
|
+
</pre>
|
813
|
+
</div>
|
814
|
+
<h5 id="distance_of_time_in_words">8.8.3 distance_of_time_in_words</h5>
|
815
|
+
<p>Reports the approximate distance in time between two Time or Date objects or integers as seconds. Set <tt>include_seconds</tt> to true if you want more detailed approximations.</p>
|
816
|
+
<div class="code_container">
|
817
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
818
|
+
distance_of_time_in_words(Time.now, Time.now + 15.seconds) # => less than a minute
|
819
|
+
distance_of_time_in_words(Time.now, Time.now + 15.seconds, true) # => less than 20 seconds
|
820
|
+
</pre>
|
821
|
+
</div>
|
822
|
+
<h5 id="select_date">8.8.4 select_date</h5>
|
823
|
+
<p>Returns a set of html select-tags (one for year, month, and day) pre-selected with the <tt>date</tt> provided.</p>
|
824
|
+
<div class="code_container">
|
825
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
826
|
+
# Generates a date select that defaults to the date provided (six days after today)
|
827
|
+
select_date(Time.today + 6.days)
|
828
|
+
|
829
|
+
# Generates a date select that defaults to today (no specified date)
|
830
|
+
select_date()
|
831
|
+
</pre>
|
832
|
+
</div>
|
833
|
+
<h5 id="select_datetime">8.8.5 select_datetime</h5>
|
834
|
+
<p>Returns a set of html select-tags (one for year, month, day, hour, and minute) pre-selected with the <tt>datetime</tt> provided.</p>
|
835
|
+
<div class="code_container">
|
836
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
837
|
+
# Generates a datetime select that defaults to the datetime provided (four days after today)
|
838
|
+
select_datetime(Time.now + 4.days)
|
839
|
+
|
840
|
+
# Generates a datetime select that defaults to today (no specified datetime)
|
841
|
+
select_datetime()
|
842
|
+
</pre>
|
843
|
+
</div>
|
844
|
+
<h5 id="select_day">8.8.6 select_day</h5>
|
845
|
+
<p>Returns a select tag with options for each of the days 1 through 31 with the current day selected.</p>
|
846
|
+
<div class="code_container">
|
847
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
848
|
+
# Generates a select field for days that defaults to the day for the date provided
|
849
|
+
select_day(Time.today + 2.days)
|
850
|
+
|
851
|
+
# Generates a select field for days that defaults to the number given
|
852
|
+
select_day(5)
|
853
|
+
</pre>
|
854
|
+
</div>
|
855
|
+
<h5 id="select_hour">8.8.7 select_hour</h5>
|
856
|
+
<p>Returns a select tag with options for each of the hours 0 through 23 with the current hour selected.</p>
|
857
|
+
<div class="code_container">
|
858
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
859
|
+
# Generates a select field for minutes that defaults to the minutes for the time provided
|
860
|
+
select_minute(Time.now + 6.hours)
|
861
|
+
</pre>
|
862
|
+
</div>
|
863
|
+
<h5 id="select_minute">8.8.8 select_minute</h5>
|
864
|
+
<p>Returns a select tag with options for each of the minutes 0 through 59 with the current minute selected.</p>
|
865
|
+
<div class="code_container">
|
866
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
867
|
+
# Generates a select field for minutes that defaults to the minutes for the time provided.
|
868
|
+
select_minute(Time.now + 6.hours)
|
869
|
+
</pre>
|
870
|
+
</div>
|
871
|
+
<h5 id="select_month">8.8.9 select_month</h5>
|
872
|
+
<p>Returns a select tag with options for each of the months January through December with the current month selected.</p>
|
873
|
+
<div class="code_container">
|
874
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
875
|
+
# Generates a select field for months that defaults to the current month
|
876
|
+
select_month(Date.today)
|
877
|
+
</pre>
|
878
|
+
</div>
|
879
|
+
<h5 id="select_second">8.8.10 select_second</h5>
|
880
|
+
<p>Returns a select tag with options for each of the seconds 0 through 59 with the current second selected.</p>
|
881
|
+
<div class="code_container">
|
882
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
883
|
+
# Generates a select field for seconds that defaults to the seconds for the time provided
|
884
|
+
select_second(Time.now + 16.minutes)
|
885
|
+
</pre>
|
886
|
+
</div>
|
887
|
+
<h5 id="select_time">8.8.11 select_time</h5>
|
888
|
+
<p>Returns a set of html select-tags (one for hour and minute).</p>
|
889
|
+
<div class="code_container">
|
890
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
891
|
+
# Generates a time select that defaults to the time provided
|
892
|
+
select_time(Time.now)
|
893
|
+
</pre>
|
894
|
+
</div>
|
895
|
+
<h5 id="select_year">8.8.12 select_year</h5>
|
896
|
+
<p>Returns a select tag with options for each of the five years on each side of the current, which is selected. The five year radius can be changed using the <tt>:start_year</tt> and <tt>:end_year</tt> keys in the <tt>options</tt>.</p>
|
897
|
+
<div class="code_container">
|
898
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
899
|
+
# Generates a select field for five years on either side of Date.today that defaults to the current year
|
900
|
+
select_year(Date.today)
|
901
|
+
|
902
|
+
# Generates a select field from 1900 to 2009 that defaults to the current year
|
903
|
+
select_year(Date.today, :start_year => 1900, :end_year => 2009)
|
904
|
+
</pre>
|
905
|
+
</div>
|
906
|
+
<h5 id="time_ago_in_words">8.8.13 time_ago_in_words</h5>
|
907
|
+
<p>Like <tt>distance_of_time_in_words</tt>, but where <tt>to_time</tt> is fixed to <tt>Time.now</tt>.</p>
|
908
|
+
<div class="code_container">
|
909
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
910
|
+
time_ago_in_words(3.minutes.from_now) # => 3 minutes
|
911
|
+
</pre>
|
912
|
+
</div>
|
913
|
+
<h5 id="time_select">8.8.14 time_select</h5>
|
914
|
+
<p>Returns a set of select tags (one for hour, minute and optionally second) pre-selected for accessing a specified time-based attribute. The selects are prepared for multi-parameter assignment to an Active Record object.</p>
|
915
|
+
<div class="code_container">
|
916
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
917
|
+
# Creates a time select tag that, when POSTed, will be stored in the order variable in the submitted attribute
|
918
|
+
time_select("order", "submitted")
|
919
|
+
</pre>
|
920
|
+
</div>
|
921
|
+
<h4 id="debughelper">8.9 DebugHelper</h4>
|
922
|
+
<p>Returns a <tt>pre</tt> tag that has object dumped by <span class="caps">YAML</span>. This creates a very readable way to inspect an object.</p>
|
923
|
+
<div class="code_container">
|
924
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
925
|
+
my_hash = {'first' => 1, 'second' => 'two', 'third' => [1,2,3]}
|
926
|
+
debug(my_hash)
|
927
|
+
</pre>
|
928
|
+
</div>
|
929
|
+
<div class="code_container">
|
930
|
+
<pre class="brush: xml; gutter: false; toolbar: false">
|
931
|
+
<pre class='debug_dump'>---
|
932
|
+
first: 1
|
933
|
+
second: two
|
934
|
+
third:
|
935
|
+
- 1
|
936
|
+
- 2
|
937
|
+
- 3
|
938
|
+
</pre>
|
939
|
+
</pre>
|
940
|
+
</div>
|
941
|
+
<h4 id="formhelper">8.10 FormHelper</h4>
|
942
|
+
<p>Form helpers are designed to make working with models much easier compared to using just standard <span class="caps">HTML</span> elements by providing a set of methods for creating forms based on your models. This helper generates the <span class="caps">HTML</span> for forms, providing a method for each sort of input (e.g., text, password, select, and so on). When the form is submitted (i.e., when the user hits the submit button or form.submit is called via JavaScript), the form inputs will be bundled into the params object and passed back to the controller.</p>
|
943
|
+
<p>There are two types of form helpers: those that specifically work with model attributes and those that don’t. This helper deals with those that work with model attributes; to see an example of form helpers that don’t work with model attributes, check the ActionView::Helpers::FormTagHelper documentation.</p>
|
944
|
+
<p>The core method of this helper, form_for, gives you the ability to create a form for a model instance; for example, let’s say that you have a model Person and want to create a new instance of it:</p>
|
945
|
+
<div class="code_container">
|
946
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
947
|
+
# Note: a @person variable will have been created in the controller (e.g. @person = Person.new)
|
948
|
+
<%= form_for @person, :url => { :action => "create" } do |f| %>
|
949
|
+
<%= f.text_field :first_name %>
|
950
|
+
<%= f.text_field :last_name %>
|
951
|
+
<%= submit_tag 'Create' %>
|
952
|
+
<% end %>
|
953
|
+
</pre>
|
954
|
+
</div>
|
955
|
+
<p>The <span class="caps">HTML</span> generated for this would be:</p>
|
956
|
+
<div class="code_container">
|
957
|
+
<pre class="brush: xml; gutter: false; toolbar: false">
|
958
|
+
<form action="/persons/create" method="post">
|
959
|
+
<input id="person_first_name" name="person[first_name]" size="30" type="text" />
|
960
|
+
<input id="person_last_name" name="person[last_name]" size="30" type="text" />
|
961
|
+
<input name="commit" type="submit" value="Create" />
|
962
|
+
</form>
|
963
|
+
</pre>
|
964
|
+
</div>
|
965
|
+
<p>The params object created when this form is submitted would look like:</p>
|
966
|
+
<div class="code_container">
|
967
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
968
|
+
{"action"=>"create", "controller"=>"persons", "person"=>{"first_name"=>"William", "last_name"=>"Smith"}}
|
969
|
+
</pre>
|
970
|
+
</div>
|
971
|
+
<p>The params hash has a nested person value, which can therefore be accessed with params[:person] in the controller.</p>
|
972
|
+
<h5 id="check_box">8.10.1 check_box</h5>
|
973
|
+
<p>Returns a checkbox tag tailored for accessing a specified attribute.</p>
|
974
|
+
<div class="code_container">
|
975
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
976
|
+
# Let's say that @post.validated? is 1:
|
977
|
+
check_box("post", "validated")
|
978
|
+
# => <input type="checkbox" id="post_validated" name="post[validated]" value="1" />
|
979
|
+
# <input name="post[validated]" type="hidden" value="0" />
|
980
|
+
</pre>
|
981
|
+
</div>
|
982
|
+
<h5 id="fields_for">8.10.2 fields_for</h5>
|
983
|
+
<p>Creates a scope around a specific model object like form_for, but doesn’t create the form tags themselves. This makes fields_for suitable for specifying additional model objects in the same form:</p>
|
984
|
+
<div class="code_container">
|
985
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
986
|
+
<%= form_for @person, :url => { :action => "update" } do |person_form| %>
|
987
|
+
First name: <%= person_form.text_field :first_name %>
|
988
|
+
Last name : <%= person_form.text_field :last_name %>
|
989
|
+
|
990
|
+
<%= fields_for @person.permission do |permission_fields| %>
|
991
|
+
Admin? : <%= permission_fields.check_box :admin %>
|
992
|
+
<% end %>
|
993
|
+
<% end %>
|
994
|
+
</pre>
|
995
|
+
</div>
|
996
|
+
<h5 id="file_field">8.10.3 file_field</h5>
|
997
|
+
<p>Returns a file upload input tag tailored for accessing a specified attribute.</p>
|
998
|
+
<div class="code_container">
|
999
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
1000
|
+
file_field(:user, :avatar)
|
1001
|
+
# => <input type="file" id="user_avatar" name="user[avatar]" />
|
1002
|
+
</pre>
|
1003
|
+
</div>
|
1004
|
+
<h5 id="form_for">8.10.4 form_for</h5>
|
1005
|
+
<p>Creates a form and a scope around a specific model object that is used as a base for questioning about values for the fields.</p>
|
1006
|
+
<div class="code_container">
|
1007
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
1008
|
+
<%= form_for @post do |f| %>
|
1009
|
+
<%= f.label :title, 'Title' %>:
|
1010
|
+
<%= f.text_field :title %><br />
|
1011
|
+
<%= f.label :body, 'Body' %>:
|
1012
|
+
<%= f.text_area :body %><br />
|
1013
|
+
<% end %>
|
1014
|
+
</pre>
|
1015
|
+
</div>
|
1016
|
+
<h5 id="hidden_field">8.10.5 hidden_field</h5>
|
1017
|
+
<p>Returns a hidden input tag tailored for accessing a specified attribute.</p>
|
1018
|
+
<div class="code_container">
|
1019
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
1020
|
+
hidden_field(:user, :token)
|
1021
|
+
# => <input type="hidden" id="user_token" name="user[token]" value="#{@user.token}" />
|
1022
|
+
</pre>
|
1023
|
+
</div>
|
1024
|
+
<h5 id="label">8.10.6 label</h5>
|
1025
|
+
<p>Returns a label tag tailored for labelling an input field for a specified attribute.</p>
|
1026
|
+
<div class="code_container">
|
1027
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
1028
|
+
label(:post, :title)
|
1029
|
+
# => <label for="post_title">Title</label>
|
1030
|
+
</pre>
|
1031
|
+
</div>
|
1032
|
+
<h5 id="password_field">8.10.7 password_field</h5>
|
1033
|
+
<p>Returns an input tag of the “password” type tailored for accessing a specified attribute.</p>
|
1034
|
+
<div class="code_container">
|
1035
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
1036
|
+
password_field(:login, :pass)
|
1037
|
+
# => <input type="text" id="login_pass" name="login[pass]" value="#{@login.pass}" />
|
1038
|
+
</pre>
|
1039
|
+
</div>
|
1040
|
+
<h5 id="radio_button">8.10.8 radio_button</h5>
|
1041
|
+
<p>Returns a radio button tag for accessing a specified attribute.</p>
|
1042
|
+
<div class="code_container">
|
1043
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
1044
|
+
# Let's say that @post.category returns "rails":
|
1045
|
+
radio_button("post", "category", "rails")
|
1046
|
+
radio_button("post", "category", "java")
|
1047
|
+
# => <input type="radio" id="post_category_rails" name="post[category]" value="rails" checked="checked" />
|
1048
|
+
# <input type="radio" id="post_category_java" name="post[category]" value="java" />
|
1049
|
+
</pre>
|
1050
|
+
</div>
|
1051
|
+
<h5 id="text_area">8.10.9 text_area</h5>
|
1052
|
+
<p>Returns a textarea opening and closing tag set tailored for accessing a specified attribute.</p>
|
1053
|
+
<div class="code_container">
|
1054
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
1055
|
+
text_area(:comment, :text, :size => "20x30")
|
1056
|
+
# => <textarea cols="20" rows="30" id="comment_text" name="comment[text]">
|
1057
|
+
# #{@comment.text}
|
1058
|
+
# </textarea>
|
1059
|
+
</pre>
|
1060
|
+
</div>
|
1061
|
+
<h5 id="text_field">8.10.10 text_field</h5>
|
1062
|
+
<p>Returns an input tag of the “text” type tailored for accessing a specified attribute.</p>
|
1063
|
+
<div class="code_container">
|
1064
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
1065
|
+
text_field(:post, :title)
|
1066
|
+
# => <input type="text" id="post_title" name="post[title]" value="#{@post.title}" />
|
1067
|
+
</pre>
|
1068
|
+
</div>
|
1069
|
+
<h4 id="formoptionshelper">8.11 FormOptionsHelper</h4>
|
1070
|
+
<p>Provides a number of methods for turning different kinds of containers into a set of option tags.</p>
|
1071
|
+
<h5 id="collection_select">8.11.1 collection_select</h5>
|
1072
|
+
<p>Returns <tt>select</tt> and <tt>option</tt> tags for the collection of existing return values of <tt>method</tt> for <tt>object</tt>’s class.</p>
|
1073
|
+
<p>Example object structure for use with this method:</p>
|
1074
|
+
<div class="code_container">
|
1075
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
1076
|
+
class Post < ActiveRecord::Base
|
1077
|
+
belongs_to :author
|
1078
|
+
end
|
1079
|
+
|
1080
|
+
class Author < ActiveRecord::Base
|
1081
|
+
has_many :posts
|
1082
|
+
def name_with_initial
|
1083
|
+
"#{first_name.first}. #{last_name}"
|
1084
|
+
end
|
1085
|
+
end
|
1086
|
+
</pre>
|
1087
|
+
</div>
|
1088
|
+
<p>Sample usage (selecting the associated Author for an instance of Post, <tt>@post</tt>):</p>
|
1089
|
+
<div class="code_container">
|
1090
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
1091
|
+
collection_select(:post, :author_id, Author.all, :id, :name_with_initial, {:prompt => true})
|
1092
|
+
</pre>
|
1093
|
+
</div>
|
1094
|
+
<p>If <tt>@post.author_id</tt> is 1, this would return:</p>
|
1095
|
+
<div class="code_container">
|
1096
|
+
<pre class="brush: xml; gutter: false; toolbar: false">
|
1097
|
+
<select name="post[author_id]">
|
1098
|
+
<option value="">Please select</option>
|
1099
|
+
<option value="1" selected="selected">D. Heinemeier Hansson</option>
|
1100
|
+
<option value="2">D. Thomas</option>
|
1101
|
+
<option value="3">M. Clark</option>
|
1102
|
+
</select>
|
1103
|
+
</pre>
|
1104
|
+
</div>
|
1105
|
+
<h5 id="country_options_for_select">8.11.2 country_options_for_select</h5>
|
1106
|
+
<p>Returns a string of option tags for pretty much any country in the world.</p>
|
1107
|
+
<h5 id="country_select">8.11.3 country_select</h5>
|
1108
|
+
<p>Return select and option tags for the given object and method, using country_options_for_select to generate the list of option tags.</p>
|
1109
|
+
<h5 id="option_groups_from_collection_for_select">8.11.4 option_groups_from_collection_for_select</h5>
|
1110
|
+
<p>Returns a string of <tt>option</tt> tags, like <tt>options_from_collection_for_select</tt>, but groups them by <tt>optgroup</tt> tags based on the object relationships of the arguments.</p>
|
1111
|
+
<p>Example object structure for use with this method:</p>
|
1112
|
+
<div class="code_container">
|
1113
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
1114
|
+
class Continent < ActiveRecord::Base
|
1115
|
+
has_many :countries
|
1116
|
+
# attribs: id, name
|
1117
|
+
end
|
1118
|
+
|
1119
|
+
class Country < ActiveRecord::Base
|
1120
|
+
belongs_to :continent
|
1121
|
+
# attribs: id, name, continent_id
|
1122
|
+
end
|
1123
|
+
</pre>
|
1124
|
+
</div>
|
1125
|
+
<p>Sample usage:</p>
|
1126
|
+
<div class="code_container">
|
1127
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
1128
|
+
option_groups_from_collection_for_select(@continents, :countries, :name, :id, :name, 3)
|
1129
|
+
</pre>
|
1130
|
+
</div>
|
1131
|
+
<p>Possible output:</p>
|
1132
|
+
<div class="code_container">
|
1133
|
+
<pre class="brush: xml; gutter: false; toolbar: false">
|
1134
|
+
<optgroup label="Africa">
|
1135
|
+
<option value="1">Egypt</option>
|
1136
|
+
<option value="4">Rwanda</option>
|
1137
|
+
...
|
1138
|
+
</optgroup>
|
1139
|
+
<optgroup label="Asia">
|
1140
|
+
<option value="3" selected="selected">China</option>
|
1141
|
+
<option value="12">India</option>
|
1142
|
+
<option value="5">Japan</option>
|
1143
|
+
...
|
1144
|
+
</optgroup>
|
1145
|
+
</pre>
|
1146
|
+
</div>
|
1147
|
+
<p>Note: Only the <tt>optgroup</tt> and <tt>option</tt> tags are returned, so you still have to wrap the output in an appropriate <tt>select</tt> tag.</p>
|
1148
|
+
<h5 id="options_for_select">8.11.5 options_for_select</h5>
|
1149
|
+
<p>Accepts a container (hash, array, enumerable, your type) and returns a string of option tags.</p>
|
1150
|
+
<div class="code_container">
|
1151
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
1152
|
+
options_for_select([ "VISA", "MasterCard" ])
|
1153
|
+
# => <option>VISA</option> <option>MasterCard</option>
|
1154
|
+
</pre>
|
1155
|
+
</div>
|
1156
|
+
<p>Note: Only the <tt>option</tt> tags are returned, you have to wrap this call in a regular <span class="caps">HTML</span> <tt>select</tt> tag.</p>
|
1157
|
+
<h5 id="options_from_collection_for_select">8.11.6 options_from_collection_for_select</h5>
|
1158
|
+
<p>Returns a string of option tags that have been compiled by iterating over the <tt>collection</tt> and assigning the result of a call to the <tt>value_method</tt> as the option value and the <tt>text_method</tt> as the option text.</p>
|
1159
|
+
<div class="code_container">
|
1160
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
1161
|
+
# options_from_collection_for_select(collection, value_method, text_method, selected = nil)
|
1162
|
+
</pre>
|
1163
|
+
</div>
|
1164
|
+
<p>For example, imagine a loop iterating over each person in @project.people to generate an input tag:</p>
|
1165
|
+
<div class="code_container">
|
1166
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
1167
|
+
options_from_collection_for_select(@project.people, "id", "name")
|
1168
|
+
# => <option value="#{person.id}">#{person.name}</option>
|
1169
|
+
</pre>
|
1170
|
+
</div>
|
1171
|
+
<p>Note: Only the <tt>option</tt> tags are returned, you have to wrap this call in a regular <span class="caps">HTML</span> <tt>select</tt> tag.</p>
|
1172
|
+
<h5 id="select">8.11.7 select</h5>
|
1173
|
+
<p>Create a select tag and a series of contained option tags for the provided object and method.</p>
|
1174
|
+
<p>Example:</p>
|
1175
|
+
<div class="code_container">
|
1176
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
1177
|
+
select("post", "person_id", Person.all.collect {|p| [ p.name, p.id ] }, { :include_blank => true })
|
1178
|
+
</pre>
|
1179
|
+
</div>
|
1180
|
+
<p>If <tt>@post.person_id</tt> is 1, this would become:</p>
|
1181
|
+
<div class="code_container">
|
1182
|
+
<pre class="brush: xml; gutter: false; toolbar: false">
|
1183
|
+
<select name="post[person_id]">
|
1184
|
+
<option value=""></option>
|
1185
|
+
<option value="1" selected="selected">David</option>
|
1186
|
+
<option value="2">Sam</option>
|
1187
|
+
<option value="3">Tobias</option>
|
1188
|
+
</select>
|
1189
|
+
</pre>
|
1190
|
+
</div>
|
1191
|
+
<h5 id="time_zone_options_for_select">8.11.8 time_zone_options_for_select</h5>
|
1192
|
+
<p>Returns a string of option tags for pretty much any time zone in the world.</p>
|
1193
|
+
<h5 id="time_zone_select">8.11.9 time_zone_select</h5>
|
1194
|
+
<p>Return select and option tags for the given object and method, using <tt>time_zone_options_for_select</tt> to generate the list of option tags.</p>
|
1195
|
+
<div class="code_container">
|
1196
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
1197
|
+
time_zone_select( "user", "time_zone")
|
1198
|
+
</pre>
|
1199
|
+
</div>
|
1200
|
+
<h4 id="formtaghelper">8.12 FormTagHelper</h4>
|
1201
|
+
<p>Provides a number of methods for creating form tags that doesn’t rely on an Active Record object assigned to the template like FormHelper does. Instead, you provide the names and values manually.</p>
|
1202
|
+
<h5 id="check_box_tag">8.12.1 check_box_tag</h5>
|
1203
|
+
<p>Creates a check box form input tag.</p>
|
1204
|
+
<div class="code_container">
|
1205
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
1206
|
+
check_box_tag 'accept'
|
1207
|
+
# => <input id="accept" name="accept" type="checkbox" value="1" />
|
1208
|
+
</pre>
|
1209
|
+
</div>
|
1210
|
+
<h5 id="field_set_tag">8.12.2 field_set_tag</h5>
|
1211
|
+
<p>Creates a field set for grouping <span class="caps">HTML</span> form elements.</p>
|
1212
|
+
<div class="code_container">
|
1213
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
1214
|
+
<%= field_set_tag do %>
|
1215
|
+
<p><%= text_field_tag 'name' %></p>
|
1216
|
+
<% end %>
|
1217
|
+
# => <fieldset><p><input id="name" name="name" type="text" /></p></fieldset>
|
1218
|
+
</pre>
|
1219
|
+
</div>
|
1220
|
+
<h5 id="file_field_tag">8.12.3 file_field_tag</h5>
|
1221
|
+
<p>Creates a file upload field.</p>
|
1222
|
+
<p>Prior to Rails 3.1, if you are using file uploads, then you will need to set the multipart option for the form tag. Rails 3.1+ does this automatically.</p>
|
1223
|
+
<div class="code_container">
|
1224
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
1225
|
+
<%= form_tag { :action => "post" }, { :multipart => true } do %>
|
1226
|
+
<label for="file">File to Upload</label> <%= file_field_tag "file" %>
|
1227
|
+
<%= submit_tag %>
|
1228
|
+
<% end %>
|
1229
|
+
</pre>
|
1230
|
+
</div>
|
1231
|
+
<p>Example output:</p>
|
1232
|
+
<div class="code_container">
|
1233
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
1234
|
+
file_field_tag 'attachment'
|
1235
|
+
# => <input id="attachment" name="attachment" type="file" />
|
1236
|
+
</pre>
|
1237
|
+
</div>
|
1238
|
+
<h5 id="form_tag">8.12.4 form_tag</h5>
|
1239
|
+
<p>Starts a form tag that points the action to an url configured with <tt>url_for_options</tt> just like <tt>ActionController::Base#url_for</tt>.</p>
|
1240
|
+
<div class="code_container">
|
1241
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
1242
|
+
<%= form_tag '/posts' do %>
|
1243
|
+
<div><%= submit_tag 'Save' %></div>
|
1244
|
+
<% end %>
|
1245
|
+
# => <form action="/posts" method="post"><div><input type="submit" name="submit" value="Save" /></div></form>
|
1246
|
+
</pre>
|
1247
|
+
</div>
|
1248
|
+
<h5 id="hidden_field_tag">8.12.5 hidden_field_tag</h5>
|
1249
|
+
<p>Creates a hidden form input field used to transmit data that would be lost due to HTTP’s statelessness or data that should be hidden from the user.</p>
|
1250
|
+
<div class="code_container">
|
1251
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
1252
|
+
hidden_field_tag 'token', 'VUBJKB23UIVI1UU1VOBVI@'
|
1253
|
+
# => <input id="token" name="token" type="hidden" value="VUBJKB23UIVI1UU1VOBVI@" />
|
1254
|
+
</pre>
|
1255
|
+
</div>
|
1256
|
+
<h5 id="image_submit_tag">8.12.6 image_submit_tag</h5>
|
1257
|
+
<p>Displays an image which when clicked will submit the form.</p>
|
1258
|
+
<div class="code_container">
|
1259
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
1260
|
+
image_submit_tag("login.png")
|
1261
|
+
# => <input src="/images/login.png" type="image" />
|
1262
|
+
</pre>
|
1263
|
+
</div>
|
1264
|
+
<h5 id="label_tag">8.12.7 label_tag</h5>
|
1265
|
+
<p>Creates a label field.</p>
|
1266
|
+
<div class="code_container">
|
1267
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
1268
|
+
label_tag 'name'
|
1269
|
+
# => <label for="name">Name</label>
|
1270
|
+
</pre>
|
1271
|
+
</div>
|
1272
|
+
<h5 id="password_field_tag">8.12.8 password_field_tag</h5>
|
1273
|
+
<p>Creates a password field, a masked text field that will hide the users input behind a mask character.</p>
|
1274
|
+
<div class="code_container">
|
1275
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
1276
|
+
password_field_tag 'pass'
|
1277
|
+
# => <input id="pass" name="pass" type="password" />
|
1278
|
+
</pre>
|
1279
|
+
</div>
|
1280
|
+
<h5 id="radio_button_tag">8.12.9 radio_button_tag</h5>
|
1281
|
+
<p>Creates a radio button; use groups of radio buttons named the same to allow users to select from a group of options.</p>
|
1282
|
+
<div class="code_container">
|
1283
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
1284
|
+
radio_button_tag 'gender', 'male'
|
1285
|
+
# => <input id="gender_male" name="gender" type="radio" value="male" />
|
1286
|
+
</pre>
|
1287
|
+
</div>
|
1288
|
+
<h5 id="select_tag">8.12.10 select_tag</h5>
|
1289
|
+
<p>Creates a dropdown selection box.</p>
|
1290
|
+
<div class="code_container">
|
1291
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
1292
|
+
select_tag "people", "<option>David</option>"
|
1293
|
+
# => <select id="people" name="people"><option>David</option></select>
|
1294
|
+
</pre>
|
1295
|
+
</div>
|
1296
|
+
<h5 id="submit_tag">8.12.11 submit_tag</h5>
|
1297
|
+
<p>Creates a submit button with the text provided as the caption.</p>
|
1298
|
+
<div class="code_container">
|
1299
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
1300
|
+
submit_tag "Publish this post"
|
1301
|
+
# => <input name="commit" type="submit" value="Publish this post" />
|
1302
|
+
</pre>
|
1303
|
+
</div>
|
1304
|
+
<h5 id="text_area_tag">8.12.12 text_area_tag</h5>
|
1305
|
+
<p>Creates a text input area; use a textarea for longer text inputs such as blog posts or descriptions.</p>
|
1306
|
+
<div class="code_container">
|
1307
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
1308
|
+
text_area_tag 'post'
|
1309
|
+
# => <textarea id="post" name="post"></textarea>
|
1310
|
+
</pre>
|
1311
|
+
</div>
|
1312
|
+
<h5 id="text_field_tag">8.12.13 text_field_tag</h5>
|
1313
|
+
<p>Creates a standard text field; use these text fields to input smaller chunks of text like a username or a search query.</p>
|
1314
|
+
<div class="code_container">
|
1315
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
1316
|
+
text_field_tag 'name'
|
1317
|
+
# => <input id="name" name="name" type="text" />
|
1318
|
+
</pre>
|
1319
|
+
</div>
|
1320
|
+
<h4 id="javascripthelper">8.13 JavaScriptHelper</h4>
|
1321
|
+
<p>Provides functionality for working with JavaScript in your views.</p>
|
1322
|
+
<h5 id="button_to_function">8.13.1 button_to_function</h5>
|
1323
|
+
<p>Returns a button that’ll trigger a JavaScript function using the onclick handler. Examples:</p>
|
1324
|
+
<div class="code_container">
|
1325
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
1326
|
+
button_to_function "Greeting", "alert('Hello world!')"
|
1327
|
+
button_to_function "Delete", "if (confirm('Really?')) do_delete()"
|
1328
|
+
button_to_function "Details" do |page|
|
1329
|
+
page[:details].visual_effect :toggle_slide
|
1330
|
+
end
|
1331
|
+
</pre>
|
1332
|
+
</div>
|
1333
|
+
<h5 id="define_javascript_functions">8.13.2 define_javascript_functions</h5>
|
1334
|
+
<p>Includes the Action Pack JavaScript libraries inside a single <tt>script</tt> tag.</p>
|
1335
|
+
<h5 id="escape_javascript">8.13.3 escape_javascript</h5>
|
1336
|
+
<p>Escape carrier returns and single and double quotes for JavaScript segments.</p>
|
1337
|
+
<h5 id="javascript_tag">8.13.4 javascript_tag</h5>
|
1338
|
+
<p>Returns a JavaScript tag wrapping the provided code.</p>
|
1339
|
+
<div class="code_container">
|
1340
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
1341
|
+
javascript_tag "alert('All is good')"
|
1342
|
+
</pre>
|
1343
|
+
</div>
|
1344
|
+
<div class="code_container">
|
1345
|
+
<pre class="brush: xml; gutter: false; toolbar: false">
|
1346
|
+
<script type="text/javascript">
|
1347
|
+
//<![CDATA[
|
1348
|
+
alert('All is good')
|
1349
|
+
//]]>
|
1350
|
+
</script>
|
1351
|
+
</pre>
|
1352
|
+
</div>
|
1353
|
+
<h5 id="link_to_function">8.13.5 link_to_function</h5>
|
1354
|
+
<p>Returns a link that will trigger a JavaScript function using the onclick handler and return false after the fact.</p>
|
1355
|
+
<div class="code_container">
|
1356
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
1357
|
+
link_to_function "Greeting", "alert('Hello world!')"
|
1358
|
+
# => <a onclick="alert('Hello world!'); return false;" href="#">Greeting</a>
|
1359
|
+
</pre>
|
1360
|
+
</div>
|
1361
|
+
<h4 id="numberhelper">8.14 NumberHelper</h4>
|
1362
|
+
<p>Provides methods for converting numbers into formatted strings. Methods are provided for phone numbers, currency, percentage, precision, positional notation, and file size.</p>
|
1363
|
+
<h5 id="number_to_currency">8.14.1 number_to_currency</h5>
|
1364
|
+
<p>Formats a number into a currency string (e.g., $13.65).</p>
|
1365
|
+
<div class="code_container">
|
1366
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
1367
|
+
number_to_currency(1234567890.50) # => $1,234,567,890.50
|
1368
|
+
</pre>
|
1369
|
+
</div>
|
1370
|
+
<h5 id="number_to_human_size">8.14.2 number_to_human_size</h5>
|
1371
|
+
<p>Formats the bytes in size into a more understandable representation; useful for reporting file sizes to users.</p>
|
1372
|
+
<div class="code_container">
|
1373
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
1374
|
+
number_to_human_size(1234) # => 1.2 KB
|
1375
|
+
number_to_human_size(1234567) # => 1.2 MB
|
1376
|
+
</pre>
|
1377
|
+
</div>
|
1378
|
+
<h5 id="number_to_percentage">8.14.3 number_to_percentage</h5>
|
1379
|
+
<p>Formats a number as a percentage string.</p>
|
1380
|
+
<div class="code_container">
|
1381
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
1382
|
+
number_to_percentage(100, :precision => 0) # => 100%
|
1383
|
+
</pre>
|
1384
|
+
</div>
|
1385
|
+
<h5 id="number_to_phone">8.14.4 number_to_phone</h5>
|
1386
|
+
<p>Formats a number into a US phone number.</p>
|
1387
|
+
<div class="code_container">
|
1388
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
1389
|
+
number_to_phone(1235551234) # => 123-555-1234
|
1390
|
+
</pre>
|
1391
|
+
</div>
|
1392
|
+
<h5 id="number_with_delimiter">8.14.5 number_with_delimiter</h5>
|
1393
|
+
<p>Formats a number with grouped thousands using a delimiter.</p>
|
1394
|
+
<div class="code_container">
|
1395
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
1396
|
+
number_with_delimiter(12345678) # => 12,345,678
|
1397
|
+
</pre>
|
1398
|
+
</div>
|
1399
|
+
<h5 id="number_with_precision">8.14.6 number_with_precision</h5>
|
1400
|
+
<p>Formats a number with the specified level of <tt>precision</tt>, which defaults to 3.</p>
|
1401
|
+
<div class="code_container">
|
1402
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
1403
|
+
number_with_precision(111.2345) # => 111.235
|
1404
|
+
number_with_precision(111.2345, 2) # => 111.23
|
1405
|
+
</pre>
|
1406
|
+
</div>
|
1407
|
+
<h3 id="localized-views">9 Localized Views</h3>
|
1408
|
+
<p>Action View has the ability render different templates depending on the current locale.</p>
|
1409
|
+
<p>For example, suppose you have a Posts controller with a show action. By default, calling this action will render <tt>app/views/posts/show.html.erb</tt>. But if you set <tt>I18n.locale = :de</tt>, then <tt>app/views/posts/show.de.html.erb</tt> will be rendered instead. If the localized template isn’t present, the undecorated version will be used. This means you’re not required to provide localized views for all cases, but they will be preferred and used if available.</p>
|
1410
|
+
<p>You can use the same technique to localize the rescue files in your public directory. For example, setting <tt>I18n.locale = :de</tt> and creating <tt>public/500.de.html</tt> and <tt>public/404.de.html</tt> would allow you to have localized rescue pages.</p>
|
1411
|
+
<p>Since Rails doesn’t restrict the symbols that you use to set I18n.locale, you can leverage this system to display different content depending on anything you like. For example, suppose you have some “expert” users that should see different pages from “normal” users. You could add the following to <tt>app/controllers/application.rb</tt>:</p>
|
1412
|
+
<div class="code_container">
|
1413
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
1414
|
+
before_filter :set_expert_locale
|
1415
|
+
|
1416
|
+
def set_expert_locale
|
1417
|
+
I18n.locale = :expert if current_user.expert?
|
1418
|
+
end
|
1419
|
+
</pre>
|
1420
|
+
</div>
|
1421
|
+
<p>Then you could create special views like <tt>app/views/posts/show.expert.html.erb</tt> that would only be displayed to expert users.</p>
|
1422
|
+
<p>You can read more about the Rails Internationalization (I18n) <span class="caps">API</span> <a href="i18n.html">here</a>.</p>
|
1423
|
+
|
1424
|
+
<h3>Feedback</h3>
|
1425
|
+
<p>
|
1426
|
+
You're encouraged to help improve the quality of this guide.
|
1427
|
+
</p>
|
1428
|
+
<p>
|
1429
|
+
If you see any typos or factual errors you are confident to
|
1430
|
+
patch, please clone <a href="https://github.com/lifo/docrails">docrails</a>
|
1431
|
+
and push the change yourself. That branch of Rails has public write access.
|
1432
|
+
Commits are still reviewed, but that happens after you've submitted your
|
1433
|
+
contribution. <a href="https://github.com/lifo/docrails">docrails</a> is
|
1434
|
+
cross-merged with master periodically.
|
1435
|
+
</p>
|
1436
|
+
<p>
|
1437
|
+
You may also find incomplete content, or stuff that is not up to date.
|
1438
|
+
Please do add any missing documentation for master. Check the
|
1439
|
+
<a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a>
|
1440
|
+
for style and conventions.
|
1441
|
+
</p>
|
1442
|
+
<p>
|
1443
|
+
If for whatever reason you spot something to fix but cannot patch it yourself, please
|
1444
|
+
<a href="https://github.com/rails/rails/issues">open an issue</a>.
|
1445
|
+
</p>
|
1446
|
+
<p>And last but not least, any kind of discussion regarding Ruby on Rails
|
1447
|
+
documentation is very welcome in the <a href="http://groups.google.com/group/rubyonrails-docs">rubyonrails-docs mailing list</a>.
|
1448
|
+
</p>
|
1449
|
+
</div>
|
1450
|
+
</div>
|
1451
|
+
</div>
|
1452
|
+
|
1453
|
+
<hr class="hide" />
|
1454
|
+
<div id="footer">
|
1455
|
+
<div class="wrapper">
|
1456
|
+
<p>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</p>
|
1457
|
+
<p>"Rails", "Ruby on Rails", and the Rails logo are trademarks of David Heinemeier Hansson. All rights reserved.</p>
|
1458
|
+
</div>
|
1459
|
+
</div>
|
1460
|
+
|
1461
|
+
<script type="text/javascript" src="javascripts/guides.js"></script>
|
1462
|
+
<script type="text/javascript" src="javascripts/syntaxhighlighter/shCore.js"></script>
|
1463
|
+
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushRuby.js"></script>
|
1464
|
+
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushXml.js"></script>
|
1465
|
+
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushSql.js"></script>
|
1466
|
+
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushPlain.js"></script>
|
1467
|
+
<script type="text/javascript">
|
1468
|
+
SyntaxHighlighter.all()
|
1469
|
+
</script>
|
1470
|
+
</body>
|
1471
|
+
</html>
|