This repository was archived by the owner on Jan 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconfiguring.html
More file actions
791 lines (759 loc) · 60.4 KB
/
configuring.html
File metadata and controls
791 lines (759 loc) · 60.4 KB
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
<!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="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Ruby on Rails Guides: Configuring Rails Applications</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" />
</head>
<body class="guide">
<div id="topNav">
<div class="wrapper">
<strong>More at <a href="http://rubyonrails.org/">rubyonrails.org:</a> </strong>
<a href="http://rubyonrails.org/">Overview</a> |
<a href="http://rubyonrails.org/download">Download</a> |
<a href="http://rubyonrails.org/deploy">Deploy</a> |
<a href="http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/overview">Code</a> |
<a href="http://rubyonrails.org/screencasts">Screencasts</a> |
<a href="http://rubyonrails.org/documentation">Documentation</a> |
<a href="http://rubyonrails.org/ecosystem">Ecosystem</a> |
<a href="http://rubyonrails.org/community">Community</a> |
<a href="http://weblog.rubyonrails.org/">Blog</a>
</div>
</div>
<div id="header">
<div class="wrapper clearfix">
<h1><a href="index.html" title="Return to home page">Guides.rubyonrails.org</a></h1>
<p class="hide"><a href="#mainCol">Skip navigation</a>.</p>
<ul class="nav">
<li><a href="index.html">홈</a></li>
<li class="index"><a href="index.html" onclick="guideMenu(); return false;" id="guidesMenu">목차</a>
<div id="guides" class="clearfix" style="display: none;">
<hr />
<dl class="L">
<dt>시작</dt>
<dd><a href="getting_started.html">레일즈 시작하기</a></dd>
<dt>모델(Models)</dt>
<dd><a href="migrations.html">레일즈 데이터베이스 마이그레이션</a></dd>
<dd><a href="active_record_validations_callbacks.html">액티브 레코드 데이터 검증(Validation)과 Callback(콜백)</a></dd>
<dd><a href="association_basics.html">액티브 레코드 Association(관계)</a></dd>
<dd><a href="active_record_querying.html">액티브 레코드 쿼리 인터페이스</a></dd>
<dt>뷰(Views)</dt>
<dd><a href="layouts_and_rendering.html">레이아웃(Layouts)과 렌더링(Rendering)</a></dd>
<dd><a href="form_helpers.html">액션 뷰 폼 핼퍼(Action View Form Helpers)</a></dd>
<dt>컨트롤러(Controllers)</dt>
<dd><a href="action_controller_overview.html">액션 컨트롤러 둘러보기</a></dd>
<dd><a href="routing.html">외부 요청에 대한 레일즈 라우팅</a></dd>
</dl>
<dl class="R">
<dt>심화내용</dt>
<dd><a href="active_support_core_extensions.html">액티브 서포트(Active Support) 확장(Core Extensions)</a></dd>
<dd><a href="i18n.html">레일즈 국제화I(nternationalization) API</a></dd>
<dd><a href="action_mailer_basics.html">액션 메일러의 기본</a></dd>
<dd><a href="testing.html">레일즈 어플리케이션 테스트하기</a></dd>
<dd><a href="security.html">레일즈 어플리케이션의 보안</a></dd>
<dd><a href="debugging_rails_applications.html">레일즈 어플리케이션 디버깅</a></dd>
<dd><a href="performance_testing.html">레일즈 어플리케이션 성능 테스트하기</a></dd>
<dd><a href="configuring.html">레일즈 어플리케이션 설정</a></dd>
<dd><a href="command_line.html">레일즈 커멘드라인 도구와 Rake 테스크</a></dd>
<dd><a href="caching_with_rails.html">레일즈를 이용한 캐싱</a></dd>
<dt>레일즈 확장하기(Extending Rails)</dt>
<dd><a href="plugins.html">레일즈 플러그인 작성의 기본</a></dd>
<dd><a href="rails_on_rack.html">렉 위의 레일즈(Rails on Rack)</a></dd>
<dd><a href="generators.html">레일즈 제너레이터(Generator) 제작과 수정</a></dd>
<dt>루비 온 레이즈에 기여하기</dt>
<dd><a href="contributing_to_ruby_on_rails.html">루비 온 레이즈에 기여하기</a></dd>
<dd><a href="api_documentation_guidelines.html">API 문서 가이드라인</a></dd>
<dd><a href="ruby_on_rails_guides_guidelines.html">루비 온 레일즈 가이드에 대한 가이드라인</a></dd>
<dt>Release Notes</dt>
<dd><a href="3_0_release_notes.html">Ruby on Rails 3.0 Release Notes</a></dd>
<dd><a href="2_3_release_notes.html">Ruby on Rails 2.3 Release Notes</a></dd>
<dd><a href="2_2_release_notes.html">Ruby on Rails 2.2 Release Notes</a></dd>
</dl>
</div>
</li>
<li><a href="contribute.html">기여하기</a></li>
<li><a href="credits.html">수고하신 분들</a></li>
</ul>
</div>
</div>
<hr class="hide" />
<div id="feature">
<div class="wrapper">
<h2>Configuring Rails Applications</h2>
<p>This guide covers the configuration and initialization features available to Rails applications. By referring to this guide, you will be able to:</p>
<ul>
<li>Adjust the behavior of your Rails applications</li>
<li>Add additional code to be run at application start time</li>
</ul>
<div id="subCol">
<h3 class="chapter"><img src="images/chapters_icon.gif" alt="" />Chapters</h3>
<ol class="chapters">
<li><a href="#locations-for-initialization-code">Locations for Initialization Code</a></li><li><a href="#running-code-before-rails">Running Code Before Rails</a></li><li><a href="#configuring-rails-components">Configuring Rails Components</a><ul><li><a href="#rails-general-configuration">Rails General Configuration</a></li> <li><a href="#configuring-generators">Configuring Generators</a></li> <li><a href="#configuring-middleware">Configuring Middleware</a></li> <li><a href="#configuring-i18n">Configuring i18n</a></li> <li><a href="#configuring-active-record">Configuring Active Record</a></li> <li><a href="#configuring-action-controller">Configuring Action Controller</a></li> <li><a href="#configuring-action-dispatch">Configuring Action Dispatch</a></li> <li><a href="#configuring-action-view">Configuring Action View</a></li> <li><a href="#configuring-action-mailer">Configuring Action Mailer</a></li> <li><a href="#configuring-active-resource">Configuring Active Resource</a></li> <li><a href="#configuring-active-support">Configuring Active Support</a></li></ul></li><li><a href="#rails-environment-settings">Rails Environment Settings</a></li><li><a href="#using-initializer-files">Using Initializer Files</a></li><li><a href="#initialization-events">Initialization events</a><ul><li><a href="#rails-railtie-initializer"><tt>Rails::Railtie#initializer</tt></a></li> <li><a href="#initializers">Initializers</a></li></ul></li><li><a href="#changelog">Changelog</a></li></ol></div>
</div>
</div>
<div id="container">
<div class="wrapper">
<div id="mainCol">
<h3 id="locations-for-initialization-code">1 Locations for Initialization Code</h3>
<p>Rails offers (at least) four good spots to place initialization code:</p>
<ul>
<li>application.rb</li>
<li>Environment-specific Configuration Files</li>
<li>Initializers</li>
<li>After-Initializers</li>
</ul>
<h3 id="running-code-before-rails">2 Running Code Before Rails</h3>
<p>To run some code before Rails itself is loaded, simply put it above the call to
<tt>require 'rails/all'</tt> in your <tt>application.rb</tt>.</p>
<h3 id="configuring-rails-components">3 Configuring Rails Components</h3>
<p>In general, the work of configuring Rails means configuring the components of Rails, as well as configuring Rails itself. The <tt>application.rb</tt> and environment-specific configuration files (such as <tt>config/environments/production.rb</tt>) allow you to specify the various settings that you want to pass down to all of the components. For example, the default Rails 3.0 <tt>application.rb</tt> file includes this setting:</p>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
config.filter_parameters += [:password]
</pre>
</div>
</notextile>
<p>This is a setting for Rails itself. If you want to pass settings to individual Rails components, you can do so via the same <tt>config</tt> object:</p>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
config.active_record.timestamped_migrations = false
</pre>
</div>
</notextile>
<p>Rails will use that particular setting to configure Active Record.</p>
<h4 id="rails-general-configuration">3.1 Rails General Configuration</h4>
<ul>
<li><tt>config.after_initialize</tt> takes a block which will be ran <em>after</em> Rails has finished initializing. Useful for configuring values set up by other initializers:</li>
</ul>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
config.after_initialize do
ActionView::Base.sanitized_allowed_tags.delete 'div'
end
</pre>
</div>
</notextile>
<ul>
<li><tt>config.allow_concurrency</tt> should be set to <tt>true</tt> to allow concurrent (threadsafe) action processing. Set to <tt>false</tt> by default. You probably don’t want to call this one directly, though, because a series of other adjustments need to be made for threadsafe mode to work properly. Can also be enabled with <tt>threadsafe!</tt>.</li>
</ul>
<ul>
<li><tt>config.asset_host</tt> sets the host for the assets. Useful when CDNs are used for hosting assets rather than the application server itself. Shorter version of <tt>config.action_controller.asset_host</tt>.</li>
</ul>
<ul>
<li><tt>config.asset_path</tt> takes a block which configures where assets can be found. Shorter version of <tt>config.action_controller.asset_path</tt>.</li>
</ul>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
config.asset_path = proc { |asset_path| "assets/#{asset_path}" }
</pre>
</div>
</notextile>
<ul>
<li><tt>config.autoload_once_paths</tt> accepts an array of paths from which Rails will automatically load from only once. All elements of this array must also be in <tt>autoload_paths</tt>.</li>
</ul>
<ul>
<li><tt>config.autoload_paths</tt> accepts an array of additional paths to prepend to the load path. By default, all app, lib, vendor and mock paths are included in this list.</li>
</ul>
<ul>
<li><tt>config.cache_classes</tt> controls whether or not application classes should be reloaded on each request. Defaults to <em>true</em> in development, <em>false</em> in test and production. Can also be enabled with <tt>threadsafe!</tt>.</li>
</ul>
<ul>
<li><tt>config.action_view.cache_template_loading</tt> controls whether or not templates should be reloaded on each request. Defaults to whatever is set for config.cache_classes.</li>
</ul>
<ul>
<li><tt>config.cache_store</tt> configures which cache store to use for Rails caching. Options include <tt>:memory_store</tt>, <tt>:file_store</tt>, <tt>:mem_cache_store</tt> or the name of your own custom class. Defaults to <tt>:file_store</tt>.</li>
</ul>
<ul>
<li><tt>config.colorize_logging</tt> specifies whether or not to use <span class="caps">ANSI</span> color codes when logging information. Defaults to <em>true</em>.</li>
</ul>
<ul>
<li><tt>config.consider_all_requests_local</tt> is generally set to <tt>true</tt> during development and <tt>false</tt> during production; if it is set to <tt>true</tt>, then any error will cause detailed debugging information to be dumped in the <span class="caps">HTTP</span> response. For finer-grained control, set this to <tt>false</tt> and implement <tt>local_request?</tt> in controllers to specify which requests should provide debugging information on errors.</li>
</ul>
<ul>
<li><tt>config.controller_paths</tt> configures where Rails can find controllers for this application.</li>
</ul>
<ul>
<li><tt>config.dependency_loading</tt> enables or disables dependency loading during the request cycle. Setting dependency_loading to <em>true</em> will allow new classes to be loaded during a request and setting it to <em>false</em> will disable this behavior. Can also be enabled with <tt>threadsafe!</tt>.</li>
</ul>
<ul>
<li><tt>config.eager_load_paths</tt> accepts an array of paths from which Rails will eager load on boot if cache classes is enabled. Defaults to every folder in the <tt>app</tt> directory of the application. All elements of this array must also be in <tt>load_paths</tt>.</li>
</ul>
<ul>
<li><tt>config.encoding</tt> sets up the application-wide encoding. Defaults to <span class="caps">UTF</span>-8.</li>
</ul>
<ul>
<li><tt>config.filter_parameters</tt> used for filtering out the parameters that you don’t want shown in the logs, such as passwords or credit card numbers.</li>
</ul>
<ul>
<li><tt>config.helper_paths</tt> configures where Rails can find helpers for this application.</li>
</ul>
<ul>
<li><tt>config.log_level</tt> defines the verbosity of the Rails logger. In production mode, this defaults to <tt>:info</tt>. In development mode, it defaults to <tt>:debug</tt>.</li>
</ul>
<ul>
<li><tt>config.log_path</tt> overrides the path to the log file to use. Defaults to <tt>log/#{environment}.log</tt> (e.g. log/development.log or log/production.log).</li>
</ul>
<ul>
<li><tt>config.logger</tt> accepts a logger conforming to the interface of Log4r or the default Ruby 1.8+ Logger class, which is then used to log information from Action Controller. Set to nil to disable logging.</li>
</ul>
<ul>
<li><tt>config.middleware</tt> allows you to configure the application’s middleware. This is covered in depth in the “Configuring Middleware” section below.</li>
</ul>
<ul>
<li><tt>config.plugins</tt> accepts the list of plugins to load. If this is set to nil, all plugins will be loaded. If this is set to [], no plugins will be loaded. Otherwise, plugins will be loaded in the order specified.</li>
</ul>
<ul>
<li><tt>config.preload_frameworks</tt> enables or disables preloading all frameworks at startup. Can also be enabled with <tt>threadsafe!</tt>. Defaults to <tt>nil</tt>, so is disabled.</li>
</ul>
<ul>
<li><tt>config.reload_plugins</tt> enables or disables plugin reloading.</li>
</ul>
<ul>
<li><tt>config.root</tt> configures the root path of the application.</li>
</ul>
<ul>
<li><tt>config.secret_token</tt> used for specifying a key which allows sessions for the application to be verified against a known secure key to prevent tampering.</li>
</ul>
<ul>
<li><tt>config.serve_static_assets</tt> configures Rails to serve static assets. Defaults to <em>true</em>, but in the production environment is turned off. The server software used to run the application should be used to serve the assets instead.</li>
</ul>
<ul>
<li><tt>config.session_store</tt> is usually set up in <tt>config/initializers/session_store.rb</tt> and specifies what class to use to store the session. Custom session stores can be specified like so:</li>
</ul>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
config.session_store = :my_custom_store
</pre>
</div>
</notextile>
<p>This custom store must be defined as <tt>ActionDispatch::Session::MyCustomStore</tt>.</p>
<ul>
<li><tt>config.threadsafe!</tt> enables <tt>allow_concurrency</tt>, <tt>cache_classes</tt>, <tt>dependency_loading</tt> and <tt>preload_frameworks</tt> to make the application threadsafe.</li>
</ul>
<div class='warning'><p>Threadsafe operation is incompatible with the normal workings of development mode Rails. In particular, automatic dependency loading and class reloading are automatically disabled when you call <tt>config.threadsafe!</tt>.</p></div>
<ul>
<li><tt>config.time_zone</tt> sets the default time zone for the application and enables time zone awareness for Active Record.</li>
</ul>
<ul>
<li><tt>config.whiny_nils</tt> enables or disables warnings when any methods of nil are invoked. Defaults to <em>true</em> in development and test environments.</li>
</ul>
<h4 id="configuring-generators">3.2 Configuring Generators</h4>
<p>Rails 3 allows you to alter what generators are used with the <tt>config.generators</tt> method. This method takes a block:</p>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
config.generators do |g|
g.orm :active_record
g.test_framework :test_unit
end
</pre>
</div>
</notextile>
<p>The full set of methods that can be used in this block are as follows:</p>
<ul>
<li><tt>force_plural</tt> allows pluralized model names. Defaults to <em>false</em>.</li>
<li><tt>helper</tt> defines whether or not to generate helpers. Defaults to <em>true</em>.</li>
<li><tt>orm</tt> defines which orm to use. Defaults to <em>nil</em>, so will use Active Record by default.</li>
<li><tt>integration_tool</tt> defines which integration tool to use. Defaults to <em>nil</em>.</li>
<li><tt>performance_tool</tt> defines which performance tool to use. Defaults to <em>nil</em>.</li>
<li><tt>resource_controller</tt> defines which generator to use for generating a controller when using <tt>rails generate resource</tt>. Defaults to <tt>:controller</tt>.</li>
<li><tt>scaffold_controller</tt> different from <tt>resource_controller</tt>, defines which generator to use for generating a <em>scaffolded</em> controller when using <tt>rails generate scaffold</tt>. Defaults to <tt>:scaffold_controller</tt>.</li>
<li><tt>stylesheets</tt> turns on the hook for stylesheets in generators. Used in Rails for when the <tt>scaffold</tt> generator is ran, but this hook can be used in other generates as well.</li>
<li><tt>test_framework</tt> defines which test framework to use. Defaults to <em>nil</em>, so will use Test::Unit by default.</li>
<li><tt>template_engine</tt> defines which template engine to use, such as <span class="caps">ERB</span> or Haml. Defaults to <tt>:erb</tt>.</li>
</ul>
<h4 id="configuring-middleware">3.3 Configuring Middleware</h4>
<p>Every Rails application comes with a standard set of middleware which it uses in this order in the development environment:</p>
<ul>
<li><tt>ActionDispatch::Static</tt> is used to serve static assets. Disabled if <tt>config.serve_static_assets</tt> is <em>true</em>.</li>
<li><tt>Rack::Lock</tt> Will wrap the app in mutex so it can only be called by a single thread at a time. Only enabled if <tt>config.action_controller.allow_concurrency</tt> is set to <em>false</em>, which it is by default.</li>
<li><tt>ActiveSupport::Cache::Strategy::LocalCache</tt> Serves as a basic memory backed cache. This cache is not thread safe and is intended only for serving as a temporary memory cache for a single thread.</li>
<li><tt>Rack::Runtime</tt> Sets an <tt>X-Runtime</tt> header, containing the time (in seconds) taken to execute the request.</li>
<li><tt>Rails::Rack::Logger</tt> Will notify the logs that the request has began. After request is complete, flushes all the logs.</li>
<li><tt>ActionDispatch::ShowExceptions</tt> rescues any exception returned by the application and renders nice exception pages if the request is local or if <tt>config.consider_all_requests_local</tt> is set to <em>true_. If <tt>config.action_dispatch.show</em>exceptions</tt></notextile> is set to <em>false</em>, exceptions will be raised regardless.</li>
<li><tt>ActionDispatch::RemoteIp</tt> checks for IP spoofing attacks. Configurable with the <tt>config.action_dispatch.ip_spoofing_check</tt> and <tt>config.action_dispatch.trusted_proxies</tt> settings.</li>
<li><tt>Rack::Sendfile</tt> The Sendfile middleware intercepts responses whose body is being served from a file and replaces it with a server specific X-Sendfile header. Configurable with <tt>config.action_dispatch.x_sendfile_header</tt></li>
<li><tt>ActionDispatch::Callbacks</tt> Runs the prepare callbacks before serving the request.</li>
<li><tt>ActiveRecord::ConnectionAdapters::ConnectionManagement</tt> cleans active connections after each request, unless the <tt>rack.test</tt> key in the request environment is set to <em>true</em>.</li>
<li><tt>ActiveRecord::QueryCache</tt> caches all <tt>SELECT</tt> queries generated in a request. If an <tt>INSERT</tt> or <tt>UPDATE</tt> takes place then the cache is cleaned.</li>
<li><tt>ActionDispatch::Cookies</tt> sets cookies for the request.</li>
<li><tt>ActionDispatch::Session::CookieStore</tt> is responsible for storing the session in cookies. An alternate middleware can be used for this by changing the <tt>config.action_controller.session_store</tt> to an alternate value. Additionally, options passed to this can be configured by using <tt>config.action_controller.session_options</tt>.</li>
<li><tt>ActionDispatch::Flash</tt> sets up the <tt>flash</tt> keys. Only available if <tt>config.action_controller.session_store</tt> is set to a value.</li>
<li><tt>ActionDispatch::ParamsParser</tt> parses out parameters from the request into <tt>params</tt></li>
<li><tt>Rack::MethodOverride</tt> allows the method to be overridden if <tt>params[:_method]</tt> is set. This is the middleware which supports the <span class="caps">PUT</span> and <span class="caps">DELETE</span> <span class="caps">HTTP</span> method types.</li>
<li><tt>ActionDispatch::Head</tt> converts <span class="caps">HEAD</span> requests to <span class="caps">GET</span> requests and serves them as so.</li>
<li><tt>ActionDispatch::BestStandardsSupport</tt> enables “best standards support” so that IE8 renders some elements correctly.</li>
</ul>
<p>Besides these usual middleware, you can add your own by using the <tt>config.middleware.use</tt> method:</p>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
config.middleware.use Magical::Unicorns
</pre>
</div>
</notextile>
<p>This will put the <tt>Magical::Unicorns</tt> middleware on the end of the stack. If you wish to put this middleware before another use <tt>insert_before</tt>:</p>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
config.middleware.insert_before ActionDispatch::Head, Magical::Unicorns
</pre>
</div>
</notextile>
<p>There’s also <tt>insert_after</tt> which will insert a middleware <em>after</em> another:</p>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
config.middleware.insert_after ActionDispatch::Head, Magical::Unicorns
</pre>
</div>
</notextile>
<p>Middlewares can also be completely swapped out and replaced with others:</p>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
config.middleware.swap ActionDispatch::BestStandardsSupport, Magical::Unicorns
</pre>
</div>
</notextile>
<p>They can also be removed from the stack completely:</p>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
config.middleware.delete ActionDispatch::BestStandardsSupport
</pre>
</div>
</notextile>
<h4 id="configuring-i18n">3.4 Configuring i18n</h4>
<ul>
<li><tt>config.i18n.default_locale</tt> sets the default locale of an application used for i18n. Defaults to <tt>:en</tt>.</li>
</ul>
<ul>
<li><tt>config.i18n.load_path</tt> sets the path Rails uses to look for locale files. Defaults to <tt>config/locales/*.{yml,rb}</tt></li>
</ul>
<h4 id="configuring-active-record">3.5 Configuring Active Record</h4>
<p><tt>config.active_record</tt> includes a variety of configuration options:</p>
<ul>
<li><tt>config.active_record.logger</tt> accepts a logger conforming to the interface of Log4r or the default Ruby 1.8.x Logger class, which is then passed on to any new database connections made. You can retrieve this logger by calling <tt>logger</tt> on either an Active Record model class or an Active Record model instance. Set to nil to disable logging.</li>
</ul>
<ul>
<li><tt>config.active_record.primary_key_prefix_type</tt> lets you adjust the naming for primary key columns. By default, Rails assumes that primary key columns are named <tt>id</tt> (and this configuration option doesn’t need to be set.) There are two other choices:
<ul>
<li><tt>:table_name</tt> would make the primary key for the Customer class <tt>customerid</tt></li>
<li><tt>:table_name_with_underscore</tt> would make the primary key for the Customer class <tt>customer_id</tt></li>
</ul></li>
</ul>
<ul>
<li><tt>config.active_record.table_name_prefix</tt> lets you set a global string to be prepended to table names. If you set this to <tt>northwest_</tt>, then the Customer class will look for <tt>northwest_customers</tt> as its table. The default is an empty string.</li>
</ul>
<ul>
<li><tt>config.active_record.table_name_suffix</tt> lets you set a global string to be appended to table names. If you set this to <tt>_northwest</tt>, then the Customer class will look for <tt>customers_northwest</tt> as its table. The default is an empty string.</li>
</ul>
<ul>
<li><tt>config.active_record.pluralize_table_names</tt> specifies whether Rails will look for singular or plural table names in the database. If set to <tt>true</tt> (the default), then the Customer class will use the <tt>customers</tt> table. If set to <tt>false</tt>, then the Customers class will use the <tt>customer</tt> table.</li>
</ul>
<ul>
<li><tt>config.active_record.default_timezone</tt> determines whether to use <tt>Time.local</tt> (if set to <tt>:local</tt>) or <tt>Time.utc</tt> (if set to <tt>:utc</tt>) when pulling dates and times from the database. The default is <tt>:local</tt>.</li>
</ul>
<ul>
<li><tt>config.active_record.schema_format</tt> controls the format for dumping the database schema to a file. The options are <tt>:ruby</tt> (the default) for a database-independent version that depends on migrations, or <tt>:sql</tt> for a set of (potentially database-dependent) <span class="caps">SQL</span> statements.</li>
</ul>
<ul>
<li><tt>config.active_record.timestamped_migrations</tt> controls whether migrations are numbered with serial integers or with timestamps. The default is <tt>true</tt>, to use timestamps, which are preferred if there are multiple developers working on the same application.</li>
</ul>
<ul>
<li><tt>config.active_record.lock_optimistically</tt> controls whether ActiveRecord will use optimistic locking. By default this is <tt>true</tt>.</li>
</ul>
<p>The MySQL adapter adds one additional configuration option:</p>
<ul>
<li><tt>ActiveRecord::ConnectionAdapters::MysqlAdapter.emulate_booleans</tt> controls whether ActiveRecord will consider all <tt>tinyint(1)</tt> columns in a MySQL database to be booleans. By default this is <tt>true</tt>.</li>
</ul>
<p>The schema dumper adds one additional configuration option:</p>
<ul>
<li><tt>ActiveRecord::SchemaDumper.ignore_tables</tt> accepts an array of tables that should <em>not</em> be included in any generated schema file. This setting is ignored unless <tt>config.active_record.schema_format == :ruby</tt>.</li>
</ul>
<h4 id="configuring-action-controller">3.6 Configuring Action Controller</h4>
<p><tt>config.action_controller</tt> includes a number of configuration settings:</p>
<ul>
<li><tt>config.action_controller.asset_host</tt> sets the host for the assets. Useful when CDNs are used for hosting assets rather than the application server itself.</li>
</ul>
<ul>
<li><tt>config.action_controller.asset_path</tt> takes a block which configures where assets can be found. Shorter version of <tt>config.action_controller.asset_path</tt>.</li>
</ul>
<ul>
<li><tt>config.action_controller.page_cache_directory</tt> should be the document root for the web server and is set using <tt>Base.page_cache_directory = “/document/root”</tt>. For Rails, this directory has already been set to <tt>Rails.public_path</tt> (which is usually set to <tt>Rails.root + “/public”</tt>). Changing this setting can be useful to avoid naming conflicts with files in <tt>public/</tt>, but doing so will likely require configuring your web server to look in the new location for cached files.</li>
</ul>
<ul>
<li><tt>config.action_controller.page_cache_extension</tt> configures the extension used for cached pages saved to <tt>page_cache_directory</tt>. Defaults to <tt>.html</tt></li>
</ul>
<ul>
<li><tt>config.action_controller.perform_caching</tt> configures whether the application should perform caching or not. Set to <em>false</em> in development mode, <em>true</em> in production.</li>
</ul>
<ul>
<li><tt>config.action_controller.default_charset</tt> specifies the default character set for all renders. The default is “utf-8”.</li>
</ul>
<ul>
<li><tt>config.action_controller.logger</tt> accepts a logger conforming to the interface of Log4r or the default Ruby 1.8+ Logger class, which is then used to log information from Action Controller. Set to nil to disable logging.</li>
</ul>
<ul>
<li><tt>config.action_controller.request_forgery_protection_token</tt> sets the token parameter name for RequestForgery. Calling <tt>protect_from_forgery</tt> sets it to <tt>:authenticity_token</tt> by default.</li>
</ul>
<ul>
<li><tt>config.action_controller.allow_forgery_protection</tt> enables or disables <span class="caps">CSRF</span> protection. By default this is <tt>false</tt> in test mode and <tt>true</tt> in all other modes.</li>
</ul>
<ul>
<li><tt>config.action_controller.relative_url_root</tt> can be used to tell Rails that you are deploying to a subdirectory. The default is <tt>ENV['RAILS_RELATIVE_URL_ROOT']</tt>.</li>
</ul>
<p>The caching code adds two additional settings:</p>
<ul>
<li><tt>ActionController::Base.page_cache_directory</tt> sets the directory where Rails will create cached pages for your web server. The default is <tt>Rails.public_path</tt> (which is usually set to <tt>Rails.root + “/public”</tt>).</li>
</ul>
<ul>
<li><tt>ActionController::Base.page_cache_extension</tt> sets the extension to be used when generating pages for the cache (this is ignored if the incoming request already has an extension). The default is <tt>.html</tt>.</li>
</ul>
<p>The Active Record session store can also be configured:</p>
<ul>
<li><tt>ActiveRecord::SessionStore::Session.table_name</tt> sets the name of the table used to store sessions. Defaults to <tt>sessions</tt>.</li>
</ul>
<ul>
<li><tt>ActiveRecord::SessionStore::Session.primary_key</tt> sets the name of the ID column used in the sessions table. Defaults to <tt>session_id</tt>.</li>
</ul>
<ul>
<li><tt>ActiveRecord::SessionStore::Session.data_column_name</tt> sets the name of the column which stores marshaled session data. Defaults to <tt>data</tt>.</li>
</ul>
<h4 id="configuring-action-dispatch">3.7 Configuring Action Dispatch</h4>
<ul>
<li><tt>config.action_dispatch.session_store</tt> sets the name of the store for session data. The default is <tt>:cookie_store</tt>; other valid options include <tt>:active_record_store</tt>, <tt>:mem_cache_store</tt> or the name of your own custom class.</li>
</ul>
<ul>
<li><tt>config.action_dispatch.tld_length</tt> sets the <span class="caps">TLD</span> (top-level domain) length for the application. Defaults to <tt>1</tt>.</li>
</ul>
<ul>
<li><tt>ActionDispatch::Callbacks.before</tt> takes a block of code to run before the request.</li>
</ul>
<ul>
<li><tt>ActionDispatch::Callbacks.to_prepare</tt> takes a block to run after <tt>ActionDispatch::Callbacks.before</tt>, but before the request. Runs for every request in <tt>development</tt> mode, but only once for <tt>production</tt> or environments with <tt>cache_classes</tt> set to <tt>true</tt>.</li>
</ul>
<ul>
<li><tt>ActionDispatch::Callbacks.after</tt> takes a block of code to run after the request.</li>
</ul>
<h4 id="configuring-action-view">3.8 Configuring Action View</h4>
<p>There are only a few configuration options for Action View, starting with four on <tt>ActionView::Base</tt>:</p>
<ul>
<li><tt>config.action_view.debug_rjs</tt> specifies whether <span class="caps">RJS</span> responses should be wrapped in a try/catch block that alerts the caught exception (and then re-raises it). The default is <tt>false</tt>.</li>
</ul>
<ul>
<li><tt>config.action_view.field_error_proc</tt> provides an <span class="caps">HTML</span> generator for displaying errors that come from Active Record. The default is <tt>Proc.new{ |html_tag, instance| <span>Q(</span><div class="field_with_errors">#{html_tag}</div>).html_safe }</tt></li>
</ul>
<ul>
<li><tt>config.action_view.default_form_builder</tt> tells Rails which form builder to use by default. The default is <tt>ActionView::Helpers::FormBuilder</tt>.</li>
</ul>
<ul>
<li><tt>config.action_view.logger</tt> accepts a logger conforming to the interface of Log4r or the default Ruby 1.8+ Logger class, which is then used to log information from Action Mailer. Set to nil to disable logging.</li>
</ul>
<ul>
<li><tt>config.action_view.erb_trim_mode</tt> gives the trim mode to be used by <span class="caps">ERB</span>. It defaults to <tt>'-'</tt>. See the <a href="http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/"><span class="caps">ERB</span> documentation</a> for more information.</li>
</ul>
<ul>
<li><tt>config.action_view.javascript_expansions</tt> is a hash containing expansions that can be used for the JavaScript include tag. By default, this is defined as:</li>
</ul>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
config.action_view.javascript_expansions = { :defaults => ['prototype', 'effects', 'dragdrop', 'controls', 'rails'] }
</pre>
</div>
</notextile>
<p>However, you may add to this by defining others:</p>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
config.action_view.javascript_expansions[:jquery] = ["jquery", "jquery-ui"]
</pre>
</div>
</notextile>
<p>And can reference in the view with the following code:</p>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
<%= javascript_include_tag :jquery %>
</pre>
</div>
</notextile>
<ul>
<li><tt>config.action_view.stylesheet_expansions</tt> works in much the same way as <tt>javascript_expansions</tt>, but has no default key. Keys defined for this hash can be referenced in the view like such:</li>
</ul>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
<%= stylesheet_link_tag :special %>
</pre>
</div>
</notextile>
<ul>
<li><tt>ActionView::Helpers::AssetTagHelper::AssetPaths.cache_asset_ids</tt> With the cache enabled, the asset tag helper methods will make fewer expensive file system calls (the default implementation checks the file system timestamp). However this prevents you from modifying any asset files while the server is running.</li>
</ul>
<h4 id="configuring-action-mailer">3.9 Configuring Action Mailer</h4>
<p>There are a number of settings available on <tt>config.action_mailer</tt>:</p>
<ul>
<li><tt>config.action_mailer.logger</tt> accepts a logger conforming to the interface of Log4r or the default Ruby 1.8+ Logger class, which is then used to log information from Action Mailer. Set to nil to disable logging.</li>
</ul>
<ul>
<li><tt>config.action_mailer.smtp_settings</tt> allows detailed configuration for the <tt>:smtp</tt> delivery method. It accepts a hash of options, which can include any of these options:
<ul>
<li><tt>:address</tt> – Allows you to use a remote mail server. Just change it from its default “localhost” setting.</li>
<li><tt>:port</tt> – On the off chance that your mail server doesn’t run on port 25, you can change it.</li>
<li><tt>:domain</tt> – If you need to specify a <span class="caps">HELO</span> domain, you can do it here.</li>
<li><tt>:user_name</tt> – If your mail server requires authentication, set the username in this setting.</li>
<li><tt>:password</tt> – If your mail server requires authentication, set the password in this setting.</li>
<li><tt>:authentication</tt> – If your mail server requires authentication, you need to specify the authentication type here. This is a symbol and one of <tt>:plain</tt>, <tt>:login</tt>, <tt>:cram_md5</tt>.</li>
</ul></li>
</ul>
<ul>
<li><tt>config.action_mailer.sendmail_settings</tt> allows detailed configuration for the <tt>sendmail</tt> delivery method. It accepts a hash of options, which can include any of these options:
<ul>
<li><tt>:location</tt> – The location of the sendmail executable. Defaults to <tt>/usr/sbin/sendmail</tt>.</li>
<li><tt>:arguments</tt> – The command line arguments. Defaults to <tt>-i -t</tt>.</li>
</ul></li>
</ul>
<ul>
<li><tt>config.action_mailer.raise_delivery_errors</tt> specifies whether to raise an error if email delivery cannot be completed. It defaults to <tt>true</tt>.</li>
</ul>
<ul>
<li><tt>config.action_mailer.delivery_method</tt> defines the delivery method. The allowed values are <tt>:smtp</tt> (default), <tt>:sendmail</tt>, and <tt>:test</tt>.</li>
</ul>
<ul>
<li><tt>config.action_mailer.perform_deliveries</tt> specifies whether mail will actually be delivered. By default this is <tt>true</tt>; it can be convenient to set it to <tt>false</tt> for testing.</li>
</ul>
<ul>
<li><tt>config.action_mailer.default</tt> configures Action Mailer defaults. These default to:</li>
</ul>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
:mime_version => "1.0",
:charset => "UTF-8",
:content_type => "text/plain",
:parts_order => [ "text/plain", "text/enriched", "text/html" ]
</pre>
</div>
</notextile>
<h4 id="configuring-active-resource">3.10 Configuring Active Resource</h4>
<p>There is a single configuration setting available on <tt>config.active_resource</tt>:</p>
<ul>
<li><tt>config.active_resource.logger</tt> accepts a logger conforming to the interface of Log4r or the default Ruby 1.8+ Logger class, which is then used to log information from Active Resource. Set to nil to disable logging.</li>
</ul>
<h4 id="configuring-active-support">3.11 Configuring Active Support</h4>
<p>There are a few configuration options available in Active Support:</p>
<ul>
<li><tt>config.active_support.bare</tt> enables or disables the loading of <tt>active_support/all</tt> when booting Rails. Defaults to <tt>nil</tt>, which means <tt>active_support/all</tt> is loaded.</li>
</ul>
<ul>
<li><tt>config.active_support.escape_html_entities_in_json</tt> enables or disables the escaping of <span class="caps">HTML</span> entities in <span class="caps">JSON</span> serialization. Defaults to <tt>true</tt>.</li>
</ul>
<ul>
<li><tt>config.active_support.use_standard_json_time_format</tt> enables or disables serializing dates to <span class="caps">ISO</span> 8601 format. Defaults to <tt>false</tt>.</li>
</ul>
<ul>
<li><tt>ActiveSupport::BufferedLogger.silencer</tt> is set to <tt>false</tt> to disable the ability to silence logging in a block. The default is <tt>true</tt>.</li>
</ul>
<ul>
<li><tt>ActiveSupport::Cache::Store.logger</tt> specifies the logger to use within cache store operations.</li>
</ul>
<ul>
<li><tt>ActiveSupport::Deprecation.behavior</tt> alternative setter to <tt>config.active_support.deprecation</tt> which configures the behavior of deprecation warnings for Rails.</li>
</ul>
<ul>
<li><tt>ActiveSupport::Deprecation.silence</tt> takes a block in which all deprecation warnings are silenced.</li>
</ul>
<ul>
<li><tt>ActiveSupport::Deprecation.silenced</tt> sets whether or not to display deprecation warnings.</li>
</ul>
<ul>
<li><tt>ActiveSupport::Logger.silencer</tt> is set to <tt>false</tt> to disable the ability to silence logging in a block. The default is <tt>true</tt>.</li>
</ul>
<h3 id="rails-environment-settings">4 Rails Environment Settings</h3>
<p>Some parts of Rails can also be configured externally by supplying environment variables. The following environment variables are recognized by various parts of Rails:</p>
<ul>
<li><tt>ENV["RAILS_ENV"]</tt> defines the Rails environment (production, development, test, and so on) that Rails will run under.</li>
</ul>
<ul>
<li><tt>ENV["RAILS_RELATIVE_URL_ROOT"]</tt> is used by the routing code to recognize URLs when you deploy your application to a subdirectory.</li>
</ul>
<ul>
<li><tt>ENV["RAILS_ASSET_ID"]</tt> will override the default cache-busting timestamps that Rails generates for downloadable assets.</li>
</ul>
<ul>
<li><tt>ENV["RAILS_CACHE_ID"]</tt> and <tt>ENV["RAILS_APP_VERSION"]</tt> are used to generate expanded cache keys in Rails’ caching code. This allows you to have multiple separate caches from the same application.</li>
</ul>
<h3 id="using-initializer-files">5 Using Initializer Files</h3>
<p>After loading the framework and any gems and plugins in your application, Rails turns to loading initializers. An initializer is any file of Ruby code stored under <tt>config/initializers</tt> in your application. You can use initializers to hold configuration settings that should be made after all of the frameworks, plugins and gems are loaded, such as options to configure settings for these parts.</p>
<div class='note'><p>You can use subfolders to organize your initializers if you like, because Rails will look into the whole file hierarchy from the initializers folder on down.</p></div>
<div class='info'><p>If you have any ordering dependency in your initializers, you can control the load order by naming. For example, <tt>01_critical.rb</tt> will be loaded before <tt>02_normal.rb</tt>.</p></div>
<h3 id="initialization-events">6 Initialization events</h3>
<p>Rails has 5 initialization events which can be hooked into (listed in order that they are ran):</p>
<ul>
<li><tt>before_configuration</tt>: This is run as soon as the application constant inherits from <tt>Rails::Application</tt>. The <tt>config</tt> calls are evaluated before this happens.</li>
</ul>
<ul>
<li><tt>before_initialize</tt>: This is run directly before the initialization process of the application occurs with the <tt>:bootstrap_hook</tt> initializer near the beginning of the Rails initialization process.</li>
</ul>
<ul>
<li><tt>to_prepare</tt>: Run after the initializers are ran for all Railties (including the application itself), but before eager loading and the middleware stack is built.</li>
</ul>
<ul>
<li><tt>before_eager_load</tt>: This is run directly before eager loading occurs, which is the default behaviour for the <em>production</em> environment and not for the <tt>development</tt> enviroment.</li>
</ul>
<ul>
<li><tt>after_initialize</tt>: Run directly after the initialization of the application, but before the application initializers are run.</li>
</ul>
<div class='warning'><p>Some parts of your application, notably observers and routing, are not yet set up at the point where the <tt>after_initialize</tt> block is called.</p></div>
<h4 id="rails-railtie-initializer">6.1 <tt>Rails::Railtie#initializer</tt></h4>
<p>Rails has several initializers that run on startup that are all defined by using the <tt>initializer</tt> method from <tt>Rails::Railtie</tt>. Here’s an example of the <tt>initialize_whiny_nils</tt> initializer from Active Support:</p>
<notextile>
<div class="code_container">
<pre class="brush: ruby; gutter: false; toolbar: false">
initializer "active_support.initialize_whiny_nils" do |app|
require 'active_support/whiny_nil' if app.config.whiny_nils
end
</pre>
</div>
</notextile>
<p>The <tt>initializer</tt> method takes three arguments with the first being the name for the initializer and the second being an options hash (not shown here) and the third being a block. The <tt>:before</tt> key in the options hash can be specified to specify which initializer this new initializer must run before, and the <tt>:after</tt> key will specify which initializer to run this initializer <em>after</em>.</p>
<p>Initializers defined using the <tt>initializer</tt> method will be ran in the order they are defined in, with the exception of ones that use the <tt>:before</tt> or <tt>:after</tt> methods.</p>
<div class='warning'><p>You may put your initializer before or after any other initializer in the chain, as long as it is logical. Say you have 4 initializers called “one” through “four” (defined in that order) and you define “four” to go <em>before</em> “four” but <em>after</em> “three”, that just isn’t logical and Rails will not be able to determine your initializer order.</p></div>
<p>The block’s argument of the <tt>initialize</tt> is the instance of the application itself, and so we can access the configuration on it by using the <tt>config</tt> method as this initializer does.</p>
<p>Because <tt>Rails::Application</tt> inherits from <tt>Rails::Railtie</tt> (indirectly), you can use the <tt>initializer</tt> method in <tt>config/application.rb</tt> to define initializers for the application.</p>
<h4 id="initializers">6.2 Initializers</h4>
<p>Below is a comprehensive list of all the initializers found in Rails in the order that they are defined (and therefore run in, unless otherwise stated).</p>
<p><strong><tt>load_environment_hook</tt></strong>
Serves as a placeholder so that <tt>:load_environment_config</tt> can be defined to run before it.</p>
<p><strong><tt>load_active_support</tt></strong> Requires <tt>active_support/dependencies</tt> which sets up the basis for Active Support. Optionally requires <tt>active_support/all</tt> if <tt>config.active_support.bare</tt> is un-truthful, which is the default.</p>
<p><strong><tt>preload_frameworks</tt></strong> Will load all autoload dependencies of Rails automatically if <tt>config.preload_frameworks</tt> is <tt>true</tt> or “truthful”. By default this configuration option is disabled. In Rails, when internal classes are referenced for the first time they are autoloaded. <tt>:preload_frameworks</tt> loads all of this at once on initialization.</p>
<p><strong><tt>initialize_logger</tt></strong> Initializes the logger (an <tt>ActiveSupport::BufferedLogger</tt> object) for the application and makes it accessible at <tt>Rails.logger</tt>, providing that there’s no initializer inserted before this point that has defined <tt>Rails.logger</tt>.</p>
<p><strong><tt>initialize_cache</tt></strong> If <tt>RAILS_CACHE</tt> isn’t yet set, initializes the cache by referencing the value in <tt>config.cache_store</tt> and stores the outcome as <tt>RAILS_CACHE</tt>. If this object responds to the <tt>middleware</tt> method, its middleware is inserted before <tt>Rack::Runtime</tt> in the middleware stack.</p>
<p><strong><tt>set_clear_dependencies_hook</tt></strong> Provides a hook for <tt>active_record.set_dispatch_hooks</tt> to use, which will run before this initializer. This initializer — which runs only if <tt>cache_classes</tt> is set to <tt>false</tt> — uses <tt>ActionDispatch::Callbacks.after</tt> to remove the constants which have been referenced during the request from the object space so that they will be reloaded during the following request.</p>
<p><strong><tt>initialize_dependency_mechanism</tt></strong> If <tt>config.cache_classes</tt> is set to <tt>true</tt>, configures <tt>ActiveSupport::Dependencies.mechanism</tt> to <tt>require</tt> dependencies rather than <tt>load</tt> them.</p>
<p><strong><tt>bootstrap_hook</tt></strong> Runs all configured <tt>before_initialize</tt> blocks.</p>
<p><strong><tt>i18n.callbacks</tt></strong> In the development environment, sets up a <tt>to_prepare</tt> callback which will call <tt>I18n.reload!</tt> if any of the locales have changed since the last request. In production mode this callback will only run on the first request.</p>
<p><strong><tt>active_support.initialize_whiny_nils</tt></strong> Will require <tt>active_support/whiny_nil</tt> if <tt>config.whiny_nils</tt> is set to <tt>true</tt>. This file will output errors such as:</p>
<notextile>
<div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
</pre>
</div>
</notextile>
<p>And:</p>
<notextile>
<div class="code_container">
<pre class="brush: plain; gutter: false; toolbar: false">
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each
</pre>
</div>
</notextile>
<p><strong><tt>active_support.deprecation_behavior</tt></strong> Sets up deprecation reporting for environments, defaulting to <tt>:log</tt> for development, <tt>:notify</tt> for production and <tt>:stderr</tt> for test. If a value isn’t set for <tt>config.active_support.deprecation</tt> then this initializer will prompt the user to configure this line in the current environment’s <tt>config/environments</tt> file. Can be set to an array of values.</p>
<p><strong><tt>active_support.initialize_time_zone</tt></strong> Sets the default time zone for the application based off the <tt>config.time_zone</tt> setting, which defaults to “<span class="caps">UTC</span>”.</p>
<p><strong><tt>action_dispatch.configure</tt></strong> Configures the <tt>ActionDispatch::Http::URL.tld_length</tt> to be set to the value of <tt>config.action_dispatch.tld_length</tt>.</p>
<p><strong><tt>action_view.cache_asset_ids</tt></strong> Will set <tt>ActionView::Helpers::AssetTagHelper::AssetPaths.cache_asset_ids</tt> to <tt>false</tt> when Active Support loads, but only if <tt>config.cache_classes</tt> is too.</p>
<p><strong><tt>action_view.javascript_expansions</tt></strong> Registers the expansions set up by <tt>config.action_view.javascript_expansions</tt> and <tt>config.action_view.stylesheet_expansions</tt> to be recognised by Action View and therefore usable in the views.</p>
<p><strong><tt>action_view.set_configs</tt></strong> Sets up Action View by using the settings in <tt>config.action_view</tt> by <tt>send</tt>’ing the method names as setters to <tt>ActionView::Base</tt> and passing the values through.</p>
<p><strong><tt>action_controller.logger</tt></strong> Sets <tt>ActionController::Base.logger</tt> — if it’s not already set — to <tt>Rails.logger</tt>.</p>
<p><strong><tt>action_controller.initialize_framework_caches</tt></strong> Sets <tt>ActionController::Base.cache_store</tt> — if it’s not already set — to <tt>RAILS_CACHE</tt>.</p>
<p><strong><tt>action_controller.set_configs</tt></strong> Sets up Action Controller by using the settings in <tt>config.action_controller</tt> by <tt>send</tt>’ing the method names as setters to <tt>ActionController::Base</tt> and passing the values through.</p>
<p><strong><tt>action_controller.compile_config_methods</tt></strong> Initializes methods for the config settings specified so that they are quicker to access.</p>
<p><strong><tt>active_record.initialize_timezone</tt></strong> Sets <tt>ActiveRecord::Base.time_zone_aware_attributes</tt> to true, as well as setting <tt>ActiveRecord::Base.default_timezone</tt> to <span class="caps">UTC</span>. When attributes are read from the database, they will be converted into the time zone specified by <tt>Time.zone</tt>.</p>
<p><strong><tt>active_record.logger</tt></strong> Sets <tt>ActiveRecord::Base.logger</tt> — if it’s not already set — to <tt>Rails.logger</tt>.</p>
<p><strong><tt>active_record.set_configs</tt></strong> Sets up Active Record by using the settings in <tt>config.active_record</tt> by <tt>send</tt>’ing the method names as setters to <tt>ActiveRecord::Base</tt> and passing the values through.</p>
<p><strong><tt>active_record.initialize_database</tt></strong> Loads the database configuration (by default) from <tt>config/database.yml</tt> and establishes a connection for the current environment.</p>
<p><strong><tt>active_record.log_runtime</tt></strong> Includes <tt>ActiveRecord::Railties::ControllerRuntime</tt> which is responsible for reporting the length of time Active Record calls took for the request back to the logger.</p>
<p><strong><tt>active_record.set_dispatch_hooks</tt></strong> If <tt>config.cache_classes</tt> is set to false, all reloadable connections to the database will be reset.</p>
<p><strong><tt>action_mailer.logger</tt></strong> Sets <tt>ActionMailer::Base.logger</tt> — if it’s not already set — to <tt>Rails.logger</tt>.</p>
<p><strong><tt>action_mailer.set_configs</tt></strong> Sets up Action Mailer by using the settings in <tt>config.action_mailer</tt> by <tt>send</tt>’ing the method names as setters to <tt>ActionMailer::Base</tt> and passing the values through.</p>
<p><strong><tt>action_mailer.compile_config_methods</tt></strong> Initializes methods for the config settings specified so that they are quicker to access.</p>
<p><strong><tt>active_resource.set_configs</tt></strong> Sets up Active Resource by using the settings in <tt>config.active_resource</tt> by <tt>send</tt>’ing the method names as setters to <tt>ActiveResource::Base</tt> and passing the values through.</p>
<p><strong><tt>set_load_path</tt></strong> This initializer runs before <tt>bootstrap_hook</tt>. Adds the <tt>vendor</tt>, <tt>lib</tt>, all directories of <tt>app</tt> and any paths specified by <tt>config.load_paths</tt> to <tt>$LOAD_PATH</tt>.</p>
<p><strong><tt>set_autoload_path</tt></strong> This initializer runs before <tt>bootstrap_hook</tt>. Adds all sub-directories of <tt>app</tt> and paths specified by <tt>config.autoload_paths</tt> to <tt>ActiveSupport::Dependencies.autoload_paths</tt>.</p>
<p><strong><tt>add_routing_paths</tt></strong> Loads (by default) all <tt>config/routes.rb</tt> files (in the application and railties, including engines) and sets up the routes for the application.</p>
<p><strong><tt>add_locales</tt></strong> Adds the files in <tt>config/locales</tt> (from the application, railties and engines) to <tt>I18n.load_path</tt>, making available the translations in these files.</p>
<p><strong><tt>add_view_paths</tt></strong> Adds the directory <tt>app/views</tt> from the application, railties and engines to the lookup path for view files for the application.</p>
<p><strong><tt>load_environment_config</tt></strong> Loads the <tt>config/environments</tt> file for the current environment.</p>
<p><strong><tt>append_asset_paths</tt></strong> Finds asset paths for the application and all attached railties and keeps a track of the available directories in <tt>config.static_asset_paths</tt>.</p>
<p><strong><tt>prepend_helpers_path</tt></strong> Adds the directory <tt>app/helpers</tt> from the application, railties and engines to the lookup path for helpers for the application.</p>
<p><strong><tt>load_config_initializers</tt></strong> Loads all Ruby files from <tt>config/initializers</tt> in the application, railties and engines. The files in this directory can be used to hold configuration settings that should be made after all of the frameworks and plugins are loaded.</p>
<div class='note'><p>You can use subfolders to organize your initializers if you like, because Rails will look into the whole file hierarchy from the <tt>initializers</tt> folder on down.</p></div>
<div class='info'><p>If you have any ordering dependency in your initializers, you can control the load order by naming. For example, <tt>01_critical.rb</tt> will be loaded before <tt>02_normal.rb</tt>.</p></div>
<p><strong><tt>engines_blank_point</tt></strong> Provides a point-in-initialization to hook into if you wish to do anything before engines are loaded. After this point, all railtie and engine initializers are ran.</p>
<p><strong><tt>add_generator_templates</tt></strong> Finds templates for generators at <tt>lib/templates</tt> for the application, railities and engines and adds these to the <tt>config.generators.templates</tt> setting, which will make the templates available for all generators to reference.</p>
<p><strong><tt>ensure_autoload_once_paths_as_subset</tt></strong> Ensures that the <tt>config.autoload_once_paths</tt> only contains paths from <tt>config.autoload_paths</tt>. If it contains extra paths, then an exception will be raised.</p>
<p><strong><tt>add_to_prepare_blocks</tt></strong> The block for every <tt>config.to_prepare</tt> call in the application, a railtie or engine is added to the <tt>to_prepare</tt> callbacks for Action Dispatch which will be ran per request in development, or before the first request in production.</p>
<p><strong><tt>add_builtin_route</tt></strong> If the application is running under the development environment then this will append the route for <tt>rails/info/properties</tt> to the application routes. This route provides the detailed information such as Rails and Ruby version for <tt>public/index.html</tt> in a default Rails application.</p>
<p><strong><tt>build_middleware_stack</tt></strong> Builds the middleware stack for the application, returning an object which has a <tt>call</tt> method which takes a Rack environment object for the request.</p>
<p><strong><tt>eager_load!</tt></strong> If <tt>config.cache_classes</tt> is <tt>true</tt>, runs the <tt>config.before_eager_load</tt> hooks and then calls <tt>eager_load!</tt> which will load all the Ruby files from <tt>config.eager_load_paths</tt>.</p>
<p><strong><tt>finisher_hook</tt></strong> Provides a hook for after the initialization of process of the application is complete, as well as running all the <tt>config.after_initialize</tt> blocks for the application, railties and engines.</p>
<p><strong><tt>set_routes_reloader</tt></strong> Configures Action Dispatch to reload the routes file using <tt>ActionDispatch::Callbacks.to_prepare</tt>.</p>
<p><strong><tt>disable_dependency_loading</tt></strong> Disables the automatic dependency loading if the <tt>config.cache_classes</tt> is set to <tt>true</tt> and <tt>config.dependency_loading</tt> is set to <tt>false</tt>.</p>
<h3 id="changelog">7 Changelog</h3>
<ul>
<li>December 3, 2010: Added initialization events for Rails 3 (<a href="http://ryanbigg.com">Ryan Bigg</a>)</li>
<li>November 26, 2010: Removed all config settings not available in Rails 3 (<a href="http://ryanbigg.com">Ryan Bigg</a>)</li>
<li>August 13, 2009: Updated with config syntax and added general configuration options by “John Pignata”</li>
<li>January 3, 2009: First reasonably complete draft by <a href="credits.html#mgunderloy">Mike Gunderloy</a></li>
<li>November 5, 2008: Rough outline by <a href="credits.html#mgunderloy">Mike Gunderloy</a></li>
</ul>
<h3>Feedback</h3>
<p>
You're encouraged to help in keeping the quality of this guide.
</p>
<p>
If you see any typos or factual errors you are confident to
patch, please clone <a href="https://github.com/lifo/docrails">docrails</a>
and push the change yourself. That branch of Rails has public write access.
Commits are still reviewed, but that happens after you've submitted your
contribution. <a href="https://github.com/lifo/docrails">docrails</a> is
cross-merged with master periodically.
</p>
<p>
You may also find incomplete content, or stuff that is not up to date.
Please do add any missing documentation for master. Check the
<a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a>
for style and conventions.
</p>
<p>
Issues may also be reported in <a href="https://github.com/lifo/docrails/issues">Github</a>.
</p>
<p>And last but not least, any kind of discussion regarding Ruby on Rails
documentation is very welcome in the <a href="http://groups.google.com/group/rubyonrails-docs">rubyonrails-docs mailing list</a>.
</p>
</div>
</div>
</div>
<hr class="hide" />
<div id="footer">
<div class="wrapper">
<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>
<p>"Rails", "Ruby on Rails", and the Rails logo are trademarks of David Heinemeier Hansson. All rights reserved.</p>
</div>
</div>
<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()
</script>
</body>
</html>