railties 3.0.3 → 3.0.4.rc1
Sign up to get free protection for your applications and to get access to all the features.
- data/guides/assets/stylesheets/main.css +1 -1
- data/guides/rails_guides/helpers.rb +2 -7
- data/guides/source/3_0_release_notes.textile +2 -2
- data/guides/source/action_controller_overview.textile +0 -4
- data/guides/source/action_mailer_basics.textile +9 -11
- data/guides/source/action_view_overview.textile +0 -2
- data/guides/source/active_record_querying.textile +9 -57
- data/guides/source/active_record_validations_callbacks.textile +4 -6
- data/guides/source/active_support_core_extensions.textile +49 -2
- data/guides/source/api_documentation_guidelines.textile +1 -1
- data/guides/source/association_basics.textile +0 -2
- data/guides/source/caching_with_rails.textile +0 -1
- data/guides/source/command_line.textile +2 -3
- data/guides/source/configuring.textile +332 -64
- data/guides/source/contribute.textile +1 -1
- data/guides/source/contributing_to_ruby_on_rails.textile +370 -0
- data/guides/source/debugging_rails_applications.textile +0 -2
- data/guides/source/form_helpers.textile +0 -2
- data/guides/source/generators.textile +262 -9
- data/guides/source/getting_started.textile +0 -2
- data/guides/source/i18n.textile +13 -18
- data/guides/source/index.html.erb +9 -9
- data/guides/source/initialization.textile +4 -4
- data/guides/source/layout.html.erb +27 -2
- data/guides/source/layouts_and_rendering.textile +10 -16
- data/guides/source/migrations.textile +0 -2
- data/guides/source/performance_testing.textile +1 -3
- data/guides/source/plugins.textile +0 -2
- data/guides/source/rails_application_templates.textile +0 -2
- data/guides/source/rails_on_rack.textile +0 -2
- data/guides/source/routing.textile +13 -15
- data/guides/source/security.textile +3 -5
- data/guides/source/testing.textile +16 -12
- data/lib/rails/generators.rb +1 -1
- data/lib/rails/generators/actions.rb +1 -1
- data/lib/rails/generators/base.rb +1 -1
- data/lib/rails/generators/rails/app/app_generator.rb +2 -8
- data/lib/rails/generators/rails/app/templates/Gemfile +1 -1
- data/lib/rails/generators/rails/app/templates/config/boot.rb +3 -10
- data/lib/rails/generators/rails/app/templates/config/databases/sqlite3.yml +1 -1
- data/lib/rails/generators/rails/app/templates/gitignore +1 -1
- data/lib/rails/rack/logger.rb +1 -1
- data/lib/rails/railtie.rb +2 -2
- data/lib/rails/test_help.rb +0 -11
- data/lib/rails/version.rb +4 -3
- metadata +22 -17
- data/guides/source/contributing_to_rails.textile +0 -308
@@ -333,7 +333,7 @@ h6 {
|
|
333
333
|
padding: 0.125em 0 0.25em 28px;*/
|
334
334
|
}
|
335
335
|
|
336
|
-
#mainCol dd.
|
336
|
+
#mainCol dd.work-in-progress, #subCol dd.work-in-progress {
|
337
337
|
background: #fff9d8 url(../images/tab_yellow.gif) no-repeat left top;
|
338
338
|
border: none;
|
339
339
|
padding: 1.25em 1em 1.25em 48px;
|
@@ -4,19 +4,14 @@ module RailsGuides
|
|
4
4
|
link = content_tag(:a, :href => url) { name }
|
5
5
|
result = content_tag(:dt, link)
|
6
6
|
|
7
|
-
if
|
8
|
-
result << content_tag(:dd,
|
7
|
+
if options[:work_in_progress]
|
8
|
+
result << content_tag(:dd, 'Work in progress', :class => 'work-in-progress')
|
9
9
|
end
|
10
10
|
|
11
11
|
result << content_tag(:dd, capture(&block))
|
12
12
|
result
|
13
13
|
end
|
14
14
|
|
15
|
-
def lh(id, label = "Lighthouse Ticket")
|
16
|
-
url = "http://rails.lighthouseapp.com/projects/16213/tickets/#{id}"
|
17
|
-
content_tag(:a, label, :href => url)
|
18
|
-
end
|
19
|
-
|
20
15
|
def author(name, nick, image = 'credits_pic_blank.gif', &block)
|
21
16
|
image = "images/#{image}"
|
22
17
|
|
@@ -271,10 +271,10 @@ end
|
|
271
271
|
|
272
272
|
<ruby>
|
273
273
|
scope 'es' do
|
274
|
-
resources :projects, :path_names => { :edit => 'cambiar' }, :path => '
|
274
|
+
resources :projects, :path_names => { :edit => 'cambiar' }, :path => 'proyecto'
|
275
275
|
end
|
276
276
|
|
277
|
-
# Gives you the edit action with /es/
|
277
|
+
# Gives you the edit action with /es/proyecto/1/cambiar
|
278
278
|
</ruby>
|
279
279
|
|
280
280
|
* Added +root+ method to the router as a short cut for <tt>match '/', :to => path</tt>.
|
@@ -481,8 +481,6 @@ end
|
|
481
481
|
|
482
482
|
Again, this is not an ideal example for this filter, because it's not run in the scope of the controller but gets the controller passed as an argument. The filter class has a class method +filter+ which gets run before or after the action, depending on if it's a before or after filter. Classes used as around filters can also use the same +filter+ method, which will get run in the same way. The method must +yield+ to execute the action. Alternatively, it can have both a +before+ and an +after+ method that are run before and after the action.
|
483
483
|
|
484
|
-
The Rails API documentation has "more information on using filters":http://api.rubyonrails.org/classes/ActionController/Filters/ClassMethods.html.
|
485
|
-
|
486
484
|
h3. Verification
|
487
485
|
|
488
486
|
Verifications make sure certain criteria are met in order for a controller or action to run. They can specify that a certain key (or several keys in the form of an array) is present in the +params+, +session+ or +flash+ hashes or that a certain HTTP method was used or that the request was made using +XMLHttpRequest+ (Ajax). The default action taken when these criteria are not met is to render a 400 Bad Request response, but you can customize this by specifying a redirect URL or rendering something else and you can also add flash messages and HTTP headers to the response. It is described in the "API documentation":http://api.rubyonrails.org/classes/ActionController/Verification/ClassMethods.html as "essentially a special kind of before_filter".
|
@@ -817,8 +815,6 @@ NOTE: Certain exceptions are only rescuable from the +ApplicationController+ cla
|
|
817
815
|
|
818
816
|
h3. Changelog
|
819
817
|
|
820
|
-
"Lighthouse Ticket":http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/17
|
821
|
-
|
822
818
|
* February 17, 2009: Yet another proofread by Xavier Noria.
|
823
819
|
|
824
820
|
* November 4, 2008: First release version by Tore Darell
|
@@ -144,7 +144,7 @@ This provides a much simpler implementation that does not require the registerin
|
|
144
144
|
|
145
145
|
The method +welcome_email+ returns a Mail::Message object which can then just be told +deliver+ to send itself out.
|
146
146
|
|
147
|
-
NOTE: In previous versions of Rails, you would call +deliver_welcome_email+ or +create_welcome_email
|
147
|
+
NOTE: In previous versions of Rails, you would call +deliver_welcome_email+ or +create_welcome_email+. This has been deprecated in Rails 3.0 in favour of just calling the method name itself.
|
148
148
|
|
149
149
|
WARNING: Sending out one email should only take a fraction of a second, if you are planning on sending out many emails, or you have a slow domain resolution service, you might want to investigate using a background process like delayed job.
|
150
150
|
|
@@ -154,7 +154,7 @@ Action Mailer now handles the auto encoding of multibyte characters inside of he
|
|
154
154
|
|
155
155
|
If you are using UTF-8 as your character set, you do not have to do anything special, just go ahead and send in UTF-8 data to the address fields, subject, keywords, filenames or body of the email and ActionMailer will auto encode it into quoted printable for you in the case of a header field or Base64 encode any body parts that are non US-ASCII.
|
156
156
|
|
157
|
-
For more complex examples
|
157
|
+
For more complex examples such as defining alternate character sets or self encoding text first, please refer to the Mail library.
|
158
158
|
|
159
159
|
h4. Complete List of Action Mailer Methods
|
160
160
|
|
@@ -213,9 +213,7 @@ NOTE: If you specify an encoding, Mail will assume that your content is already
|
|
213
213
|
|
214
214
|
h5. Making Inline Attachments
|
215
215
|
|
216
|
-
|
217
|
-
|
218
|
-
ActionMailer now makes inline attachments as trivial as they should be.
|
216
|
+
ActionMailer 3.0 makes inline attachments, which involved a lot of hacking in pre 3.0 versions, much simpler and trivial as they should be.
|
219
217
|
|
220
218
|
* Firstly, to tell Mail to turn an attachment into an inline attachment, you just call <tt>#inline</tt> on the attachments method within your Mailer:
|
221
219
|
|
@@ -244,7 +242,7 @@ end
|
|
244
242
|
|
245
243
|
h4. Mailer Views
|
246
244
|
|
247
|
-
Mailer views are located in the +app/views/name_of_mailer_class+ directory. The specific mailer view is known to the class because
|
245
|
+
Mailer views are located in the +app/views/name_of_mailer_class+ directory. The specific mailer view is known to the class because its name is the same as the mailer method. In our example from above, our mailer view for the +welcome_email+ method will be in +app/views/user_mailer/welcome_email.html.erb+ for the HTML version and +welcome_email.text.erb+ for the plain text version.
|
248
246
|
|
249
247
|
To change the default mailer view for your action you do something like:
|
250
248
|
|
@@ -411,9 +409,9 @@ h3. Action Mailer Configuration
|
|
411
409
|
The following configuration options are best made in one of the environment files (environment.rb, production.rb, etc...)
|
412
410
|
|
413
411
|
|template_root|Determines the base from which template references will be made.|
|
414
|
-
|logger|
|
415
|
-
|smtp_settings|Allows detailed configuration for :smtp delivery method
|
416
|
-
|sendmail_settings|Allows you to override options for the :sendmail delivery method
|
412
|
+
|logger|Generates information on the mailing run if available. Can be set to nil for no logging. Compatible with both Ruby's own Logger and Log4r loggers.|
|
413
|
+
|smtp_settings|Allows detailed configuration for :smtp delivery method:<ul><li>:address - Allows you to use a remote mail server. Just change it from its default "localhost" setting.</li><li>:port - On the off chance that your mail server doesn't run on port 25, you can change it.</li><li>:domain - If you need to specify a HELO domain, you can do it here.</li><li>:user_name - If your mail server requires authentication, set the username in this setting.</li><li>:password - If your mail server requires authentication, set the password in this setting.</li><li>:authentication - If your mail server requires authentication, you need to specify the authentication type here. This is a symbol and one of :plain, :login, :cram_md5.</li></ul>|
|
414
|
+
|sendmail_settings|Allows you to override options for the :sendmail delivery method.<ul><li>:location - The location of the sendmail executable. Defaults to /usr/sbin/sendmail.</li><li>:arguments - The command line arguments to be passed to sendmail. Defaults to -i -t.</li></ul>|
|
417
415
|
|raise_delivery_errors|Whether or not errors should be raised if the email fails to be delivered.|
|
418
416
|
|delivery_method|Defines a delivery method. Possible values are :smtp (default), :sendmail, and :test.|
|
419
417
|
|perform_deliveries|Determines whether deliver_* methods are actually carried out. By default they are, but this can be turned off to help functional testing.|
|
@@ -474,8 +472,8 @@ class UserMailerTest < ActionMailer::TestCase
|
|
474
472
|
end
|
475
473
|
</ruby>
|
476
474
|
|
477
|
-
In the test we send the email and store the returned object in the +email+ variable. We then ensure that it was sent (the first assert), then, in the second batch of assertions, we ensure that the email does indeed contain
|
475
|
+
In the test we send the email and store the returned object in the +email+ variable. We then ensure that it was sent (the first assert), then, in the second batch of assertions, we ensure that the email does indeed contain what we expect.
|
478
476
|
|
479
477
|
h3. Changelog
|
480
478
|
|
481
|
-
"
|
479
|
+
* September 30, 2010: Fixed typos and reformatted Action Mailer configuration table for better understanding. "Jaime Iniesta":http://jaimeiniesta.com
|
@@ -1475,7 +1475,5 @@ You can read more about the Rails Internationalization (I18n) API "here":i18n.ht
|
|
1475
1475
|
|
1476
1476
|
h3. Changelog
|
1477
1477
|
|
1478
|
-
"Lighthouse Ticket":http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/71
|
1479
|
-
|
1480
1478
|
* September 3, 2009: Continuing work by Trevor Turk, leveraging the "Action Pack docs":http://ap.rubyonrails.org/ and "What's new in Edge Rails":http://ryandaigle.com/articles/2007/8/3/what-s-new-in-edge-rails-partials-get-layouts
|
1481
1479
|
* April 5, 2009: Starting work by Trevor Turk, leveraging Mike Gunderloy's docs
|
@@ -65,6 +65,7 @@ The methods are:
|
|
65
65
|
* +lock+
|
66
66
|
* +readonly+
|
67
67
|
* +from+
|
68
|
+
* +having+
|
68
69
|
|
69
70
|
All of the above methods return an instance of <tt>ActiveRecord::Relation</tt>.
|
70
71
|
|
@@ -103,7 +104,7 @@ h5. +first+
|
|
103
104
|
|
104
105
|
<ruby>
|
105
106
|
client = Client.first
|
106
|
-
=> #<Client id: 1, first_name:
|
107
|
+
=> #<Client id: 1, first_name: "Lifo">
|
107
108
|
</ruby>
|
108
109
|
|
109
110
|
SQL equivalent of the above is:
|
@@ -120,7 +121,7 @@ h5. +last+
|
|
120
121
|
|
121
122
|
<ruby>
|
122
123
|
client = Client.last
|
123
|
-
=> #<Client id: 221, first_name:
|
124
|
+
=> #<Client id: 221, first_name: "Russel">
|
124
125
|
</ruby>
|
125
126
|
|
126
127
|
SQL equivalent of the above is:
|
@@ -231,7 +232,7 @@ WARNING: Building your own conditions as pure strings can leave you vulnerable t
|
|
231
232
|
|
232
233
|
h4. Array Conditions
|
233
234
|
|
234
|
-
Now what if that number could vary, say as an argument from somewhere
|
235
|
+
Now what if that number could vary, say as an argument from somewhere? The find then becomes something like:
|
235
236
|
|
236
237
|
<ruby>
|
237
238
|
Client.where("orders_count = ?", params[:orders])
|
@@ -279,62 +280,15 @@ h5(#array-range_conditions). Range Conditions
|
|
279
280
|
If you're looking for a range inside of a table (for example, users created in a certain timeframe) you can use the conditions option coupled with the +IN+ SQL statement for this. If you had two dates coming in from a controller you could do something like this to look for a range:
|
280
281
|
|
281
282
|
<ruby>
|
282
|
-
Client.where(
|
283
|
-
(params[:start_date].to_date)..(params[:end_date].to_date))
|
283
|
+
Client.where(:created_at => (params[:start_date].to_date)..(params[:end_date].to_date))
|
284
284
|
</ruby>
|
285
285
|
|
286
|
-
This
|
286
|
+
This query will generate something similar to the following SQL:
|
287
287
|
|
288
288
|
<sql>
|
289
|
-
SELECT
|
290
|
-
('2007-12-31','2008-01-01','2008-01-02','2008-01-03','2008-01-04','2008-01-05',
|
291
|
-
'2008-01-06','2008-01-07','2008-01-08','2008-01-09','2008-01-10','2008-01-11',
|
292
|
-
'2008-01-12','2008-01-13','2008-01-14','2008-01-15','2008-01-16','2008-01-17',
|
293
|
-
'2008-01-18','2008-01-19','2008-01-20','2008-01-21','2008-01-22','2008-01-23',...
|
294
|
-
‘2008-12-15','2008-12-16','2008-12-17','2008-12-18','2008-12-19','2008-12-20',
|
295
|
-
'2008-12-21','2008-12-22','2008-12-23','2008-12-24','2008-12-25','2008-12-26',
|
296
|
-
'2008-12-27','2008-12-28','2008-12-29','2008-12-30','2008-12-31'))
|
289
|
+
SELECT "clients".* FROM "clients" WHERE ("clients"."created_at" BETWEEN '2010-09-29' AND '2010-11-30')
|
297
290
|
</sql>
|
298
291
|
|
299
|
-
h5. Time and Date Conditions
|
300
|
-
|
301
|
-
Things can get *really* messy if you pass in Time objects as it will attempt to compare your field to *every second* in that range:
|
302
|
-
|
303
|
-
<ruby>
|
304
|
-
Client.where("created_at IN (?)",
|
305
|
-
(params[:start_date].to_date.to_time)..(params[:end_date].to_date.to_time))
|
306
|
-
</ruby>
|
307
|
-
|
308
|
-
<sql>
|
309
|
-
SELECT * FROM users WHERE (created_at IN
|
310
|
-
('2007-12-01 00:00:00', '2007-12-01 00:00:01' ...
|
311
|
-
'2007-12-01 23:59:59', '2007-12-02 00:00:00'))
|
312
|
-
</sql>
|
313
|
-
|
314
|
-
This could possibly cause your database server to raise an unexpected error, for example MySQL will throw back this error:
|
315
|
-
|
316
|
-
<shell>
|
317
|
-
Got a packet bigger than 'max_allowed_packet' bytes: _query_
|
318
|
-
</shell>
|
319
|
-
|
320
|
-
Where _query_ is the actual query used to get that error.
|
321
|
-
|
322
|
-
In this example it would be better to use greater-than and less-than operators in SQL, like so:
|
323
|
-
|
324
|
-
<ruby>
|
325
|
-
Client.where(
|
326
|
-
"created_at > ? AND created_at < ?", params[:start_date], params[:end_date])
|
327
|
-
</ruby>
|
328
|
-
|
329
|
-
You can also use the greater-than-or-equal-to and less-than-or-equal-to like this:
|
330
|
-
|
331
|
-
<ruby>
|
332
|
-
Client.where(
|
333
|
-
"created_at >= ? AND created_at <= ?", params[:start_date], params[:end_date])
|
334
|
-
</ruby>
|
335
|
-
|
336
|
-
Just like in Ruby. If you want a shorter syntax be sure to check out the "Hash Conditions":#hash-conditions section later on in the guide.
|
337
|
-
|
338
292
|
h4. Hash Conditions
|
339
293
|
|
340
294
|
Active Record also allows you to pass in hash conditions which can increase the readability of your conditions syntax. With hash conditions, you pass in a hash with keys of the fields you want conditionalised and the values of how you want to conditionalise them:
|
@@ -385,7 +339,7 @@ SELECT * FROM clients WHERE (clients.orders_count IN (1,3,5))
|
|
385
339
|
|
386
340
|
h4. Ordering
|
387
341
|
|
388
|
-
To retrieve records from the database in a specific order, you can
|
342
|
+
To retrieve records from the database in a specific order, you can use the +order+ method.
|
389
343
|
|
390
344
|
For example, if you're getting a set of records and want to order them in ascending order by the +created_at+ field in your table:
|
391
345
|
|
@@ -496,7 +450,7 @@ SQL uses the +HAVING+ clause to specify conditions on the +GROUP BY+ fields. You
|
|
496
450
|
For example:
|
497
451
|
|
498
452
|
<ruby>
|
499
|
-
Order.group("date(created_at)".having("created_at > ?", 1.month.ago)
|
453
|
+
Order.group("date(created_at)").having("created_at > ?", 1.month.ago)
|
500
454
|
</ruby>
|
501
455
|
|
502
456
|
The SQL that would be executed would be something like this:
|
@@ -928,8 +882,6 @@ For options, please see the parent section, "Calculations":#calculations.
|
|
928
882
|
|
929
883
|
h3. Changelog
|
930
884
|
|
931
|
-
"Lighthouse ticket":http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/16
|
932
|
-
|
933
885
|
* April 7, 2010: Fixed document to validate XHTML 1.0 Strict. "Jaime Iniesta":http://jaimeiniesta.com
|
934
886
|
* February 3, 2010: Update to Rails 3 by "James Miller":credits.html#bensie
|
935
887
|
* February 7, 2009: Second version by "Pratik":credits.html#lifo
|
@@ -141,7 +141,7 @@ end
|
|
141
141
|
|
142
142
|
h4(#validations_overview-errors). +errors[]+
|
143
143
|
|
144
|
-
To verify whether or not a particular attribute of an object is valid, you can use +errors[:attribute]
|
144
|
+
To verify whether or not a particular attribute of an object is valid, you can use +errors[:attribute]+. It returns an array of all the errors for +:attribute+. If there are no errors on the specified attribute, an empty array is returned.
|
145
145
|
|
146
146
|
This method is only useful _after_ validations have been run, because it only inspects the errors collection and does not trigger validations itself. It's different from the +ActiveRecord::Base#invalid?+ method explained above because it doesn't verify the validity of the object as a whole. It only checks to see whether there are errors found on an individual attribute of the object.
|
147
147
|
|
@@ -414,7 +414,7 @@ class Person < ActiveRecord::Base
|
|
414
414
|
validates_with GoodnessValidator
|
415
415
|
end
|
416
416
|
|
417
|
-
class GoodnessValidator <
|
417
|
+
class GoodnessValidator < ActiveModel::Validator
|
418
418
|
def validate
|
419
419
|
if record.first_name == "Evil"
|
420
420
|
record.errors[:base] << "This person is evil"
|
@@ -824,10 +824,10 @@ Here is a simple example where we change the Rails behaviour to always display t
|
|
824
824
|
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
|
825
825
|
if instance.error_message.kind_of?(Array)
|
826
826
|
%(#{html_tag}<span class="validation-error">
|
827
|
-
#{instance.error_message.join(',')}</span>)
|
827
|
+
#{instance.error_message.join(',')}</span>).html_safe
|
828
828
|
else
|
829
829
|
%(#{html_tag}<span class="validation-error">
|
830
|
-
#{instance.error_message}</span>)
|
830
|
+
#{instance.error_message}</span>).html_safe
|
831
831
|
end
|
832
832
|
end
|
833
833
|
</ruby>
|
@@ -1160,8 +1160,6 @@ config.active_record.observers = :mailer_observer
|
|
1160
1160
|
|
1161
1161
|
h3. Changelog
|
1162
1162
|
|
1163
|
-
"Lighthouse ticket":http://rails.lighthouseapp.com/projects/16213/tickets/26-active-record-validations-and-callbacks
|
1164
|
-
|
1165
1163
|
* July 20, 2010: Fixed typos and rephrased some paragraphs for clarity. "Jaime Iniesta":http://jaimeiniesta.com
|
1166
1164
|
* May 24, 2010: Fixed document to validate XHTML 1.0 Strict. "Jaime Iniesta":http://jaimeiniesta.com
|
1167
1165
|
* May 15, 2010: Validation Errors section updated by "Emili Parreño":http://www.eparreno.com
|
@@ -1216,6 +1216,12 @@ To insert something verbatim use the +raw+ helper rather than calling +html_safe
|
|
1216
1216
|
<%= raw @cms.current_template %> <%# inserts @cms.current_template as is %>
|
1217
1217
|
</erb>
|
1218
1218
|
|
1219
|
+
or, equivalently, use <tt><%==</tt>:
|
1220
|
+
|
1221
|
+
<erb>
|
1222
|
+
<%== @cms.current_template %> <%# inserts @cms.current_template as is %>
|
1223
|
+
</erb>
|
1224
|
+
|
1219
1225
|
The +raw+ helper calls +html_safe+ for you:
|
1220
1226
|
|
1221
1227
|
<ruby>
|
@@ -3359,6 +3365,49 @@ The auxiliary file is written in a standard directory for temporary files, but y
|
|
3359
3365
|
|
3360
3366
|
NOTE: Defined in +active_support/core_ext/file/atomic.rb+.
|
3361
3367
|
|
3368
|
+
h3. Extensions to +Logger+
|
3369
|
+
|
3370
|
+
h4. +around_[level]+
|
3371
|
+
|
3372
|
+
Takes two arguments, a +before_message+ and +after_message+ and calls the current level method on the +Logger+ instance, passing in the +before_message+, then the specified message, then the +after_message+:
|
3373
|
+
|
3374
|
+
<ruby>
|
3375
|
+
logger = Logger.new("log/development.log")
|
3376
|
+
logger.around_info("before", "after") { |logger| logger.info("during") }
|
3377
|
+
</ruby>
|
3378
|
+
|
3379
|
+
h4. +silence+
|
3380
|
+
|
3381
|
+
Silences every log level lesser to the specified one for the duration of the given block. Log level orders are: debug, info, error and fatal.
|
3382
|
+
|
3383
|
+
<ruby>
|
3384
|
+
logger = Logger.new("log/development.log")
|
3385
|
+
logger.silence(Logger::INFO) do
|
3386
|
+
logger.debug("In space, no one can hear you scream.")
|
3387
|
+
logger.info("Scream all you want, small mailman!")
|
3388
|
+
end
|
3389
|
+
</ruby>
|
3390
|
+
|
3391
|
+
h4. +datetime_format=+
|
3392
|
+
|
3393
|
+
Modifies the datetime format output by the formatter class associated with this logger. If the formatter class does not have a +datetime_format+ method then this is ignored.
|
3394
|
+
|
3395
|
+
<ruby>
|
3396
|
+
class Logger::FormatWithTime < Logger::Formatter
|
3397
|
+
cattr_accessor(:datetime_format) { "%Y%m%d%H%m%S" }
|
3398
|
+
|
3399
|
+
def self.call(severity, timestamp, progname, msg)
|
3400
|
+
"#{timestamp.strftime(datetime_format)} -- #{String === msg ? msg : msg.inspect}\n"
|
3401
|
+
end
|
3402
|
+
end
|
3403
|
+
|
3404
|
+
logger = Logger.new("log/development.log")
|
3405
|
+
logger.formatter = Logger::FormatWithTime
|
3406
|
+
logger.info("<- is the current time")
|
3407
|
+
</ruby>
|
3408
|
+
|
3409
|
+
NOTE: Defined in +active_support/core_ext/logger.rb+.
|
3410
|
+
|
3362
3411
|
h3. Extensions to +NameError+
|
3363
3412
|
|
3364
3413
|
Active Support adds +missing_name?+ to +NameError+, which tests whether the exception was raised because of the name passed as argument.
|
@@ -3407,7 +3456,5 @@ NOTE: Defined in +active_support/core_ext/load_error.rb+.
|
|
3407
3456
|
|
3408
3457
|
h3. Changelog
|
3409
3458
|
|
3410
|
-
"Lighthouse ticket":https://rails.lighthouseapp.com/projects/16213/tickets/67
|
3411
|
-
|
3412
3459
|
* August 10, 2010: Starts to take shape, added to the index.
|
3413
3460
|
* April 18, 2009: Initial version by "Xavier Noria":credits.html#fxn
|
@@ -6,7 +6,7 @@ endprologue.
|
|
6
6
|
|
7
7
|
h3. RDoc
|
8
8
|
|
9
|
-
The Rails API documentation is generated with RDoc 2.5. Please consult the
|
9
|
+
The Rails API documentation is generated with RDoc 2.5. Please consult the documentation for help with the "markup":http://rdoc.rubyforge.org/RDoc/Markup.html, and take into account also these "additional directives":http://rdoc.rubyforge.org/RDoc/Parser/Ruby.html.
|
10
10
|
|
11
11
|
h3. Wording
|
12
12
|
|
@@ -1851,8 +1851,6 @@ Extensions can refer to the internals of the association proxy using these three
|
|
1851
1851
|
|
1852
1852
|
h3. Changelog
|
1853
1853
|
|
1854
|
-
"Lighthouse ticket":http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/11
|
1855
|
-
|
1856
1854
|
* April 7, 2010: Fixed document to validate XHTML 1.0 Strict. "Jaime Iniesta":http://jaimeiniesta.com
|
1857
1855
|
* April 19, 2009: Added +:touch+ option to +belongs_to+ associations by "Mike Gunderloy":credits.html#mgunderloy
|
1858
1856
|
* February 1, 2009: Added +:autosave+ option "Mike Gunderloy":credits.html#mgunderloy
|
@@ -197,6 +197,8 @@ Examples:
|
|
197
197
|
Creates a Post model with a string title, text body, and published flag.
|
198
198
|
</shell>
|
199
199
|
|
200
|
+
NOTE: For a list of available field types, refer to the "API documentation":http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/TableDefinition.html#method-i-column for the column method for the +TableDefinition+ class.
|
201
|
+
|
200
202
|
But instead of generating a model directly (which we'll be doing later), let's set up a scaffold. A *scaffold* in Rails is a full set of model, database migration for that model, controller to manipulate it, views to view and manipulate the data, and a test suite for each of the above.
|
201
203
|
|
202
204
|
We will set up a simple resource called "HighScore" that will keep track of our highest score on video games we play.
|
@@ -590,6 +592,3 @@ h5. Miscellaneous Tasks
|
|
590
592
|
|
591
593
|
+rake routes+ will list all of your defined routes, which is useful for tracking down routing problems in your app, or giving you a good overview of the URLs in an app you're trying to get familiar with.
|
592
594
|
|
593
|
-
h3. Changelog
|
594
|
-
|
595
|
-
"Lighthouse ticket":http://rails.lighthouseapp.com/projects/16213/tickets/29
|
@@ -13,7 +13,7 @@ Rails offers (at least) four good spots to place initialization code:
|
|
13
13
|
|
14
14
|
* application.rb
|
15
15
|
* Environment-specific Configuration Files
|
16
|
-
* Initializers
|
16
|
+
* Initializers
|
17
17
|
* After-Initializers
|
18
18
|
|
19
19
|
h3. Running Code Before Rails
|
@@ -23,41 +23,63 @@ To run some code before Rails itself is loaded, simply put it above the call to
|
|
23
23
|
|
24
24
|
h3. Configuring Rails Components
|
25
25
|
|
26
|
-
In general, the work of configuring Rails means configuring the components of Rails, as well as configuring Rails itself. The +application.rb+ and environment-specific configuration files (such as +config/environments/production.rb+) allow you to specify the various settings that you want to pass down to all of the components. For example, the default Rails
|
26
|
+
In general, the work of configuring Rails means configuring the components of Rails, as well as configuring Rails itself. The +application.rb+ and environment-specific configuration files (such as +config/environments/production.rb+) allow you to specify the various settings that you want to pass down to all of the components. For example, the default Rails 3.0 +application.rb+ file includes this setting:
|
27
27
|
|
28
28
|
<ruby>
|
29
|
-
config.filter_parameters
|
29
|
+
config.filter_parameters += [:password]
|
30
30
|
</ruby>
|
31
31
|
|
32
32
|
This is a setting for Rails itself. If you want to pass settings to individual Rails components, you can do so via the same +config+ object:
|
33
33
|
|
34
34
|
<ruby>
|
35
|
-
config.active_record.timestamped_migrations = false
|
35
|
+
config.active_record.timestamped_migrations = false
|
36
36
|
</ruby>
|
37
37
|
|
38
38
|
Rails will use that particular setting to configure Active Record.
|
39
39
|
|
40
40
|
h4. Rails General Configuration
|
41
41
|
|
42
|
-
* +config.
|
42
|
+
* +config.after_initialize+ takes a block which will be ran _after_ Rails has finished initializing. Useful for configuring values set up by other initializers:
|
43
43
|
|
44
|
-
|
44
|
+
<ruby>
|
45
|
+
config.after_initialize do
|
46
|
+
ActionView::Base.sanitized_allowed_tags.delete 'div'
|
47
|
+
end
|
48
|
+
</ruby>
|
49
|
+
|
50
|
+
* +config.allow_concurrency+ should be set to +true+ to allow concurrent (threadsafe) action processing. Set to +false+ 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 +threadsafe!+.
|
51
|
+
|
52
|
+
* +config.asset_host+ sets the host for the assets. Useful when CDNs are used for hosting assets rather than the application server itself. Shorter version of +config.action_controller.asset_host+.
|
53
|
+
|
54
|
+
* +config.asset_path+ takes a block which configures where assets can be found. Shorter version of +config.action_controller.asset_path+.
|
55
|
+
|
56
|
+
<ruby>
|
57
|
+
config.asset_path = proc { |asset_path| "assets/#{asset_path}" }
|
58
|
+
</ruby>
|
59
|
+
|
60
|
+
* +config.autoload_once_paths+ accepts an array of paths from which Rails will automatically load from only once. All elements of this array must also be in +autoload_paths+.
|
61
|
+
|
62
|
+
* +config.autoload_paths+ 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.
|
63
|
+
|
64
|
+
* +config.cache_classes+ controls whether or not application classes should be reloaded on each request. Defaults to _true_ in development, _false_ in test and production. Can also be enabled with +threadsafe!+.
|
45
65
|
|
46
|
-
* +config.cache_store+ configures which cache store to use for Rails caching. Options include +:memory_store+, +:file_store+, +:mem_cache_store+ or the name of your own custom class.
|
66
|
+
* +config.cache_store+ configures which cache store to use for Rails caching. Options include +:memory_store+, +:file_store+, +:mem_cache_store+ or the name of your own custom class. Defaults to +:file_store+.
|
47
67
|
|
48
|
-
* +config.colorize_logging+
|
68
|
+
* +config.colorize_logging+ specifies whether or not to use ANSI color codes when logging information. Defaults to _true_.
|
49
69
|
|
50
|
-
* +config.
|
70
|
+
* +config.consider_all_requests_local+ is generally set to +true+ during development and +false+ during production; if it is set to +true+, then any error will cause detailed debugging information to be dumped in the HTTP response. For finer-grained control, set this to +false+ and implement +local_request?+ in controllers to specify which requests should provide debugging information on errors.
|
51
71
|
|
52
|
-
* +config.
|
72
|
+
* +config.controller_paths+ configures where Rails can find controllers for this application.
|
53
73
|
|
54
|
-
* +config.dependency_loading+ enables or disables dependency loading during the request cycle. Setting dependency_loading to _true_ will allow new classes to be loaded during a request and setting it to _false_ will disable this behavior.
|
74
|
+
* +config.dependency_loading+ enables or disables dependency loading during the request cycle. Setting dependency_loading to _true_ will allow new classes to be loaded during a request and setting it to _false_ will disable this behavior. Can also be enabled with +threadsafe!+.
|
55
75
|
|
56
|
-
* +config.eager_load_paths+ accepts an array of paths from which Rails will eager load on boot if cache classes is enabled. All elements of this array must also be in +load_paths+.
|
76
|
+
* +config.eager_load_paths+ accepts an array of paths from which Rails will eager load on boot if cache classes is enabled. Defaults to every folder in the +app+ directory of the application. All elements of this array must also be in +load_paths+.
|
57
77
|
|
58
|
-
* +config.
|
78
|
+
* +config.encoding+ sets up the application-wide encoding. Defaults to UTF-8.
|
59
79
|
|
60
|
-
* +config.
|
80
|
+
* +config.filter_parameters+ used for filtering out the parameters that you don't want shown in the logs, such as passwords or credit card numbers.
|
81
|
+
|
82
|
+
* +config.helper_paths+ configures where Rails can find helpers for this application.
|
61
83
|
|
62
84
|
* +config.log_level+ defines the verbosity of the Rails logger. In production mode, this defaults to +:info+. In development mode, it defaults to +:debug+.
|
63
85
|
|
@@ -65,26 +87,107 @@ h4. Rails General Configuration
|
|
65
87
|
|
66
88
|
* +config.logger+ accepts a logger conforming to the interface of Log4r or the default Ruby 1.8+ Logger class, which is then used to log information from Action Controller. Set to nil to disable logging.
|
67
89
|
|
68
|
-
* +config.
|
69
|
-
|
70
|
-
* +config.plugin_locators+ overrides the class that handle finding the desired plugins that you‘d like to load for your application. By default it is the +Rails::Plugin::FileSystemLocator+.
|
71
|
-
|
72
|
-
* +config.plugin_paths+ overrides the path to the root of the plugins directory. Defaults to +vendor/plugins+.
|
90
|
+
* +config.middleware+ allows you to configure the application's middleware. This is covered in depth in the "Configuring Middleware" section below.
|
73
91
|
|
74
92
|
* +config.plugins+ 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.
|
75
93
|
|
76
|
-
* +config.preload_frameworks+ enables or disables preloading all frameworks at startup.
|
94
|
+
* +config.preload_frameworks+ enables or disables preloading all frameworks at startup. Can also be enabled with +threadsafe!+. Defaults to +nil+, so is disabled.
|
77
95
|
|
78
96
|
* +config.reload_plugins+ enables or disables plugin reloading.
|
79
97
|
|
80
|
-
* +config.
|
98
|
+
* +config.root+ configures the root path of the application.
|
81
99
|
|
82
|
-
* +config.
|
100
|
+
* +config.secret_token+ used for specifying a key which allows sessions for the application to be verified against a known secure key to prevent tampering.
|
101
|
+
|
102
|
+
* +config.serve_static_assets+ configures Rails to serve static assets. Defaults to _true_, but in the production environment is turned off. The server software used to run the application should be used to serve the assets instead.
|
103
|
+
|
104
|
+
* +config.session_store+ is usually set up in +config/initializers/session_store.rb+ and specifies what class to use to store the session. Custom session stores can be specified like so:
|
105
|
+
|
106
|
+
<ruby>
|
107
|
+
config.session_store = :my_custom_store
|
108
|
+
</ruby>
|
109
|
+
|
110
|
+
This custom store must be defined as +ActionDispatch::Session::MyCustomStore+.
|
83
111
|
|
84
|
-
* +config.
|
112
|
+
* +config.threadsafe!+ enables +allow_concurrency+, +cache_classes+, +dependency_loading+ and +preload_frameworks+ to make the application threadsafe.
|
113
|
+
|
114
|
+
WARNING: 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 +config.threadsafe!+.
|
115
|
+
|
116
|
+
* +config.time_zone+ sets the default time zone for the application and enables time zone awareness for Active Record.
|
85
117
|
|
86
118
|
* +config.whiny_nils+ enables or disabled warnings when an methods of nil are invoked. Defaults to _false_.
|
87
119
|
|
120
|
+
h4. Configuring Generators
|
121
|
+
|
122
|
+
Rails 3 allows you to alter what generators are used with the +config.generators+ method. This method takes a block:
|
123
|
+
|
124
|
+
<ruby>
|
125
|
+
config.generators do |g|
|
126
|
+
g.orm :active_record
|
127
|
+
g.test_framework :test_unit
|
128
|
+
end
|
129
|
+
</ruby>
|
130
|
+
|
131
|
+
The full set of methods that can be used in this block are as follows:
|
132
|
+
|
133
|
+
* +force_plural+ allows pluralized model names. Defaults to _false_.
|
134
|
+
* +helper+ defines whether or not to generate helpers. Defaults to _true_
|
135
|
+
* +orm+ defines which orm to use. Defaults to _nil_, so will use Active Record by default.
|
136
|
+
* +integration_tool+ defines which integration tool to use. Defaults to _nil_
|
137
|
+
* +performance_tool+ defines which performance tool to use. Defaults to _nil_
|
138
|
+
* +resource_controller+ defines which generator to use for generating a controller when using +rails generate resource+. Defaults to +:controller+.
|
139
|
+
* +scaffold_controller+ different from +resource_controller+, defines which generator to use for generating a _scaffolded_ controller when using +rails generate scaffold+. Defaults to +:scaffold_controller+
|
140
|
+
* +stylesheets+ turns on the hook for stylesheets in generators. Used in Rails for when the +scaffold+ generator is ran, but this hook can be used in other generates as well.
|
141
|
+
* +test_framework+ defines which test framework to use. Defaults to _nil_, so will use Test::Unit by default.
|
142
|
+
* +template_engine+ defines which template engine to use, such as ERB or Haml. Defaults to +:erb+.
|
143
|
+
|
144
|
+
h4. Configuring Middleware
|
145
|
+
|
146
|
+
Every Rails application comes with a standard set of middleware which it uses in this order in the development environment:
|
147
|
+
|
148
|
+
* +ActionDispatch::Static+ is used to serve static assets. Disabled if +config.serve_static_assets+ is _true_.
|
149
|
+
* +Rack::Lock+ Will wrap the app in mutex so it can only be called by a single thread at a time. Only enabled if +config.action_controller.allow_concurrency+ is set to _false_, which it is by default.
|
150
|
+
* +ActiveSupport::Cache::Strategy::LocalCache+ 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.
|
151
|
+
* +Rack::Runtime+ Sets an +X-Runtime+ header, containing the time (in seconds) taken to execute the request.
|
152
|
+
* +Rails::Rack::Logger+ Will notify the logs that the request has began. After request is complete, flushes all the logs.
|
153
|
+
* +ActionDispatch::ShowExceptions+ rescues any exception returned by the application and renders nice exception pages if the request is local or if +config.consider_all_requests_local+ is set to _true_. If +config.action_dispatch.show_exceptions+ is set to _false_, exceptions will be raised regardless.
|
154
|
+
* +ActionDispatch::RemoteIp+ checks for IP spoofing attacks. Configurable with the +config.action_dispatch.ip_spoofing_check+ and +config.action_dispatch.trusted_proxies+ settings.
|
155
|
+
* +Rack::Sendfile+ 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 +config.action_dispatch_
|
156
|
+
* +ActionDispatch::Callbacks+ Runs the prepare callbacks before serving the request.
|
157
|
+
* +ActiveRecord::ConnectionAdapters::ConnectionManagement+ cleans active connections after each request, unless the +rack.test+ key in the request environment is set to _true_.
|
158
|
+
* +ActiveRecord::QueryCache+ caches all +SELECT+ queries generated in a request. If an +INSERT+ or +UPDATE+ takes place then the cache is cleaned.
|
159
|
+
* +ActionDispatch::Cookies+ sets cookies for the request.
|
160
|
+
* +ActionDispatch::Session::CookieStore+ is responsible for storing the session in cookies. An alternate middleware can be used for this by changing the +config.action_controller.session_store+ to an alternate value. Additionally, options passed to this can be configured by using +config.action_controller.session_options+.
|
161
|
+
* +ActionDispatch::Flash+ sets up the +flash+ keys. Only available if +config.action_controller.session_store+ is set to a value.
|
162
|
+
* +ActionDispatch::ParamsParser+ parses out parameters from the request into +params+
|
163
|
+
* +Rack::MethodOverride+ allows the method to be overridden if +params[:_method]+ is set. This is the middleware which supports the PUT and DELETE HTTP method types.
|
164
|
+
* +ActionDispatch::Head+ converts HEAD requests to GET requests and serves them as so.
|
165
|
+
* +ActionDispatch::BestStandardsSupport+ enables "best standards support" so that IE8 renders some elements correctly.
|
166
|
+
|
167
|
+
Besides these usual middleware, you can add your own by using the +config.middleware.use+ method:
|
168
|
+
|
169
|
+
<ruby>
|
170
|
+
config.middleware.use Magical::Unicorns
|
171
|
+
</ruby>
|
172
|
+
|
173
|
+
This will put the +Magical::Unicorns+ middleware on the end of the stack. If you wish to put this middleware before another use +insert_before+:
|
174
|
+
|
175
|
+
<ruby>
|
176
|
+
config.middleware.insert_before ActionDispatch::Head, Magical::Unicorns
|
177
|
+
</ruby>
|
178
|
+
|
179
|
+
There's also +insert_after+ which will insert a middleware _after_ another:
|
180
|
+
|
181
|
+
<ruby>
|
182
|
+
config.middleware.insert_after ActionDispatch::Head, Magical::Unicorns
|
183
|
+
</ruby>
|
184
|
+
|
185
|
+
Middlewares can also be completely swapped out and replaced with others:
|
186
|
+
|
187
|
+
<ruby>
|
188
|
+
config.middleware.swap ActionDispatch::BestStandardsSupport, Magical::Unicorns
|
189
|
+
</ruby>
|
190
|
+
|
88
191
|
h4. Configuring i18n
|
89
192
|
|
90
193
|
* +config.i18n.default_locale+ sets the default locale of an application used for i18n. Defaults to +:en+.
|
@@ -127,17 +230,15 @@ h4. Configuring Action Controller
|
|
127
230
|
|
128
231
|
<tt>config.action_controller</tt> includes a number of configuration settings:
|
129
232
|
|
130
|
-
* +config.action_controller.asset_host+
|
131
|
-
|
132
|
-
* +config.action_controller.asset_path+ allows you to override the default asset path generation by providing your own instructions.
|
233
|
+
* +config.action_controller.asset_host+ sets the host for the assets. Useful when CDNs are used for hosting assets rather than the application server itself.
|
133
234
|
|
134
|
-
* +config.action_controller.
|
235
|
+
* +config.action_controller.asset_path+ takes a block which configures where assets can be found. Shorter version of +config.action_controller.asset_path+.
|
135
236
|
|
136
|
-
* +config.action_controller.
|
237
|
+
* +config.action_controller.page_cache_directory+ 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 Rails.public_path (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.
|
137
238
|
|
138
|
-
|
239
|
+
* +config.action_controller.page_cache_extension+ configures the extension used for cached pages saved to +page_cache_directory+. Defaults to +.html+
|
139
240
|
|
140
|
-
* +config.action_controller.
|
241
|
+
* +config.action_controller.perform_caching+ configures whether the application should perform caching or not. Set to _false_ in development mode, _true_ in production.
|
141
242
|
|
142
243
|
* +config.action_controller.default_charset+ specifies the default character set for all renders. The default is "utf-8".
|
143
244
|
|
@@ -157,8 +258,6 @@ WARNING: Threadsafe operation is incompatible with the normal workings of develo
|
|
157
258
|
|
158
259
|
* +config.action_controller.relative_url_root+ can be used to tell Rails that you are deploying to a subdirectory. The default is +ENV['RAILS_RELATIVE_URL_ROOT']+.
|
159
260
|
|
160
|
-
* +config.action_dispatch.session_store+ sets the name of the store for session data. The default is +:cookie_store+; other valid options include +:active_record_store+, +:mem_cache_store+ or the name of your own custom class.
|
161
|
-
|
162
261
|
The caching code adds two additional settings:
|
163
262
|
|
164
263
|
* +ActionController::Base.page_cache_directory+ sets the directory where Rails will create cached pages for your web server. The default is +Rails.public_path+ (which is usually set to +Rails.root + "/public"+).
|
@@ -173,14 +272,24 @@ The Active Record session store can also be configured:
|
|
173
272
|
|
174
273
|
* +ActiveRecord::SessionStore::Session.data_column_name+ sets the name of the column which stores marshaled session data. Defaults to +data+.
|
175
274
|
|
275
|
+
h4. Configuring Action Dispatch
|
276
|
+
|
277
|
+
* +config.action_dispatch.session_store+ sets the name of the store for session data. The default is +:cookie_store+; other valid options include +:active_record_store+, +:mem_cache_store+ or the name of your own custom class.
|
278
|
+
|
279
|
+
* +config.action_dispatch.tld_length+ sets the TLD (top-level domain) length for the application. Defaults to +1+.
|
280
|
+
|
281
|
+
* +ActionDispatch::Callbacks.before+ takes a block of code to run before the request.
|
282
|
+
|
283
|
+
* +ActionDispatch::Callbacks.to_prepare+ takes a block to run after +ActionDispatch::Callbacks.before+, but before the request. Runs for every request in +development+ mode, but only once for +production+ or environments with +cache_classes+ set to +true+.
|
284
|
+
|
285
|
+
* +ActionDispatch::Callbacks.after+ takes a block of code to run after the request.
|
286
|
+
|
176
287
|
h4. Configuring Action View
|
177
288
|
|
178
289
|
There are only a few configuration options for Action View, starting with four on +ActionView::Base+:
|
179
290
|
|
180
291
|
* +config.action_view.debug_rjs+ specifies whether RJS responses should be wrapped in a try/catch block that alert()s the caught exception (and then re-raises it). The default is +false+.
|
181
292
|
|
182
|
-
* +config.action_view.warn_cache_misses+ tells Rails to display a warning whenever an action results in a cache miss on your view paths. The default is +false+.
|
183
|
-
|
184
293
|
* +config.action_view.field_error_proc+ provides an HTML generator for displaying errors that come from Active Record. The default is <tt>Proc.new{ |html_tag, instance| %Q(%<div class="field_with_errors">#{html_tag}</div>).html_safe }</tt>
|
185
294
|
|
186
295
|
* +config.action_view.default_form_builder+ tells Rails which form builder to use by default. The default is +ActionView::Helpers::FormBuilder+.
|
@@ -189,12 +298,36 @@ There are only a few configuration options for Action View, starting with four o
|
|
189
298
|
|
190
299
|
* +config.action_view.erb_trim_mode+ gives the trim mode to be used by ERB. It defaults to +'-'+. See the "ERB documentation":http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/ for more information.
|
191
300
|
|
301
|
+
* +config.action_view.javascript_expansions+ a hash containining expansions that can be used for javascript include tag. By default, this is defined as:
|
302
|
+
|
303
|
+
<ruby>
|
304
|
+
config.action_view.javascript_expansions = { :defaults => ['prototype', 'effects', 'dragdrop', 'controls', 'rails'] }
|
305
|
+
</ruby>
|
306
|
+
|
307
|
+
However, you may add to this by defining others:
|
308
|
+
|
309
|
+
<ruby>
|
310
|
+
config.action_view.javascript_expansions[:jquery] = ["jquery", "jquery-ui"]
|
311
|
+
</ruby>
|
312
|
+
|
313
|
+
Then this can be referenced in the view with the following code:
|
314
|
+
|
315
|
+
<ruby>
|
316
|
+
<%= javascript_include_tag :jquery %>
|
317
|
+
</ruby>
|
318
|
+
|
319
|
+
* +config.action_view.stylesheet_expansions+ works in much the same way as +javascript_expansions+, but has no default key. Keys defined for this hash can be referenced in the view like such:
|
320
|
+
|
321
|
+
<ruby>
|
322
|
+
<%= stylesheet_link_tag :special %>
|
323
|
+
</ruby>
|
324
|
+
|
325
|
+
* +ActionView::Helpers::AssetTagHelper::AssetPaths.cache_asset_ids+ 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.
|
326
|
+
|
192
327
|
h4. Configuring Action Mailer
|
193
328
|
|
194
329
|
There are a number of settings available on +config.action_mailer+:
|
195
330
|
|
196
|
-
* +config.action_mailer.template_root+ gives the root folder for Action Mailer templates.
|
197
|
-
|
198
331
|
* +config.action_mailer.logger+ accepts a logger conforming to the interface of Log4r or the default Ruby 1.8+ Logger class, which is then used to log information from Action Mailer. Set to nil to disable logging.
|
199
332
|
|
200
333
|
* +config.action_mailer.smtp_settings+ allows detailed configuration for the +:smtp+ delivery method. It accepts a hash of options, which can include any of these options:
|
@@ -215,15 +348,13 @@ There are a number of settings available on +config.action_mailer+:
|
|
215
348
|
|
216
349
|
* +config.action_mailer.perform_deliveries+ specifies whether mail will actually be delivered. By default this is +true+; it can be convenient to set it to +false+ for testing.
|
217
350
|
|
218
|
-
* +config.action_mailer.
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
which specify the content type in their filenames) this variable controls how the parts are ordered. Defaults to +["text/html", "text/enriched", "text/plain"]+. Items that appear first in the array have higher priority in the mail client
|
226
|
-
and appear last in the mime encoded message.
|
351
|
+
* +config.action_mailer.default+ configures Action Mailer defaults. These default to:
|
352
|
+
<ruby>
|
353
|
+
:mime_version => "1.0",
|
354
|
+
:charset => "UTF-8",
|
355
|
+
:content_type => "text/plain",
|
356
|
+
:parts_order => [ "text/plain", "text/enriched", "text/html" ]
|
357
|
+
</ruby>
|
227
358
|
|
228
359
|
h4. Configuring Active Resource
|
229
360
|
|
@@ -235,6 +366,8 @@ h4. Configuring Active Support
|
|
235
366
|
|
236
367
|
There are a few configuration options available in Active Support:
|
237
368
|
|
369
|
+
* +config.active_support.bare+ enables or disables the loading of +active_support/all+ when booting Rails. Defaults to +nil+, which means +active_support/all+ is loaded.
|
370
|
+
|
238
371
|
* +config.active_support.escape_html_entities_in_json+ enables or disables the escaping of HTML entities in JSON serialization. Defaults to +true+.
|
239
372
|
|
240
373
|
* +config.active_support.use_standard_json_time_format+ enables or disables serializing dates to ISO 8601 format. Defaults to +false+.
|
@@ -245,44 +378,179 @@ There are a few configuration options available in Active Support:
|
|
245
378
|
|
246
379
|
* +ActiveSupport::Logger.silencer+ is set to +false+ to disable the ability to silence logging in a block. The default is +true+.
|
247
380
|
|
248
|
-
h3.
|
381
|
+
h3. Rails Environment Settings
|
382
|
+
|
383
|
+
Some parts of Rails can also be configured externally by supplying environment variables. The following environment variables are recognized by various parts of Rails:
|
249
384
|
|
250
|
-
|
385
|
+
* +ENV['RAILS_ENV']+ defines the Rails environment (production, development, test, and so on) that Rails will run under.
|
251
386
|
|
252
|
-
|
387
|
+
* +ENV['RAILS_RELATIVE_URL_ROOT']+ is used by the routing code to recognize URLs when you deploy your application to a subdirectory.
|
253
388
|
|
254
|
-
|
389
|
+
* +ENV["RAILS_ASSET_ID"]+ will override the default cache-busting timestamps that Rails generates for downloadable assets.
|
390
|
+
|
391
|
+
* +ENV["RAILS_CACHE_ID"]+ and +ENV["RAILS_APP_VERSION"]+ are used to generate expanded cache keys in Rails' caching code. This allows you to have multiple separate caches from the same application.
|
392
|
+
|
393
|
+
h3. Initialization events
|
394
|
+
|
395
|
+
Rails has 5 initialization events which can be hooked into (listed in order that they are ran):
|
255
396
|
|
256
|
-
|
397
|
+
* +before_configuration+: This is run as soon as the application constant inherits from +Rails::Application+. The +config+ calls are evaluated before this happens.
|
257
398
|
|
258
|
-
|
399
|
+
* +before_initialize+: This is run directly before the initialization process of the application occurs with the +:bootstrap_hook+ initializer near the beginning of the Rails initialization process.
|
400
|
+
|
401
|
+
* +to_prepare+: Run after the initializers are ran for all Railties (including the application itself), but before eager loading and the middleware stack is built.
|
402
|
+
|
403
|
+
* +before_eager_load+: This is run directly before eager loading occurs, which is the default behaviour for the _production_ environment and not for the +development+ enviroment.
|
404
|
+
|
405
|
+
* +after_initialize+: Run directly after the initialization of the application, but before the application initializers are run.
|
406
|
+
|
407
|
+
|
408
|
+
WARNING: Some parts of your application, notably observers and routing, are not yet set up at the point where the +after_initialize+ block is called.
|
409
|
+
|
410
|
+
h4. +Rails::Railtie#initializer+
|
411
|
+
|
412
|
+
Rails has several initializers that run on startup that are all defined by using the +initializer+ method from +Rails::Railtie+. Here's an example of the +initialize_whiny_nils+ initializer from Active Support:
|
259
413
|
|
260
414
|
<ruby>
|
261
|
-
|
262
|
-
|
263
|
-
end
|
415
|
+
initializer "active_support.initialize_whiny_nils" do |app|
|
416
|
+
require 'active_support/whiny_nil' if app.config.whiny_nils
|
417
|
+
end
|
264
418
|
</ruby>
|
265
419
|
|
266
|
-
|
420
|
+
The +initializer+ 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 +:before+ key in the options hash can be specified to specify which initializer this new initializer must run before, and the +:after+ key will specify which initializer to run this initializer _after_.
|
267
421
|
|
268
|
-
|
422
|
+
Initializers defined using the +initializer+ method will be ran in the order they are defined in, with the exception of ones that use the +:before+ or +:after+ methods.
|
269
423
|
|
270
|
-
|
424
|
+
WARNING: 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 _before_ "four" but _after_ "three", that just isn't logical and Rails will not be able to determine your initializer order.
|
271
425
|
|
272
|
-
|
426
|
+
The block's argument of the +initialize+ is the instance of the application itself, and so we can access the configuration on it by using the +config+ method as this initializer does.
|
273
427
|
|
274
|
-
|
428
|
+
Because +Rails::Application+ inherits from +Rails::Railtie+ (indirectly), you can use the +initializer+ method in +config/application.rb+ to define initializers for the application.
|
275
429
|
|
276
|
-
|
430
|
+
h4. Initializers
|
277
431
|
|
278
|
-
|
432
|
+
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).
|
279
433
|
|
280
|
-
|
434
|
+
*+load_environment_hook+*
|
435
|
+
Serves as a placeholder so that +:load_environment_config+ can be defined to run before it.
|
281
436
|
|
282
|
-
|
437
|
+
*+load_active_support+* Requires +active_support/dependencies+ which sets up the basis for Active Support. Optionally requires +active_support/all+ if +config.active_support.bare+ is un-truthful, which is the default.
|
438
|
+
|
439
|
+
*+preload_frameworks+* Will load all autoload dependencies of Rails automatically if +config.preload_frameworks+ is +true+ or "truthful". By default this configuration option is disabled. In Rails, when internal classes are referenced for the first time they are autoloaded. +:preload_frameworks+ loads all of this at once on initialization.
|
440
|
+
|
441
|
+
*+initialize_logger+* Initializes the logger (an +ActiveSupport::BufferedLogger+ object) for the application and makes it accessible at +Rails.logger+, providing that there's no initializer inserted before this point that has defined +Rails.logger+.
|
442
|
+
|
443
|
+
*+initialize_cache+* If +RAILS_CACHE+ isn't yet set, initializes the cache by referencing the value in +config.cache_store+ and stores the outcome as +RAILS_CACHE+. If this object responds to the +middleware+ method, its middleware is inserted before +Rack::Runtime+ in the middleware stack.
|
444
|
+
|
445
|
+
*+set_clear_dependencies_hook+* Provides a hook for +active_record.set_dispatch_hooks+ to use, which will run before this initializer. This initializer -- which runs only if +cache_classes+ is set to +false+ -- uses +ActionDispatch::Callbacks.after+ 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.
|
446
|
+
|
447
|
+
*+initialize_dependency_mechanism+* If +config.cache_classes+ is set to +true+, configures +ActiveSupport::Dependencies.mechanism+ to +require+ dependencies rather than +load+ them.
|
448
|
+
|
449
|
+
*+bootstrap_hook+* Runs all configured +before_initialize+ blocks.
|
450
|
+
|
451
|
+
*+i18n.callbacks+* In the development environment, sets up a +to_prepare+ callback which will call +I18n.reload!+ if any of the locales have changed since the last request. In production mode this callback will only run on the first request.
|
452
|
+
|
453
|
+
*+active_support.initialize_whiny_nils+* Will require +active_support/whiny_nil+ if +config.whiny_nil+ is set to +true+. This file will output errors such as:
|
454
|
+
|
455
|
+
<plain>
|
456
|
+
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
|
457
|
+
</plain>
|
458
|
+
|
459
|
+
And:
|
460
|
+
|
461
|
+
<plain>
|
462
|
+
You have a nil object when you didn't expect it!
|
463
|
+
You might have expected an instance of Array.
|
464
|
+
The error occurred while evaluating nil.each
|
465
|
+
</plain>
|
466
|
+
|
467
|
+
*+active_support.deprecation_behavior+* Sets up deprecation reporting for environments, defaulting to +log+ for development, +notify+ for production and +stderr+ for test. If a value isn't set for +config.active_support.deprecation+ then this initializer will prompt the user to configure this line in the current environment's +config/environments+ file.
|
468
|
+
|
469
|
+
*+active_support.initialize_time_zone+* Sets the default time zone for the application based off the +config.time_zone+ setting, which defaults to "UTC".
|
470
|
+
|
471
|
+
*+action_dispatch.configure+* Configures the +ActionDispatch::Http::URL.tld_length+ to be set to the value of +config.action_dispatch.tld_length+.
|
472
|
+
|
473
|
+
*+action_view.cache_asset_ids+* Will set +ActionView::Helpers::AssetTagHelper::AssetPaths.cache_asset_ids+ to +false+ when Active Support loads, but only if +config.cache_classes+ is too.
|
283
474
|
|
284
|
-
|
475
|
+
*+action_view.javascript_expansions+* Registers the expansions set up by +config.action_view.javascript_expansions+ and +config.action_view.stylesheet_expansions+ to be recognised by Action View and therefore usable in the views.
|
476
|
+
|
477
|
+
*+action_view.set_configs+* Sets up Action View by using the settings in +config.action_view+ by +send+'ing the method names as setters to +ActionView::Base+ and passing the values through.
|
478
|
+
|
479
|
+
*+action_controller.logger+* Sets +ActionController::Base.logger+ -- if it's not already set -- to +Rails.logger+.
|
480
|
+
|
481
|
+
*+action_controller.initialize_framework_caches+* Sets +ActionController::Base.cache_store+ -- if it's not already set -- to +RAILS_CACHE+.
|
482
|
+
|
483
|
+
*+action_controller.set_configs+* Sets up Action Controller by using the settings in +config.action_controller+ by +send+'ing the method names as setters to +ActionController::Base+ and passing the values through.
|
484
|
+
|
485
|
+
*+action_controller.compile_config_methods+* Initializes methods for the config settings specified so that they are quicker to access.
|
486
|
+
|
487
|
+
*+active_record.initialize_timezone+* Sets +ActiveRecord::Base.time_zone_aware_attributes+ to true, as well as setting +ActiveRecord::Base.default_timezone+ to UTC. When attributes are read from the database, they will be converted into the time zone specified by +Time.zone+.
|
488
|
+
|
489
|
+
*+active_record.logger+* Sets +ActiveRecord::Base.logger+ -- if it's not already set -- to +Rails.logger+.
|
490
|
+
|
491
|
+
*+active_record.set_configs+* Sets up Active Record by using the settings in +config.active_record+ by +send+'ing the method names as setters to +ActiveRecord::Base+ and passing the values through.
|
492
|
+
|
493
|
+
*+active_record.initialize_database+* Loads the database configuration (by default) from +config/database.yml+ and establishes a connection for the current environment.
|
494
|
+
|
495
|
+
*+active_record.log_runtime+* Includes +ActiveRecord::Railties::ControllerRuntime+ which is responsible for reporting the length of time Active Record calls took for the request back to the logger.
|
496
|
+
|
497
|
+
*+active_record.set_dispatch_hooks+* If +config.cache_classes+ is set to false, all reloadable connections to the database will be reset.
|
498
|
+
|
499
|
+
*+action_mailer.logger+* Sets +ActionMailer::Base.logger+ -- if it's not already set -- to +Rails.logger+.
|
500
|
+
|
501
|
+
*+action_mailer.set_configs+* Sets up Action Mailer by using the settings in +config.action_mailer+ by +send+'ing the method names as setters to +ActionMailer::Base+ and passing the values through.
|
502
|
+
|
503
|
+
*+action_mailer.compile_config_methods+* Initializes methods for the config settings specified so that they are quicker to access.
|
504
|
+
|
505
|
+
*+active_resource.set_configs+* Sets up Active Resource by using the settings in +config.active_resource+ by +send+'ing the method names as setters to +ActiveResource::Base+ and passing the values through.
|
506
|
+
|
507
|
+
*+set_load_path+* This initializer runs before +bootstrap_hook+. Adds the +vendor+, +lib+, all directories of +app+ and any paths specified by +config.load_paths+ to +$LOAD_PATH+.
|
508
|
+
|
509
|
+
*+set_autoload_path+* This initializer runs before +bootstrap_hook+. Adds all sub-directories of +app+ and paths specified by +config.autoload_paths+ to +ActiveSupport::Dependencies.autoload_paths+.
|
510
|
+
|
511
|
+
*+add_routing_paths+* Loads (by default) all +config/routes.rb+ files (in the application and railties, including engines) and sets up the routes for the application.
|
512
|
+
|
513
|
+
*+add_locales+* Adds the files in +config/locales+ (from the application, railties and engines) to +I18n.load_path+, making available the translations in these files.
|
514
|
+
|
515
|
+
*+add_view_paths+* Adds the directory +app/views+ from the application, railties and engines to the lookup path for view files for the application.
|
516
|
+
|
517
|
+
*+load_environment_config+* Loads the +config/environments+ file for the current environment.
|
518
|
+
|
519
|
+
*+append_asset_paths+* Finds asset paths for the application and all attached railties and keeps a track of the available directories in +config.static_asset_paths+.
|
520
|
+
|
521
|
+
*+prepend_helpers_path+* Adds the directory +app/helpers+ from the application, railties and engines to the lookup path for helpers for the application.
|
522
|
+
|
523
|
+
*+load_config_initializers+* Loads all Ruby files from +config/initializers+ 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.
|
524
|
+
|
525
|
+
NOTE: You can use subfolders to organize your initializers if you like, because Rails will look into the whole file hierarchy from the +initializers+ folder on down.
|
526
|
+
|
527
|
+
TIP: If you have any ordering dependency in your initializers, you can control the load order by naming. For example, +01_critical.rb+ will be loaded before +02_normal.rb+.
|
528
|
+
|
529
|
+
*+engines_blank_point+* 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.
|
530
|
+
|
531
|
+
*+add_generator_templates+* Finds templates for generators at +lib/templates+ for the application, railities and engines and adds these to the +config.generators.templates+ setting, which will make the templates available for all generators to reference.
|
532
|
+
|
533
|
+
*+ensure_autoload_once_paths_as_subset+* Ensures that the +config.autoload_once_paths+ only contains paths from +config.autoload_paths+. If it contains extra paths, then an exception will be raised.
|
534
|
+
|
535
|
+
*+add_to_prepare_blocks+* The block for every +config.to_prepare+ call in the application, a railtie or engine is added to the +to_prepare+ callbacks for Action Dispatch which will be ran per request in development, or before the first request in production.
|
536
|
+
|
537
|
+
*+add_builtin_route+* If the application is running under the development environment then this will append the route for +rails/info/properties+ to the application routes. This route provides the detailed information such as Rails and Ruby version for +public/index.html+ in a default Rails application.
|
538
|
+
|
539
|
+
*+build_middleware_stack+* Builds the middleware stack for the application, returning an object which has a +call+ method which takes a Rack environment object for the request.
|
540
|
+
|
541
|
+
*+eager_load!+* If +config.cache_classes+ is +true+, runs the +config.before_eager_load+ hooks and then calls +eager_load!+ which will load all the Ruby files from +config.eager_load_paths+.
|
542
|
+
|
543
|
+
*+finisher_hook+* Provides a hook for after the initialization of process of the application is complete, as well as running all the +config.after_initialize+ blocks for the application, railties and engines.
|
544
|
+
|
545
|
+
*+set_routes_reloader+* Configures Action Dispatch to reload the routes file using +ActionDispatch::Callbacks.to_prepare+.
|
546
|
+
|
547
|
+
*+disable_dependency_loading+*
|
548
|
+
|
549
|
+
|
550
|
+
h3. Changelog
|
285
551
|
|
552
|
+
* December 3, 2010: Added initialization events for Rails 3 ("Ryan Bigg":http://ryanbigg.com)
|
553
|
+
* November 26, 2010: Removed all config settings not available in Rails 3 ("Ryan Bigg":http://ryanbigg.com)
|
286
554
|
* August 13, 2009: Updated with config syntax and added general configuration options by "John Pignata"
|
287
555
|
* January 3, 2009: First reasonably complete draft by "Mike Gunderloy":credits.html#mgunderloy
|
288
556
|
* November 5, 2008: Rough outline by "Mike Gunderloy":credits.html#mgunderloy
|