-
Notifications
You must be signed in to change notification settings - Fork 2
/
2_3_release_notes.html
880 lines (806 loc) · 67.3 KB
/
2_3_release_notes.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-CN" lang="zh-CN">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Ruby on Rails 2.3 Release Notes — Ruby on Rails 指南</title>
<link rel="stylesheet" type="text/css" href="stylesheets/style.css" />
<link rel="stylesheet" type="text/css" href="stylesheets/print.css" media="print" />
<link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shCore.css" />
<link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shThemeRailsGuides.css" />
<link rel="stylesheet" type="text/css" href="stylesheets/fixes.css" />
<link href="images/favicon.ico" rel="shortcut icon" type="image/x-icon" />
</head>
<body class="guide">
<div id="topNav">
<div class="wrapper">
<strong class="more-info-label">更多内容 <a href="http://rubyonrails.org/">rubyonrails.org:</a> </strong>
<span class="red-button more-info-button">
更多内容
</span>
<ul class="more-info-links s-hidden">
<li class="more-info"><a href="http://rubyonrails.org/">综览</a></li>
<li class="more-info"><a href="http://rubyonrails.org/download">下载</a></li>
<li class="more-info"><a href="http://rubyonrails.org/deploy">部署</a></li>
<li class="more-info"><a href="https://github.com/rails/rails">源码</a></li>
<li class="more-info"><a href="http://rubyonrails.org/screencasts">视频</a></li>
<li class="more-info"><a href="http://rubyonrails.org/documentation">文件</a></li>
<li class="more-info"><a href="http://rubyonrails.org/community">社群</a></li>
<li class="more-info"><a href="http://weblog.rubyonrails.org/">Blog</a></li>
</ul>
</div>
</div>
<div id="header">
<div class="wrapper clearfix">
<h1><a href="index.html" title="回首页">Guides.rubyonrails.org</a></h1>
<ul class="nav">
<li><a class="nav-item" href="index.html">首页</a></li>
<li class="guides-index guides-index-large">
<a href="index.html" id="guidesMenu" class="guides-index-item nav-item">指南目录</a>
<div id="guides" class="clearfix" style="display: none;">
<hr />
<dl class="L">
<dt>入门</dt>
<dd><a href="getting_started.html">Rails 入门</a></dd>
<dt>模型</dt>
<dd><a href="active_record_basics.html">Active Record 基础</a></dd>
<dd><a href="active_record_migrations.html">Active Record 数据库迁移</a></dd>
<dd><a href="active_record_validations.html">Active Record 数据验证</a></dd>
<dd><a href="active_record_callbacks.html">Active Record 回调</a></dd>
<dd><a href="association_basics.html">Active Record 关联</a></dd>
<dd><a href="active_record_querying.html">Active Record 查询</a></dd>
<dt>视图</dt>
<dd><a href="layouts_and_rendering.html">Rails 布局和视图渲染</a></dd>
<dd><a href="form_helpers.html">Action View 表单帮助方法</a></dd>
<dt>控制器</dt>
<dd><a href="action_controller_overview.html">Action Controller 简介</a></dd>
<dd><a href="routing.html">Rails 路由全解</a></dd>
</dl>
<dl class="R">
<dt>深入</dt>
<dd><a href="active_support_core_extensions.html">Active Support 核心扩展</a></dd>
<dd><a href="i18n.html">Rails 国际化 API</a></dd>
<dd><a href="action_mailer_basics.html">Action Mailer 基础</a></dd>
<dd><a href="active_job_basics.html">Active Job 基础</a></dd>
<dd><a href="security.html">Rails 安全指南</a></dd>
<dd><a href="debugging_rails_applications.html">调试 Rails 程序</a></dd>
<dd><a href="configuring.html">设置 Rails 程序</a></dd>
<dd><a href="command_line.html">Rails 命令行</a></dd>
<dd><a href="asset_pipeline.html">Asset Pipeline</a></dd>
<dd><a href="working_with_javascript_in_rails.html">在 Rails 中使用 JavaScript</a></dd>
<dd><a href="constant_autoloading_and_reloading.html">Constant Autoloading and Reloading</a></dd>
<dt>扩展 Rails</dt>
<dd><a href="rails_on_rack.html">Rails on Rack</a></dd>
<dd><a href="generators.html">客制与新建 Rails 产生器</a></dd>
<dd><a href="rails_application_templates.html">Rails 应用程式模版</a></dd>
<dt>贡献 Ruby on Rails</dt>
<dd><a href="contributing_to_ruby_on_rails.html">贡献 Ruby on Rails</a></dd>
<dd><a href="api_documentation_guidelines.html">API 文件准则</a></dd>
<dd><a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails 指南准则</a></dd>
<dt>维护方针</dt>
<dd><a href="maintenance_policy.html">维护方针</a></dd>
<dt>发布记</dt>
<dd><a href="upgrading_ruby_on_rails.html">升级 Ruby on Rails</a></dd>
<dd><a href="4_2_release_notes.html">Ruby on Rails 4.2 发布记</a></dd>
<dd><a href="4_1_release_notes.html">Ruby on Rails 4.1 发布记</a></dd>
<dd><a href="4_0_release_notes.html">Ruby on Rails 4.0 发布记</a></dd>
<dd><a href="3_2_release_notes.html">Ruby on Rails 3.2 发布记</a></dd>
<dd><a href="3_1_release_notes.html">Ruby on Rails 3.1 发布记</a></dd>
<dd><a href="3_0_release_notes.html">Ruby on Rails 3.0 发布记</a></dd>
<dd><a href="2_3_release_notes.html">Ruby on Rails 2.3 发布记</a></dd>
<dd><a href="2_2_release_notes.html">Ruby on Rails 2.2 发布记</a></dd>
</dl>
</div>
</li>
<!-- <li><a class="nav-item" href="//github.com/docrails-tw/wiki">参与翻译</a></li> -->
<li><a class="nav-item" href="https://github.com/ruby-china/guides/blob/master/CONTRIBUTING.md">贡献</a></li>
<li><a class="nav-item" href="credits.html">致谢</a></li>
<li class="guides-index guides-index-small">
<select class="guides-index-item nav-item">
<option value="index.html">指南目录</option>
<optgroup label="入门">
<option value="getting_started.html">Rails 入门</option>
</optgroup>
<optgroup label="模型">
<option value="active_record_basics.html">Active Record 基础</option>
<option value="active_record_migrations.html">Active Record 数据库迁移</option>
<option value="active_record_validations.html">Active Record 数据验证</option>
<option value="active_record_callbacks.html">Active Record 回调</option>
<option value="association_basics.html">Active Record 关联</option>
<option value="active_record_querying.html">Active Record 查询</option>
</optgroup>
<optgroup label="视图">
<option value="layouts_and_rendering.html">Rails 布局和视图渲染</option>
<option value="form_helpers.html">Action View 表单帮助方法</option>
</optgroup>
<optgroup label="控制器">
<option value="action_controller_overview.html">Action Controller 简介</option>
<option value="routing.html">Rails 路由全解</option>
</optgroup>
<optgroup label="深入">
<option value="active_support_core_extensions.html">Active Support 核心扩展</option>
<option value="i18n.html">Rails 国际化 API</option>
<option value="action_mailer_basics.html">Action Mailer 基础</option>
<option value="active_job_basics.html">Active Job 基础</option>
<option value="security.html">Rails 安全指南</option>
<option value="debugging_rails_applications.html">调试 Rails 程序</option>
<option value="configuring.html">设置 Rails 程序</option>
<option value="command_line.html">Rails 命令行</option>
<option value="asset_pipeline.html">Asset Pipeline</option>
<option value="working_with_javascript_in_rails.html">在 Rails 中使用 JavaScript</option>
<option value="constant_autoloading_and_reloading.html">Constant Autoloading and Reloading</option>
</optgroup>
<optgroup label="扩展 Rails">
<option value="rails_on_rack.html">Rails on Rack</option>
<option value="generators.html">客制与新建 Rails 产生器</option>
<option value="rails_application_templates.html">Rails 应用程式模版</option>
</optgroup>
<optgroup label="贡献 Ruby on Rails">
<option value="contributing_to_ruby_on_rails.html">贡献 Ruby on Rails</option>
<option value="api_documentation_guidelines.html">API 文件准则</option>
<option value="ruby_on_rails_guides_guidelines.html">Ruby on Rails 指南准则</option>
</optgroup>
<optgroup label="维护方针">
<option value="maintenance_policy.html">维护方针</option>
</optgroup>
<optgroup label="发布记">
<option value="upgrading_ruby_on_rails.html">升级 Ruby on Rails</option>
<option value="4_2_release_notes.html">Ruby on Rails 4.2 发布记</option>
<option value="4_1_release_notes.html">Ruby on Rails 4.1 发布记</option>
<option value="4_0_release_notes.html">Ruby on Rails 4.0 发布记</option>
<option value="3_2_release_notes.html">Ruby on Rails 3.2 发布记</option>
<option value="3_1_release_notes.html">Ruby on Rails 3.1 发布记</option>
<option value="3_0_release_notes.html">Ruby on Rails 3.0 发布记</option>
<option value="2_3_release_notes.html">Ruby on Rails 2.3 发布记</option>
<option value="2_2_release_notes.html">Ruby on Rails 2.2 发布记</option>
</optgroup>
</select>
</li>
</ul>
</div>
</div>
</div>
<hr class="hide" />
<div id="feature">
<div class="wrapper">
<h2>Ruby on Rails 2.3 Release Notes</h2><p>Rails 2.3 delivers a variety of new and improved features, including pervasive Rack integration, refreshed support for Rails Engines, nested transactions for Active Record, dynamic and default scopes, unified rendering, more efficient routing, application templates, and quiet backtraces. This list covers the major upgrades, but doesn't include every little bug fix and change. If you want to see everything, check out the <a href="http://github.com/rails/rails/commits/master">list of commits</a> in the main Rails repository on GitHub or review the <code>CHANGELOG</code> files for the individual Rails components.</p>
<div id="subCol">
<h3 class="chapter"><img src="images/chapters_icon.gif" alt="" />Chapters</h3>
<ol class="chapters">
<li>
<a href="#application-architecture">Application Architecture</a>
<ul>
<li><a href="#rack-integration">Rack Integration</a></li>
<li><a href="#renewed-support-for-rails-engines">Renewed Support for Rails Engines</a></li>
</ul>
</li>
<li><a href="#documentation">Documentation</a></li>
<li><a href="#ruby-1.9.1-support">Ruby 1.9.1 Support</a></li>
<li>
<a href="#active-record">Active Record</a>
<ul>
<li><a href="#nested-attributes">Nested Attributes</a></li>
<li><a href="#nested-transactions">Nested Transactions</a></li>
<li><a href="#dynamic-scopes">Dynamic Scopes</a></li>
<li><a href="#default-scopes">Default Scopes</a></li>
<li><a href="#batch-processing">Batch Processing</a></li>
<li><a href="#multiple-conditions-for-callbacks">Multiple Conditions for Callbacks</a></li>
<li><a href="#find-with-having">Find with having</a></li>
<li><a href="#reconnecting-mysql-connections">Reconnecting MySQL Connections</a></li>
<li><a href="#other-active-record-changes">Other Active Record Changes</a></li>
</ul>
</li>
<li>
<a href="#action-controller">Action Controller</a>
<ul>
<li><a href="#unified-rendering">Unified Rendering</a></li>
<li><a href="#application-controller-renamed">Application Controller Renamed</a></li>
<li><a href="#http-digest-authentication-support">HTTP Digest Authentication Support</a></li>
<li><a href="#more-efficient-routing">More Efficient Routing</a></li>
<li><a href="#rack-based-lazy-loaded-sessions">Rack-based Lazy-loaded Sessions</a></li>
<li><a href="#mime-type-handling-changes">MIME Type Handling Changes</a></li>
<li><a href="#optimization-of-respond_to">Optimization of <code>respond_to</code></a></li>
<li><a href="#improved-caching-performance">Improved Caching Performance</a></li>
<li><a href="#localized-views">Localized Views</a></li>
<li><a href="#partial-scoping-for-translations">Partial Scoping for Translations</a></li>
<li><a href="#other-action-controller-changes">Other Action Controller Changes</a></li>
</ul>
</li>
<li>
<a href="#action-view">Action View</a>
<ul>
<li><a href="#nested-object-forms">Nested Object Forms</a></li>
<li><a href="#smart-rendering-of-partials">Smart Rendering of Partials</a></li>
<li><a href="#prompts-for-date-select-helpers">Prompts for Date Select Helpers</a></li>
<li><a href="#assettag-timestamp-caching">AssetTag Timestamp Caching</a></li>
<li><a href="#asset-hosts-as-objects">Asset Hosts as Objects</a></li>
<li><a href="#grouped_options_for_select-helper-method">grouped_options_for_select Helper Method</a></li>
<li><a href="#disabled-option-tags-for-form-select-helpers">Disabled Option Tags for Form Select Helpers</a></li>
<li><a href="#a-note-about-template-loading">A Note About Template Loading</a></li>
<li><a href="#other-action-view-changes">Other Action View Changes</a></li>
</ul>
</li>
<li>
<a href="#active-support">Active Support</a>
<ul>
<li><a href="#object#try">Object#try</a></li>
<li><a href="#object#tap-backport">Object#tap Backport</a></li>
<li><a href="#swappable-parsers-for-xmlmini">Swappable Parsers for XMLmini</a></li>
<li><a href="#fractional-seconds-for-timewithzone">Fractional seconds for TimeWithZone</a></li>
<li><a href="#json-key-quoting">JSON Key Quoting</a></li>
<li><a href="#other-active-support-changes">Other Active Support Changes</a></li>
</ul>
</li>
<li>
<a href="#railties">Railties</a>
<ul>
<li><a href="#rails-metal">Rails Metal</a></li>
<li><a href="#application-templates">Application Templates</a></li>
<li><a href="#quieter-backtraces">Quieter Backtraces</a></li>
<li><a href="#faster-boot-time-in-development-mode-with-lazy-loading/autoload">Faster Boot Time in Development Mode with Lazy Loading/Autoload</a></li>
<li><a href="#rake-gem-task-rewrite">rake gem Task Rewrite</a></li>
<li><a href="#other-railties-changes">Other Railties Changes</a></li>
</ul>
</li>
<li><a href="#deprecated">Deprecated</a></li>
<li><a href="#credits">Credits</a></li>
</ol>
</div>
</div>
</div>
<div id="container">
<div class="wrapper">
<div id="mainCol">
<h3 id="application-architecture">1 Application Architecture</h3><p>There are two major changes in the architecture of Rails applications: complete integration of the <a href="http://rack.github.io/">Rack</a> modular web server interface, and renewed support for Rails Engines.</p><h4 id="rack-integration">1.1 Rack Integration</h4><p>Rails has now broken with its CGI past, and uses Rack everywhere. This required and resulted in a tremendous number of internal changes (but if you use CGI, don't worry; Rails now supports CGI through a proxy interface.) Still, this is a major change to Rails internals. After upgrading to 2.3, you should test on your local environment and your production environment. Some things to test:</p>
<ul>
<li>Sessions</li>
<li>Cookies</li>
<li>File uploads</li>
<li>JSON/XML APIs</li>
</ul>
<p>Here's a summary of the rack-related changes:</p>
<ul>
<li>
<code>script/server</code> has been switched to use Rack, which means it supports any Rack compatible server. <code>script/server</code> will also pick up a rackup configuration file if one exists. By default, it will look for a <code>config.ru</code> file, but you can override this with the <code>-c</code> switch.</li>
<li>The FCGI handler goes through Rack.</li>
<li>
<code>ActionController::Dispatcher</code> maintains its own default middleware stack. Middlewares can be injected in, reordered, and removed. The stack is compiled into a chain on boot. You can configure the middleware stack in <code>environment.rb</code>.</li>
<li>The <code>rake middleware</code> task has been added to inspect the middleware stack. This is useful for debugging the order of the middleware stack.</li>
<li>The integration test runner has been modified to execute the entire middleware and application stack. This makes integration tests perfect for testing Rack middleware.</li>
<li>
<code>ActionController::CGIHandler</code> is a backwards compatible CGI wrapper around Rack. The <code>CGIHandler</code> is meant to take an old CGI object and convert its environment information into a Rack compatible form.</li>
<li>
<code>CgiRequest</code> and <code>CgiResponse</code> have been removed.</li>
<li>Session stores are now lazy loaded. If you never access the session object during a request, it will never attempt to load the session data (parse the cookie, load the data from memcache, or lookup an Active Record object).</li>
<li>You no longer need to use <code>CGI::Cookie.new</code> in your tests for setting a cookie value. Assigning a <code>String</code> value to request.cookies["foo"] now sets the cookie as expected.</li>
<li>
<code>CGI::Session::CookieStore</code> has been replaced by <code>ActionController::Session::CookieStore</code>.</li>
<li>
<code>CGI::Session::MemCacheStore</code> has been replaced by <code>ActionController::Session::MemCacheStore</code>.</li>
<li>
<code>CGI::Session::ActiveRecordStore</code> has been replaced by <code>ActiveRecord::SessionStore</code>.</li>
<li>You can still change your session store with <code>ActionController::Base.session_store = :active_record_store</code>.</li>
<li>Default sessions options are still set with <code>ActionController::Base.session = { :key => "..." }</code>. However, the <code>:session_domain</code> option has been renamed to <code>:domain</code>.</li>
<li>The mutex that normally wraps your entire request has been moved into middleware, <code>ActionController::Lock</code>.</li>
<li>
<code>ActionController::AbstractRequest</code> and <code>ActionController::Request</code> have been unified. The new <code>ActionController::Request</code> inherits from <code>Rack::Request</code>. This affects access to <code>response.headers['type']</code> in test requests. Use <code>response.content_type</code> instead.</li>
<li>
<code>ActiveRecord::QueryCache</code> middleware is automatically inserted onto the middleware stack if <code>ActiveRecord</code> has been loaded. This middleware sets up and flushes the per-request Active Record query cache.</li>
<li>The Rails router and controller classes follow the Rack spec. You can call a controller directly with <code>SomeController.call(env)</code>. The router stores the routing parameters in <code>rack.routing_args</code>.</li>
<li>
<code>ActionController::Request</code> inherits from <code>Rack::Request</code>.</li>
<li>Instead of <code>config.action_controller.session = { :session_key => 'foo', ...</code> use <code>config.action_controller.session = { :key => 'foo', ...</code>.</li>
<li>Using the <code>ParamsParser</code> middleware preprocesses any XML, JSON, or YAML requests so they can be read normally with any <code>Rack::Request</code> object after it.</li>
</ul>
<h4 id="renewed-support-for-rails-engines">1.2 Renewed Support for Rails Engines</h4><p>After some versions without an upgrade, Rails 2.3 offers some new features for Rails Engines (Rails applications that can be embedded within other applications). First, routing files in engines are automatically loaded and reloaded now, just like your <code>routes.rb</code> file (this also applies to routing files in other plugins). Second, if your plugin has an app folder, then app/[models|controllers|helpers] will automatically be added to the Rails load path. Engines also support adding view paths now, and Action Mailer as well as Action View will use views from engines and other plugins.</p><h3 id="documentation">2 Documentation</h3><p>The <a href="http://guides.rubyonrails.org/">Ruby on Rails guides</a> project has published several additional guides for Rails 2.3. In addition, a <a href="http://edgeguides.rubyonrails.org/">separate site</a> maintains updated copies of the Guides for Edge Rails. Other documentation efforts include a relaunch of the <a href="http://newwiki.rubyonrails.org/">Rails wiki</a> and early planning for a Rails Book.</p>
<ul>
<li>More Information: <a href="http://weblog.rubyonrails.org/2009/1/15/rails-documentation-projects.">Rails Documentation Projects</a>
</li>
</ul>
<h3 id="ruby-1.9.1-support">3 Ruby 1.9.1 Support</h3><p>Rails 2.3 should pass all of its own tests whether you are running on Ruby 1.8 or the now-released Ruby 1.9.1. You should be aware, though, that moving to 1.9.1 entails checking all of the data adapters, plugins, and other code that you depend on for Ruby 1.9.1 compatibility, as well as Rails core.</p><h3 id="active-record">4 Active Record</h3><p>Active Record gets quite a number of new features and bug fixes in Rails 2.3. The highlights include nested attributes, nested transactions, dynamic and default scopes, and batch processing.</p><h4 id="nested-attributes">4.1 Nested Attributes</h4><p>Active Record can now update the attributes on nested models directly, provided you tell it to do so:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class Book < ActiveRecord::Base
has_one :author
has_many :pages
accepts_nested_attributes_for :author, :pages
end
</pre>
</div>
<p>Turning on nested attributes enables a number of things: automatic (and atomic) saving of a record together with its associated children, child-aware validations, and support for nested forms (discussed later).</p><p>You can also specify requirements for any new records that are added via nested attributes using the <code>:reject_if</code> option:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
accepts_nested_attributes_for :author,
:reject_if => proc { |attributes| attributes['name'].blank? }
</pre>
</div>
<ul>
<li>Lead Contributor: <a href="http://superalloy.nl/">Eloy Duran</a>
</li>
<li>More Information: <a href="http://weblog.rubyonrails.org/2009/1/26/nested-model-forms">Nested Model Forms</a>
</li>
</ul>
<h4 id="nested-transactions">4.2 Nested Transactions</h4><p>Active Record now supports nested transactions, a much-requested feature. Now you can write code like this:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
User.transaction do
User.create(:username => 'Admin')
User.transaction(:requires_new => true) do
User.create(:username => 'Regular')
raise ActiveRecord::Rollback
end
end
User.find(:all) # => Returns only Admin
</pre>
</div>
<p>Nested transactions let you roll back an inner transaction without affecting the state of the outer transaction. If you want a transaction to be nested, you must explicitly add the <code>:requires_new</code> option; otherwise, a nested transaction simply becomes part of the parent transaction (as it does currently on Rails 2.2). Under the covers, nested transactions are <a href="http://rails.lighthouseapp.com/projects/8994/tickets/383,">using savepoints</a> so they're supported even on databases that don't have true nested transactions. There is also a bit of magic going on to make these transactions play well with transactional fixtures during testing.</p>
<ul>
<li>Lead Contributors: <a href="http://www.workingwithrails.com/person/4985-jonathan-viney">Jonathan Viney</a> and <a href="http://izumi.plan99.net/blog/">Hongli Lai</a>
</li>
</ul>
<h4 id="dynamic-scopes">4.3 Dynamic Scopes</h4><p>You know about dynamic finders in Rails (which allow you to concoct methods like <code>find_by_color_and_flavor</code> on the fly) and named scopes (which allow you to encapsulate reusable query conditions into friendly names like <code>currently_active</code>). Well, now you can have dynamic scope methods. The idea is to put together syntax that allows filtering on the fly <em>and</em> method chaining. For example:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
Order.scoped_by_customer_id(12)
Order.scoped_by_customer_id(12).find(:all,
:conditions => "status = 'open'")
Order.scoped_by_customer_id(12).scoped_by_status("open")
</pre>
</div>
<p>There's nothing to define to use dynamic scopes: they just work.</p>
<ul>
<li>Lead Contributor: <a href="http://evilmartians.com/">Yaroslav Markin</a>
</li>
<li>More Information: <a href="http://ryandaigle.com/articles/2008/12/29/what-s-new-in-edge-rails-dynamic-scope-methods.">What's New in Edge Rails: Dynamic Scope Methods</a>
</li>
</ul>
<h4 id="default-scopes">4.4 Default Scopes</h4><p>Rails 2.3 will introduce the notion of <em>default scopes</em> similar to named scopes, but applying to all named scopes or find methods within the model. For example, you can write <code>default_scope :order => 'name ASC'</code> and any time you retrieve records from that model they'll come out sorted by name (unless you override the option, of course).</p>
<ul>
<li>Lead Contributor: Paweł Kondzior</li>
<li>More Information: <a href="http://ryandaigle.com/articles/2008/11/18/what-s-new-in-edge-rails-default-scoping">What's New in Edge Rails: Default Scoping</a>
</li>
</ul>
<h4 id="batch-processing">4.5 Batch Processing</h4><p>You can now process large numbers of records from an Active Record model with less pressure on memory by using <code>find_in_batches</code>:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
Customer.find_in_batches(:conditions => {:active => true}) do |customer_group|
customer_group.each { |customer| customer.update_account_balance! }
end
</pre>
</div>
<p>You can pass most of the <code>find</code> options into <code>find_in_batches</code>. However, you cannot specify the order that records will be returned in (they will always be returned in ascending order of primary key, which must be an integer), or use the <code>:limit</code> option. Instead, use the <code>:batch_size</code> option, which defaults to 1000, to set the number of records that will be returned in each batch.</p><p>The new <code>find_each</code> method provides a wrapper around <code>find_in_batches</code> that returns individual records, with the find itself being done in batches (of 1000 by default):</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
Customer.find_each do |customer|
customer.update_account_balance!
end
</pre>
</div>
<p>Note that you should only use this method for batch processing: for small numbers of records (less than 1000), you should just use the regular find methods with your own loop.</p>
<ul>
<li>More Information (at that point the convenience method was called just <code>each</code>):
<ul>
<li><a href="http://afreshcup.com/2009/02/23/rails-23-batch-finding/">Rails 2.3: Batch Finding</a></li>
<li><a href="http://ryandaigle.com/articles/2009/2/23/what-s-new-in-edge-rails-batched-find">What's New in Edge Rails: Batched Find</a></li>
</ul>
</li>
</ul>
<h4 id="multiple-conditions-for-callbacks">4.6 Multiple Conditions for Callbacks</h4><p>When using Active Record callbacks, you can now combine <code>:if</code> and <code>:unless</code> options on the same callback, and supply multiple conditions as an array:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
before_save :update_credit_rating, :if => :active,
:unless => [:admin, :cash_only]
</pre>
</div>
<ul>
<li>Lead Contributor: L. Caviola</li>
</ul>
<h4 id="find-with-having">4.7 Find with having</h4><p>Rails now has a <code>:having</code> option on find (as well as on <code>has_many</code> and <code>has_and_belongs_to_many</code> associations) for filtering records in grouped finds. As those with heavy SQL backgrounds know, this allows filtering based on grouped results:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
developers = Developer.find(:all, :group => "salary",
:having => "sum(salary) > 10000", :select => "salary")
</pre>
</div>
<ul>
<li>Lead Contributor: <a href="http://github.com/miloops">Emilio Tagua</a>
</li>
</ul>
<h4 id="reconnecting-mysql-connections">4.8 Reconnecting MySQL Connections</h4><p>MySQL supports a reconnect flag in its connections - if set to true, then the client will try reconnecting to the server before giving up in case of a lost connection. You can now set <code>reconnect = true</code> for your MySQL connections in <code>database.yml</code> to get this behavior from a Rails application. The default is <code>false</code>, so the behavior of existing applications doesn't change.</p>
<ul>
<li>Lead Contributor: <a href="http://twitter.com/dubek">Dov Murik</a>
</li>
<li>More information:
<ul>
<li><a href="http://dev.mysql.com/doc/refman/5.0/en/auto-reconnect.html">Controlling Automatic Reconnection Behavior</a></li>
<li><a href="http://groups.google.com/group/rubyonrails-core/browse_thread/thread/49d2a7e9c96cb9f4">MySQL auto-reconnect revisited</a></li>
</ul>
</li>
</ul>
<h4 id="other-active-record-changes">4.9 Other Active Record Changes</h4>
<ul>
<li>An extra <code>AS</code> was removed from the generated SQL for <code>has_and_belongs_to_many</code> preloading, making it work better for some databases.</li>
<li>
<code>ActiveRecord::Base#new_record?</code> now returns <code>false</code> rather than <code>nil</code> when confronted with an existing record.</li>
<li>A bug in quoting table names in some <code>has_many :through</code> associations was fixed.</li>
<li>You can now specify a particular timestamp for <code>updated_at</code> timestamps: <code>cust = Customer.create(:name => "ABC Industries", :updated_at => 1.day.ago)</code>
</li>
<li>Better error messages on failed <code>find_by_attribute!</code> calls.</li>
<li>Active Record's <code>to_xml</code> support gets just a little bit more flexible with the addition of a <code>:camelize</code> option.</li>
<li>A bug in canceling callbacks from <code>before_update</code> or <code>before_create</code> was fixed.</li>
<li>Rake tasks for testing databases via JDBC have been added.</li>
<li>
<code>validates_length_of</code> will use a custom error message with the <code>:in</code> or <code>:within</code> options (if one is supplied).</li>
<li>Counts on scoped selects now work properly, so you can do things like <code>Account.scoped(:select => "DISTINCT credit_limit").count</code>.</li>
<li>
<code>ActiveRecord::Base#invalid?</code> now works as the opposite of <code>ActiveRecord::Base#valid?</code>.</li>
</ul>
<h3 id="action-controller">5 Action Controller</h3><p>Action Controller rolls out some significant changes to rendering, as well as improvements in routing and other areas, in this release.</p><h4 id="unified-rendering">5.1 Unified Rendering</h4><p><code>ActionController::Base#render</code> is a lot smarter about deciding what to render. Now you can just tell it what to render and expect to get the right results. In older versions of Rails, you often need to supply explicit information to render:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
render :file => '/tmp/random_file.erb'
render :template => 'other_controller/action'
render :action => 'show'
</pre>
</div>
<p>Now in Rails 2.3, you can just supply what you want to render:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
render '/tmp/random_file.erb'
render 'other_controller/action'
render 'show'
render :show
</pre>
</div>
<p>Rails chooses between file, template, and action depending on whether there is a leading slash, an embedded slash, or no slash at all in what's to be rendered. Note that you can also use a symbol instead of a string when rendering an action. Other rendering styles (<code>:inline</code>, <code>:text</code>, <code>:update</code>, <code>:nothing</code>, <code>:json</code>, <code>:xml</code>, <code>:js</code>) still require an explicit option.</p><h4 id="application-controller-renamed">5.2 Application Controller Renamed</h4><p>If you're one of the people who has always been bothered by the special-case naming of <code>application.rb</code>, rejoice! It's been reworked to be application_controller.rb in Rails 2.3. In addition, there's a new rake task, <code>rake rails:update:application_controller</code> to do this automatically for you - and it will be run as part of the normal <code>rake rails:update</code> process.</p>
<ul>
<li>More Information:
<ul>
<li><a href="http://afreshcup.com/2008/11/17/rails-2x-the-death-of-applicationrb/">The Death of Application.rb</a></li>
<li><a href="http://ryandaigle.com/articles/2008/11/19/what-s-new-in-edge-rails-application-rb-duality-is-no-more">What's New in Edge Rails: Application.rb Duality is no More</a></li>
</ul>
</li>
</ul>
<h4 id="http-digest-authentication-support">5.3 HTTP Digest Authentication Support</h4><p>Rails now has built-in support for HTTP digest authentication. To use it, you call <code>authenticate_or_request_with_http_digest</code> with a block that returns the user's password (which is then hashed and compared against the transmitted credentials):</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class PostsController < ApplicationController
Users = {"dhh" => "secret"}
before_filter :authenticate
def secret
render :text => "Password Required!"
end
private
def authenticate
realm = "Application"
authenticate_or_request_with_http_digest(realm) do |name|
Users[name]
end
end
end
</pre>
</div>
<ul>
<li>Lead Contributor: <a href="http://www.kellogg-assoc.com/">Gregg Kellogg</a>
</li>
<li>More Information: <a href="http://ryandaigle.com/articles/2009/1/30/what-s-new-in-edge-rails-http-digest-authentication">What's New in Edge Rails: HTTP Digest Authentication</a>
</li>
</ul>
<h4 id="more-efficient-routing">5.4 More Efficient Routing</h4><p>There are a couple of significant routing changes in Rails 2.3. The <code>formatted_</code> route helpers are gone, in favor just passing in <code>:format</code> as an option. This cuts down the route generation process by 50% for any resource - and can save a substantial amount of memory (up to 100MB on large applications). If your code uses the <code>formatted_</code> helpers, it will still work for the time being - but that behavior is deprecated and your application will be more efficient if you rewrite those routes using the new standard. Another big change is that Rails now supports multiple routing files, not just <code>routes.rb</code>. You can use <code>RouteSet#add_configuration_file</code> to bring in more routes at any time - without clearing the currently-loaded routes. While this change is most useful for Engines, you can use it in any application that needs to load routes in batches.</p>
<ul>
<li>Lead Contributors: <a href="http://blog.hungrymachine.com/">Aaron Batalion</a>
</li>
</ul>
<h4 id="rack-based-lazy-loaded-sessions">5.5 Rack-based Lazy-loaded Sessions</h4><p>A big change pushed the underpinnings of Action Controller session storage down to the Rack level. This involved a good deal of work in the code, though it should be completely transparent to your Rails applications (as a bonus, some icky patches around the old CGI session handler got removed). It's still significant, though, for one simple reason: non-Rails Rack applications have access to the same session storage handlers (and therefore the same session) as your Rails applications. In addition, sessions are now lazy-loaded (in line with the loading improvements to the rest of the framework). This means that you no longer need to explicitly disable sessions if you don't want them; just don't refer to them and they won't load.</p><h4 id="mime-type-handling-changes">5.6 MIME Type Handling Changes</h4><p>There are a couple of changes to the code for handling MIME types in Rails. First, <code>MIME::Type</code> now implements the <code>=~</code> operator, making things much cleaner when you need to check for the presence of a type that has synonyms:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
if content_type && Mime::JS =~ content_type
# do something cool
end
Mime::JS =~ "text/javascript" => true
Mime::JS =~ "application/javascript" => true
</pre>
</div>
<p>The other change is that the framework now uses the <code>Mime::JS</code> when checking for JavaScript in various spots, making it handle those alternatives cleanly.</p>
<ul>
<li>Lead Contributor: <a href="http://www.workingwithrails.com/person/5510-seth-fitzsimmons">Seth Fitzsimmons</a>
</li>
</ul>
<h4 id="optimization-of-respond_to">5.7 Optimization of <code>respond_to</code>
</h4><p>In some of the first fruits of the Rails-Merb team merger, Rails 2.3 includes some optimizations for the <code>respond_to</code> method, which is of course heavily used in many Rails applications to allow your controller to format results differently based on the MIME type of the incoming request. After eliminating a call to <code>method_missing</code> and some profiling and tweaking, we're seeing an 8% improvement in the number of requests per second served with a simple <code>respond_to</code> that switches between three formats. The best part? No change at all required to the code of your application to take advantage of this speedup.</p><h4 id="improved-caching-performance">5.8 Improved Caching Performance</h4><p>Rails now keeps a per-request local cache of read from the remote cache stores, cutting down on unnecessary reads and leading to better site performance. While this work was originally limited to <code>MemCacheStore</code>, it is available to any remote store than implements the required methods.</p>
<ul>
<li>Lead Contributor: <a href="http://www.motionstandingstill.com/">Nahum Wild</a>
</li>
</ul>
<h4 id="localized-views">5.9 Localized Views</h4><p>Rails can now provide localized views, depending on the locale that you have set. For example, suppose you have a <code>Posts</code> controller with a <code>show</code> action. By default, this will render <code>app/views/posts/show.html.erb</code>. But if you set <code>I18n.locale = :da</code>, it will render <code>app/views/posts/show.da.html.erb</code>. If the localized template isn't present, the undecorated version will be used. Rails also includes <code>I18n#available_locales</code> and <code>I18n::SimpleBackend#available_locales</code>, which return an array of the translations that are available in the current Rails project.</p><p>In addition, you can use the same scheme to localize the rescue files in the <code>public</code> directory: <code>public/500.da.html</code> or <code>public/404.en.html</code> work, for example.</p><h4 id="partial-scoping-for-translations">5.10 Partial Scoping for Translations</h4><p>A change to the translation API makes things easier and less repetitive to write key translations within partials. If you call <code>translate(".foo")</code> from the <code>people/index.html.erb</code> template, you'll actually be calling <code>I18n.translate("people.index.foo")</code> If you don't prepend the key with a period, then the API doesn't scope, just as before.</p><h4 id="other-action-controller-changes">5.11 Other Action Controller Changes</h4>
<ul>
<li>ETag handling has been cleaned up a bit: Rails will now skip sending an ETag header when there's no body to the response or when sending files with <code>send_file</code>.</li>
<li>The fact that Rails checks for IP spoofing can be a nuisance for sites that do heavy traffic with cell phones, because their proxies don't generally set things up right. If that's you, you can now set <code>ActionController::Base.ip_spoofing_check = false</code> to disable the check entirely.</li>
<li>
<code>ActionController::Dispatcher</code> now implements its own middleware stack, which you can see by running <code>rake middleware</code>.</li>
<li>Cookie sessions now have persistent session identifiers, with API compatibility with the server-side stores.</li>
<li>You can now use symbols for the <code>:type</code> option of <code>send_file</code> and <code>send_data</code>, like this: <code>send_file("fabulous.png", :type => :png)</code>.</li>
<li>The <code>:only</code> and <code>:except</code> options for <code>map.resources</code> are no longer inherited by nested resources.</li>
<li>The bundled memcached client has been updated to version 1.6.4.99.</li>
<li>The <code>expires_in</code>, <code>stale?</code>, and <code>fresh_when</code> methods now accept a <code>:public</code> option to make them work well with proxy caching.</li>
<li>The <code>:requirements</code> option now works properly with additional RESTful member routes.</li>
<li>Shallow routes now properly respect namespaces.</li>
<li>
<code>polymorphic_url</code> does a better job of handling objects with irregular plural names.</li>
</ul>
<h3 id="action-view">6 Action View</h3><p>Action View in Rails 2.3 picks up nested model forms, improvements to <code>render</code>, more flexible prompts for the date select helpers, and a speedup in asset caching, among other things.</p><h4 id="nested-object-forms">6.1 Nested Object Forms</h4><p>Provided the parent model accepts nested attributes for the child objects (as discussed in the Active Record section), you can create nested forms using <code>form_for</code> and <code>field_for</code>. These forms can be nested arbitrarily deep, allowing you to edit complex object hierarchies on a single view without excessive code. For example, given this model:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
class Customer < ActiveRecord::Base
has_many :orders
accepts_nested_attributes_for :orders, :allow_destroy => true
end
</pre>
</div>
<p>You can write this view in Rails 2.3:</p><div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
<% form_for @customer do |customer_form| %>
<div>
<%= customer_form.label :name, 'Customer Name:' %>
<%= customer_form.text_field :name %>
</div>
<!-- Here we call fields_for on the customer_form builder instance.
The block is called for each member of the orders collection. -->
<% customer_form.fields_for :orders do |order_form| %>
<p>
<div>
<%= order_form.label :number, 'Order Number:' %>
<%= order_form.text_field :number %>
</div>
<!-- The allow_destroy option in the model enables deletion of
child records. -->
<% unless order_form.object.new_record? %>
<div>
<%= order_form.label :_delete, 'Remove:' %>
<%= order_form.check_box :_delete %>
</div>
<% end %>
</p>
<% end %>
<%= customer_form.submit %>
<% end %>
</pre>
</div>
<ul>
<li>Lead Contributor: <a href="http://superalloy.nl/">Eloy Duran</a>
</li>
<li>More Information:
<ul>
<li><a href="http://weblog.rubyonrails.org/2009/1/26/nested-model-forms">Nested Model Forms</a></li>
<li><a href="http://github.com/alloy/complex-form-examples">complex-form-examples</a></li>
<li><a href="http://ryandaigle.com/articles/2009/2/1/what-s-new-in-edge-rails-nested-attributes">What's New in Edge Rails: Nested Object Forms</a></li>
</ul>
</li>
</ul>
<h4 id="smart-rendering-of-partials">6.2 Smart Rendering of Partials</h4><p>The render method has been getting smarter over the years, and it's even smarter now. If you have an object or a collection and an appropriate partial, and the naming matches up, you can now just render the object and things will work. For example, in Rails 2.3, these render calls will work in your view (assuming sensible naming):</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
# Equivalent of render :partial => 'articles/_article',
# :object => @article
render @article
# Equivalent of render :partial => 'articles/_article',
# :collection => @articles
render @articles
</pre>
</div>
<ul>
<li>More Information: <a href="http://ryandaigle.com/articles/2008/11/20/what-s-new-in-edge-rails-render-stops-being-high-maintenance">What's New in Edge Rails: render Stops Being High-Maintenance</a>
</li>
</ul>
<h4 id="prompts-for-date-select-helpers">6.3 Prompts for Date Select Helpers</h4><p>In Rails 2.3, you can supply custom prompts for the various date select helpers (<code>date_select</code>, <code>time_select</code>, and <code>datetime_select</code>), the same way you can with collection select helpers. You can supply a prompt string or a hash of individual prompt strings for the various components. You can also just set <code>:prompt</code> to <code>true</code> to use the custom generic prompt:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
select_datetime(DateTime.now, :prompt => true)
select_datetime(DateTime.now, :prompt => "Choose date and time")
select_datetime(DateTime.now, :prompt =>
{:day => 'Choose day', :month => 'Choose month',
:year => 'Choose year', :hour => 'Choose hour',
:minute => 'Choose minute'})
</pre>
</div>
<ul>
<li>Lead Contributor: <a href="http://samoliver.com/">Sam Oliver</a>
</li>
</ul>
<h4 id="assettag-timestamp-caching">6.4 AssetTag Timestamp Caching</h4><p>You're likely familiar with Rails' practice of adding timestamps to static asset paths as a "cache buster." This helps ensure that stale copies of things like images and stylesheets don't get served out of the user's browser cache when you change them on the server. You can now modify this behavior with the <code>cache_asset_timestamps</code> configuration option for Action View. If you enable the cache, then Rails will calculate the timestamp once when it first serves an asset, and save that value. This means fewer (expensive) file system calls to serve static assets - but it also means that you can't modify any of the assets while the server is running and expect the changes to get picked up by clients.</p><h4 id="asset-hosts-as-objects">6.5 Asset Hosts as Objects</h4><p>Asset hosts get more flexible in edge Rails with the ability to declare an asset host as a specific object that responds to a call. This allows you to implement any complex logic you need in your asset hosting.</p>
<ul>
<li>More Information: <a href="http://github.com/dhh/asset-hosting-with-minimum-ssl/tree/master">asset-hosting-with-minimum-ssl</a>
</li>
</ul>
<h4 id="grouped_options_for_select-helper-method">6.6 grouped_options_for_select Helper Method</h4><p>Action View already had a bunch of helpers to aid in generating select controls, but now there's one more: <code>grouped_options_for_select</code>. This one accepts an array or hash of strings, and converts them into a string of <code>option</code> tags wrapped with <code>optgroup</code> tags. For example:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
grouped_options_for_select([["Hats", ["Baseball Cap","Cowboy Hat"]]],
"Cowboy Hat", "Choose a product...")
</pre>
</div>
<p>returns</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
<option value="">Choose a product...</option>
<optgroup label="Hats">
<option value="Baseball Cap">Baseball Cap</option>
<option selected="selected" value="Cowboy Hat">Cowboy Hat</option>
</optgroup>
</pre>
</div>
<h4 id="disabled-option-tags-for-form-select-helpers">6.7 Disabled Option Tags for Form Select Helpers</h4><p>The form select helpers (such as <code>select</code> and <code>options_for_select</code>) now support a <code>:disabled</code> option, which can take a single value or an array of values to be disabled in the resulting tags:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
select(:post, :category, Post::CATEGORIES, :disabled => 'private')
</pre>
</div>
<p>returns</p><div class="code_container">
<pre class="brush: xml; gutter: false; toolbar: false">
<select name="post[category]">
<option>story</option>
<option>joke</option>
<option>poem</option>
<option disabled="disabled">private</option>
</select>
</pre>
</div>
<p>You can also use an anonymous function to determine at runtime which options from collections will be selected and/or disabled:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
options_from_collection_for_select(@product.sizes, :name, :id, :disabled => lambda{|size| size.out_of_stock?})
</pre>
</div>
<ul>
<li>Lead Contributor: <a href="http://tekin.co.uk/">Tekin Suleyman</a>
</li>
<li>More Information: <a href="http://tekin.co.uk/2009/03/new-in-rails-23-disabled-option-tags-and-lambdas-for-selecting-and-disabling-options-from-collections/">New in rails 2.3 - disabled option tags and lambdas for selecting and disabling options from collections</a>
</li>
</ul>
<h4 id="a-note-about-template-loading">6.8 A Note About Template Loading</h4><p>Rails 2.3 includes the ability to enable or disable cached templates for any particular environment. Cached templates give you a speed boost because they don't check for a new template file when they're rendered - but they also mean that you can't replace a template "on the fly" without restarting the server.</p><p>In most cases, you'll want template caching to be turned on in production, which you can do by making a setting in your <code>production.rb</code> file:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
config.action_view.cache_template_loading = true
</pre>
</div>
<p>This line will be generated for you by default in a new Rails 2.3 application. If you've upgraded from an older version of Rails, Rails will default to caching templates in production and test but not in development.</p><h4 id="other-action-view-changes">6.9 Other Action View Changes</h4>
<ul>
<li>Token generation for CSRF protection has been simplified; now Rails uses a simple random string generated by <code>ActiveSupport::SecureRandom</code> rather than mucking around with session IDs.</li>
<li>
<code>auto_link</code> now properly applies options (such as <code>:target</code> and <code>:class</code>) to generated e-mail links.</li>
<li>The <code>autolink</code> helper has been refactored to make it a bit less messy and more intuitive.</li>
<li>
<code>current_page?</code> now works properly even when there are multiple query parameters in the URL.</li>
</ul>
<h3 id="active-support">7 Active Support</h3><p>Active Support has a few interesting changes, including the introduction of <code>Object#try</code>.</p><h4 id="object#try">7.1 Object#try</h4><p>A lot of folks have adopted the notion of using try() to attempt operations on objects. It's especially helpful in views where you can avoid nil-checking by writing code like <code><%= @person.try(:name) %></code>. Well, now it's baked right into Rails. As implemented in Rails, it raises <code>NoMethodError</code> on private methods and always returns <code>nil</code> if the object is nil.</p>
<ul>
<li>More Information: <a href="http://ozmm.org/posts/try.html.">try()</a>
</li>
</ul>
<h4 id="object#tap-backport">7.2 Object#tap Backport</h4><p><code>Object#tap</code> is an addition to <a href="http://www.ruby-doc.org/core-1.9/classes/Object.html#M000309">Ruby 1.9</a> and 1.8.7 that is similar to the <code>returning</code> method that Rails has had for a while: it yields to a block, and then returns the object that was yielded. Rails now includes code to make this available under older versions of Ruby as well.</p><h4 id="swappable-parsers-for-xmlmini">7.3 Swappable Parsers for XMLmini</h4><p>The support for XML parsing in Active Support has been made more flexible by allowing you to swap in different parsers. By default, it uses the standard REXML implementation, but you can easily specify the faster LibXML or Nokogiri implementations for your own applications, provided you have the appropriate gems installed:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
XmlMini.backend = 'LibXML'
</pre>
</div>
<ul>
<li>Lead Contributor: <a href="http://www.movesonrails.com/">Bart ten Brinke</a>
</li>
<li>Lead Contributor: <a href="http://tenderlovemaking.com/">Aaron Patterson</a>
</li>
</ul>
<h4 id="fractional-seconds-for-timewithzone">7.4 Fractional seconds for TimeWithZone</h4><p>The <code>Time</code> and <code>TimeWithZone</code> classes include an <code>xmlschema</code> method to return the time in an XML-friendly string. As of Rails 2.3, <code>TimeWithZone</code> supports the same argument for specifying the number of digits in the fractional second part of the returned string that <code>Time</code> does:</p><div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
>> Time.zone.now.xmlschema(6)
=> "2009-01-16T13:00:06.13653Z"
</pre>
</div>
<ul>
<li>Lead Contributor: <a href="http://www.workingwithrails.com/person/13536-nicholas-dainty">Nicholas Dainty</a>
</li>
</ul>
<h4 id="json-key-quoting">7.5 JSON Key Quoting</h4><p>If you look up the spec on the "json.org" site, you'll discover that all keys in a JSON structure must be strings, and they must be quoted with double quotes. Starting with Rails 2.3, we do the right thing here, even with numeric keys.</p><h4 id="other-active-support-changes">7.6 Other Active Support Changes</h4>
<ul>
<li>You can use <code>Enumerable#none?</code> to check that none of the elements match the supplied block.</li>
<li>If you're using Active Support <a href="http://afreshcup.com/2008/10/19/coming-in-rails-22-delegate-prefixes/,">delegates</a> the new <code>:allow_nil</code> option lets you return <code>nil</code> instead of raising an exception when the target object is nil.</li>
<li>
<code>ActiveSupport::OrderedHash</code>: now implements <code>each_key</code> and <code>each_value</code>.</li>
<li>
<code>ActiveSupport::MessageEncryptor</code> provides a simple way to encrypt information for storage in an untrusted location (like cookies).</li>
<li>Active Support's <code>from_xml</code> no longer depends on XmlSimple. Instead, Rails now includes its own XmlMini implementation, with just the functionality that it requires. This lets Rails dispense with the bundled copy of XmlSimple that it's been carting around.</li>
<li>If you memoize a private method, the result will now be private.</li>
<li>
<code>String#parameterize</code> accepts an optional separator: <code>"Quick Brown Fox".parameterize('_') => "quick_brown_fox"</code>.</li>
<li>
<code>number_to_phone</code> accepts 7-digit phone numbers now.</li>
<li>
<code>ActiveSupport::Json.decode</code> now handles <code>\u0000</code> style escape sequences.</li>
</ul>
<h3 id="railties">8 Railties</h3><p>In addition to the Rack changes covered above, Railties (the core code of Rails itself) sports a number of significant changes, including Rails Metal, application templates, and quiet backtraces.</p><h4 id="rails-metal">8.1 Rails Metal</h4><p>Rails Metal is a new mechanism that provides superfast endpoints inside of your Rails applications. Metal classes bypass routing and Action Controller to give you raw speed (at the cost of all the things in Action Controller, of course). This builds on all of the recent foundation work to make Rails a Rack application with an exposed middleware stack. Metal endpoints can be loaded from your application or from plugins.</p>
<ul>
<li>More Information:
<ul>
<li><a href="http://weblog.rubyonrails.org/2008/12/17/introducing-rails-metal">Introducing Rails Metal</a></li>
<li><a href="http://soylentfoo.jnewland.com/articles/2008/12/16/rails-metal-a-micro-framework-with-the-power-of-rails-m">Rails Metal: a micro-framework with the power of Rails</a></li>
<li><a href="http://www.railsinside.com/deployment/180-metal-super-fast-endpoints-within-your-rails-apps.html">Metal: Super-fast Endpoints within your Rails Apps</a></li>
<li><a href="http://ryandaigle.com/articles/2008/12/18/what-s-new-in-edge-rails-rails-metal">What's New in Edge Rails: Rails Metal</a></li>
</ul>
</li>
</ul>
<h4 id="application-templates">8.2 Application Templates</h4><p>Rails 2.3 incorporates Jeremy McAnally's <a href="http://github.com/jeremymcanally/rg/tree/master">rg</a> application generator. What this means is that we now have template-based application generation built right into Rails; if you have a set of plugins you include in every application (among many other use cases), you can just set up a template once and use it over and over again when you run the <code>rails</code> command. There's also a rake task to apply a template to an existing application:</p><div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
rake rails:template LOCATION=~/template.rb
</pre>
</div>
<p>This will layer the changes from the template on top of whatever code the project already contains.</p>
<ul>
<li>Lead Contributor: <a href="http://www.jeremymcanally.com/">Jeremy McAnally</a>
</li>
<li>More Info:<a href="http://m.onkey.org/2008/12/4/rails-templates">Rails templates</a>
</li>
</ul>
<h4 id="quieter-backtraces">8.3 Quieter Backtraces</h4><p>Building on Thoughtbot's <a href="https://github.com/thoughtbot/quietbacktrace">Quiet Backtrace</a> plugin, which allows you to selectively remove lines from <code>Test::Unit</code> backtraces, Rails 2.3 implements <code>ActiveSupport::BacktraceCleaner</code> and <code>Rails::BacktraceCleaner</code> in core. This supports both filters (to perform regex-based substitutions on backtrace lines) and silencers (to remove backtrace lines entirely). Rails automatically adds silencers to get rid of the most common noise in a new application, and builds a <code>config/backtrace_silencers.rb</code> file to hold your own additions. This feature also enables prettier printing from any gem in the backtrace.</p><h4 id="faster-boot-time-in-development-mode-with-lazy-loading/autoload">8.4 Faster Boot Time in Development Mode with Lazy Loading/Autoload</h4><p>Quite a bit of work was done to make sure that bits of Rails (and its dependencies) are only brought into memory when they're actually needed. The core frameworks - Active Support, Active Record, Action Controller, Action Mailer and Action View - are now using <code>autoload</code> to lazy-load their individual classes. This work should help keep the memory footprint down and improve overall Rails performance.</p><p>You can also specify (by using the new <code>preload_frameworks</code> option) whether the core libraries should be autoloaded at startup. This defaults to <code>false</code> so that Rails autoloads itself piece-by-piece, but there are some circumstances where you still need to bring in everything at once - Passenger and JRuby both want to see all of Rails loaded together.</p><h4 id="rake-gem-task-rewrite">8.5 rake gem Task Rewrite</h4><p>The internals of the various <code>rake gem</code> tasks have been substantially revised, to make the system work better for a variety of cases. The gem system now knows the difference between development and runtime dependencies, has a more robust unpacking system, gives better information when querying for the status of gems, and is less prone to "chicken and egg" dependency issues when you're bringing things up from scratch. There are also fixes for using gem commands under JRuby and for dependencies that try to bring in external copies of gems that are already vendored.</p>
<ul>
<li>Lead Contributor: <a href="http://www.workingwithrails.com/person/12240-david-dollar">David Dollar</a>
</li>
</ul>
<h4 id="other-railties-changes">8.6 Other Railties Changes</h4>
<ul>
<li>The instructions for updating a CI server to build Rails have been updated and expanded.</li>
<li>Internal Rails testing has been switched from <code>Test::Unit::TestCase</code> to <code>ActiveSupport::TestCase</code>, and the Rails core requires Mocha to test.</li>
<li>The default <code>environment.rb</code> file has been decluttered.</li>
<li>The dbconsole script now lets you use an all-numeric password without crashing.</li>
<li>
<code>Rails.root</code> now returns a <code>Pathname</code> object, which means you can use it directly with the <code>join</code> method to <a href="http://afreshcup.com/2008/12/05/a-little-rails_root-tidiness/">clean up existing code</a> that uses <code>File.join</code>.</li>
<li>Various files in /public that deal with CGI and FCGI dispatching are no longer generated in every Rails application by default (you can still get them if you need them by adding <code>--with-dispatchers</code> when you run the <code>rails</code> command, or add them later with <code>rake rails:update:generate_dispatchers</code>).</li>
<li>Rails Guides have been converted from AsciiDoc to Textile markup.</li>
<li>Scaffolded views and controllers have been cleaned up a bit.</li>
<li>
<code>script/server</code> now accepts a <code>--path</code> argument to mount a Rails application from a specific path.</li>
<li>If any configured gems are missing, the gem rake tasks will skip loading much of the environment. This should solve many of the "chicken-and-egg" problems where rake gems:install couldn't run because gems were missing.</li>
<li>Gems are now unpacked exactly once. This fixes issues with gems (hoe, for instance) which are packed with read-only permissions on the files.</li>
</ul>
<h3 id="deprecated">9 Deprecated</h3><p>A few pieces of older code are deprecated in this release:</p>
<ul>
<li>If you're one of the (fairly rare) Rails developers who deploys in a fashion that depends on the inspector, reaper, and spawner scripts, you'll need to know that those scripts are no longer included in core Rails. If you need them, you'll be able to pick up copies via the <a href="http://github.com/rails/irs_process_scripts/tree">irs_process_scripts</a> plugin.</li>
<li>
<code>render_component</code> goes from "deprecated" to "nonexistent" in Rails 2.3. If you still need it, you can install the <a href="http://github.com/rails/render_component/tree/master">render_component plugin</a>.</li>
<li>Support for Rails components has been removed.</li>
<li>If you were one of the people who got used to running <code>script/performance/request</code> to look at performance based on integration tests, you need to learn a new trick: that script has been removed from core Rails now. There's a new request_profiler plugin that you can install to get the exact same functionality back.</li>
<li>
<code>ActionController::Base#session_enabled?</code> is deprecated because sessions are lazy-loaded now.</li>
<li>The <code>:digest</code> and <code>:secret</code> options to <code>protect_from_forgery</code> are deprecated and have no effect.</li>
<li>Some integration test helpers have been removed. <code>response.headers["Status"]</code> and <code>headers["Status"]</code> will no longer return anything. Rack does not allow "Status" in its return headers. However you can still use the <code>status</code> and <code>status_message</code> helpers. <code>response.headers["cookie"]</code> and <code>headers["cookie"]</code> will no longer return any CGI cookies. You can inspect <code>headers["Set-Cookie"]</code> to see the raw cookie header or use the <code>cookies</code> helper to get a hash of the cookies sent to the client.</li>
<li>
<code>formatted_polymorphic_url</code> is deprecated. Use <code>polymorphic_url</code> with <code>:format</code> instead.</li>
<li>The <code>:http_only</code> option in <code>ActionController::Response#set_cookie</code> has been renamed to <code>:httponly</code>.</li>
<li>The <code>:connector</code> and <code>:skip_last_comma</code> options of <code>to_sentence</code> have been replaced by <code>:words_connnector</code>, <code>:two_words_connector</code>, and <code>:last_word_connector</code> options.</li>
<li>Posting a multipart form with an empty <code>file_field</code> control used to submit an empty string to the controller. Now it submits a nil, due to differences between Rack's multipart parser and the old Rails one.</li>
</ul>
<h3 id="credits">10 Credits</h3><p>Release notes compiled by <a href="http://afreshcup.com">Mike Gunderloy</a>. This version of the Rails 2.3 release notes was compiled based on RC2 of Rails 2.3.</p>
<h3>反馈</h3>
<p>
欢迎帮忙改善指南质量。
</p>
<p>
如发现任何错误,欢迎修正。开始贡献前,可先行阅读<a href="http://edgeguides.rubyonrails.org/contributing_to_ruby_on_rails.html#contributing-to-the-rails-documentation">贡献指南:文档</a>。
</p>
<p>翻译如有错误,深感抱歉,欢迎 <a href="https://github.com/ruby-china/guides/fork">Fork</a> 修正,或至此处<a href="https://github.com/ruby-china/guides/issues/new">回报</a>。</p>
<p>
文章可能有未完成或过时的内容。请先检查 <a href="http://edgeguides.rubyonrails.org">Edge Guides</a> 来确定问题在 master 是否已经修掉了。再上 master 补上缺少的文件。内容参考 <a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails 指南准则</a>来了解行文风格。
</p>
<p>最后,任何关于 Ruby on Rails 文档的讨论,欢迎到 <a href="http://groups.google.com/group/rubyonrails-docs">rubyonrails-docs 邮件群组</a>。
</p>
</div>
</div>
</div>
<hr class="hide" />
<div id="footer">
<div class="wrapper">
<p>本著作采用<a href="https://creativecommons.org/licenses/by-sa/4.0/">创用 CC 姓名标示-相同方式分享 4.0 国际授权条款</a>授权。</p>
<p>“Rails”、“Ruby on Rails”,以及 Rails logo 为 David Heinemeier Hansson 的商标。版权所有。</p>
</div>
</div>
<script type="text/javascript" src="javascripts/jquery.min.js"></script>
<script type="text/javascript" src="javascripts/responsive-tables.js"></script>
<script type="text/javascript" src="javascripts/guides.js"></script>
<script type="text/javascript" src="javascripts/syntaxhighlighter/shCore.js"></script>
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushRuby.js"></script>
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushXml.js"></script>
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushSql.js"></script>
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushPlain.js"></script>
<script type="text/javascript">
SyntaxHighlighter.all();
$(guidesIndex.bind);
</script>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
// ga('create', '', 'ruby-china.github.io');
ga('require', 'displayfeatures');
ga('send', 'pageview');
</script>
</body>
</html>