class UserMailer < ActionMailer::Base end
From a0f5e0b6d9de6594e5a83a4d8eaf7ee9ec1754c9 Mon Sep 17 00:00:00 2001
From: Pratik Naik This guide should provide you with all you need to get started in sending and receiving emails from/to your application, and many internals of the ActionMailer class. It will also cover how to test your mailers. This guide should provide you with all you need to get started in sending and receiving emails from/to your application, and many internals of Action Mailer. It will also cover how to test your mailers. Action Mailer allows you to send email from your application using a mailer model and views.
-Yes, that is correct, in Rails, emails are used by creating models that inherit from ActionMailer::Base. They live alongside other models in /app/models BUT they have views just like controllers that appear alongside other views in app/views. Let’s say you want to send a welcome email to a user after they signup. Here is how you would go about this: Lets add a method called welcome_email, that will send an email to the user’s registered email address: Lets add a method called welcome_email, that will send an email to the user’s registered email address: recipients who the recipients are, put in an array for multiple, ie, @recipients = ["user1@example.com", "user2@example.com"] The recipients of the email. It can be a string or, if there are multiple recipients, an array of strings from How about @body[:user]? Well anything you put in the @body hash will appear in the mailer view (more about mailer views below) as an instance variable ready for you to use, ie, in our example the mailer view will have a @user instance variable available for its consumption. The keys of the hash passed to body become instance variables in the view. Thus, in our example the mailer view will have a @user and a @url instance variables available. The file can look like: There are 3 ways to achieve this. One is to send the email from the controller that sends the email, another is to put it in a before_create block in the user model, and the last one is to use an observer on the user model. Whether you use the second or third methods is up to you, but staying away from the first is recommended. Not because it’s wrong, but because it keeps your controller clean, and keeps all logic related to the user model within the user model. This way, whichever way a user is created (from a web form, or from an API call, for example), we are guaranteed that the email will be sent. There are three ways to achieve this. One is to send the email from the controller that sends the email, another is to put it in a before_create callback in the user model, and the last one is to use an observer on the user model. Whether you use the second or third methods is up to you, but staying away from the first is recommended. Not because it’s wrong, but because it keeps your controller clean, and keeps all logic related to the user model within the user model. This way, whichever way a user is created (from a web form, or from an API call, for example), we are guaranteed that the email will be sent. Let’s see how we would go about wiring it up using an observer: In config/environment.rb: In config/environment.rb: There was a bit of a debate on where to put observers. Some people put them in app/models, but a cleaner method may be to create an app/observers folder to store all observers, and add that to your load path. Open config/environment.rb and make it look like: ALMOST THERE :) Now all we need is that danged observer, and we’re done:
-Create a file called user_observer in app/models or app/observers depending on where you stored it, and make it look like: Now create a file called user_observer in app/models or app/observers depending on where you stored it, and make it look like: Notice how we call deliver_welcome_email? Where is that method? Well if you remember, we created a method called welcome_email in UserMailer, right? Well, as part of the "magic" of rails, we deliver the email identified by welcome_email by calling deliver_welcome_email. The next section will go through this in more detail. Notice how we call deliver_welcome_email? Where is that method? Well if you remember, we created a method called welcome_email in UserMailer, right? Well, as part of the "magic" of Rails, we deliver the email identified by welcome_email by calling deliver_welcome_email. The next section will go through this in more detail. That’s it! Now whenever your users signup, they will be greeted with a nice welcome email. So how does Action Mailer understand this deliver_welcome_email call? If you read the documentation (http://api.rubyonrails.org/files/vendor/rails/actionmailer/README.html), you will find this in the "Sending Emails" section: You never instantiate your mailer class. Rather, your delivery instance
methods are automatically wrapped in class methods that start with the word
-deliver_ followed by the name of the mailer method that you would
-like to deliver. The signup_notification method defined above is
-delivered by invoking Notifier.deliver_signup_notification.
-
Action Mailer Basics
1. Introduction
2. Sending Emails
2.1. Walkthrough to generating a Mailer
+2.1. Walkthrough to generating a mailer
2.1.1. Create the mailer:
class UserMailer < ActionMailer::Base
end
-
+
@@ -172,7 +172,7 @@ cellspacing="0" cellpadding="4">
2.1.3. Create the mailer view
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
- <meta content='text/html; charset=iso-8859-1' http-equiv='Content-Type' />
+ <meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
</head>
<body>
- <h1>Welcome to example.com, <%= @user.first_name %></h1>
+ <h1>Welcome to example.com, <%=h @user.first_name %></h1>
<p>
- You have successfully signed up to example.com, and your username is: <%= @user.login %>.<br/>
- To login to the site, just follow this link: <%= @url %>.
+ You have successfully signed up to example.com, and your username is: <%=h @user.login %>.<br/>
+ To login to the site, just follow this link: <%=h @url %>.
</p>
<p>Thanks for joining and have a great day!</p>
</body>
</html>
2.1.4. Wire it up so that the system sends the email when a user signs up
-# Code that already exists
Rails::Initializer.run do |config|
-
# Code that already exists
-
config.active_record.observers = :user_observer
-
end
2.2. Action Mailer and dynamic deliver_ methods
+2.2. Action Mailer and dynamic deliver_* methods
So, how exactly does this work?
In ActionMailer:Base, you will find this:
In ActionMailer::Base, you will find this:
Ah, this makes things so much clearer :) so if the method name starts with deliver_ followed by any combination of lowercase letters or underscore, method missing calls new on your mailer class (UserMailer in our example above), sending the combination of lower case letters or underscore, along with the parameter. The resulting object is then sent the deliver! method, which well... delivers it.
Hence, if the method name starts with "deliver_" followed by any combination of lowercase letters or underscore, method_missing+ calls `new on your mailer class (UserMailer in our example above), sending the combination of lower case letters or underscore, along with the parameters. The resulting object is then sent the deliver! method, which well... delivers it.
WEBrick isn’t your only option for serving Rails. We’ll get to that in a later section. [XXX: which section] |
Here we’ll flex our server command, which without any prodding of any kind will run our new shiny Rails app:
Without any prodding of any kind, server will run our new shiny Rails app:
$ mkdir gitapp +$ cd gitapp +$ git init +Initialized empty Git repository in .git/ +$ rails . --git --database=postgresql + exists + create app/controllers + create app/helpers +... +... + create tmp/cache + create tmp/pids + create Rakefile +add 'Rakefile' + create README +add 'README' + create app/controllers/application.rb +add 'app/controllers/application.rb' + create app/helpers/application_helper.rb +... + create log/test.log +add 'log/test.log'
We had to create the gitapp directory and initialize an empty git repository before Rails would add files it created to our repository. Let’s see what it put in our database configuration:
$ cat config/database.yml +# PostgreSQL. Versions 7.4 and 8.x are supported. +# +# Install the ruby-postgres driver: +# gem install ruby-postgres +# On Mac OS X: +# gem install ruby-postgres -- --include=/usr/local/pgsql +# On Windows: +# gem install ruby-postgres +# Choose the win32 build. +# Install PostgreSQL and put its /bin directory on your path. +development: + adapter: postgresql + encoding: unicode + database: gitapp_development + pool: 5 + username: gitapp + password: +... +...
It also generated some lines in our database.yml configuration corresponding to our choice of PostgreSQL for database. The only catch with using the SCM options is that you have to make your application’s directory first, then initialize your SCM, then you can run the rails command to generate the basis of your app.
* -* November 3, 2008: Formatting patch from Dave Rothlisberger -* November 1, 2008: First approved version by Mike Gunderloy -* October 16, 2008: Revised based on feedback from Pratik Naik by Mike Gunderloy (not yet approved for publication) -* October 13, 2008: First complete draft by Mike Gunderloy (not yet approved for publication) -* October 12, 2008: More detail, rearrangement, editing by Mike Gunderloy (not yet approved for publication) -* September 8, 2008: initial version by James Miller (not yet approved for publication)
+February 1, 2009: Updated for Rails 2.3 by Mike Gunderloy +
++November 3, 2008: Formatting patch from Dave Rothlisberger +
++November 1, 2008: First approved version by Mike Gunderloy +
++October 16, 2008: Revised based on feedback from Pratik Naik by Mike Gunderloy (not yet approved for publication) +
++October 13, 2008: First complete draft by Mike Gunderloy (not yet approved for publication) +
++October 12, 2008: More detail, rearrangement, editing by Mike Gunderloy (not yet approved for publication) +
++September 8, 2008: initial version by James Miller (not yet approved for publication) +
+Of course, in production environment you would need much robust code, and could use a plugin such as Iaian Hecker’s http_accept_language.
Of course, in production environment you would need much robust code, and could use a plugin such as Iaian Hecker’s http_accept_language or even Rack middleware such as Ryan Tomayko’s locale.
Another way of choosing the locale from client’s information would be to use a database for mapping client IP to region, such as GeoIP Lite Country. The mechanics of the code would be very similar to the code above — you would need to query database for user’s IP, and lookup your preffered locale for the country/region/city returned.
You may use YAML (.yml) or plain Ruby (.rb) files for storing your translations in SimpleStore. YAML is the preffered option among Rails developers, has one big disadvantage, though. YAML is very sensitive to whitespace and special characters, so the application may not load your dictionary properly. Ruby files will crash your application on first request, so you may easily find what’s wrong. (If you encounter any "weird issues" with YAML dictionaries, try putting the relevant portion of your dictionary into Ruby file.)
OK! Now let’s add a timestamp to the view, so we can demo the date/time localization feature as well. To localize the time format you pass the Time object to I18n.l or (preferably) use Rails' #l helper. You can pick a format by passing the :format option — by default the :default format is used.
- You have successfully signed up to example.com, and your username is: <%= @user.login %>.
- To login to the site, just follow this link: <%= @url %>.
+ You have successfully signed up to example.com, and your username is: <%=h @user.login %>.
+ To login to the site, just follow this link: <%=h @url %>.
Thanks for joining and have a great day!
@@ -87,21 +87,18 @@ The file can look like: ------------------------------------------------------- ==== Wire it up so that the system sends the email when a user signs up -There are 3 ways to achieve this. One is to send the email from the controller that sends the email, another is to put it in a before_create block in the user model, and the last one is to use an observer on the user model. Whether you use the second or third methods is up to you, but staying away from the first is recommended. Not because it's wrong, but because it keeps your controller clean, and keeps all logic related to the user model within the user model. This way, whichever way a user is created (from a web form, or from an API call, for example), we are guaranteed that the email will be sent. +There are three ways to achieve this. One is to send the email from the controller that sends the email, another is to put it in a `before_create` callback in the user model, and the last one is to use an observer on the user model. Whether you use the second or third methods is up to you, but staying away from the first is recommended. Not because it's wrong, but because it keeps your controller clean, and keeps all logic related to the user model within the user model. This way, whichever way a user is created (from a web form, or from an API call, for example), we are guaranteed that the email will be sent. Let's see how we would go about wiring it up using an observer: -In config/environment.rb: +In `config/environment.rb`: [source, ruby] ------------------------------------------------------- # Code that already exists Rails::Initializer.run do |config| - # Code that already exists - config.active_record.observers = :user_observer - end ------------------------------------------------------- @@ -121,8 +118,7 @@ Rails::Initializer.run do |config| end ------------------------------------------------------- -ALMOST THERE :) Now all we need is that danged observer, and we're done: -Create a file called user_observer in app/models or app/observers depending on where you stored it, and make it look like: +Now create a file called user_observer in app/models or app/observers depending on where you stored it, and make it look like: [source, ruby] ------------------------------------------------------- class UserObserver < ActiveRecord::Observer @@ -132,22 +128,22 @@ class UserObserver < ActiveRecord::Observer end ------------------------------------------------------- -Notice how we call deliver_welcome_email? Where is that method? Well if you remember, we created a method called welcome_email in UserMailer, right? Well, as part of the "magic" of rails, we deliver the email identified by welcome_email by calling deliver_welcome_email. The next section will go through this in more detail. +Notice how we call `deliver_welcome_email`? Where is that method? Well if you remember, we created a method called `welcome_email` in UserMailer, right? Well, as part of the "magic" of Rails, we deliver the email identified by `welcome_email` by calling `deliver_welcome_email`. The next section will go through this in more detail. That's it! Now whenever your users signup, they will be greeted with a nice welcome email. -=== Action Mailer and dynamic deliver_ methods +=== Action Mailer and dynamic deliver_* methods So how does Action Mailer understand this deliver_welcome_email call? If you read the documentation (http://api.rubyonrails.org/files/vendor/rails/actionmailer/README.html), you will find this in the "Sending Emails" section: You never instantiate your mailer class. Rather, your delivery instance methods are automatically wrapped in class methods that start with the word -deliver_ followed by the name of the mailer method that you would -like to deliver. The signup_notification method defined above is -delivered by invoking Notifier.deliver_signup_notification. +"deliver_" followed by the name of the mailer method that you would +like to deliver. The `welcome_email` method defined above is +delivered by invoking `Notifier.deliver_welcome_email`. So, how exactly does this work? -In ActionMailer:Base, you will find this: +In ActionMailer::Base, you will find this: [source, ruby] ------------------------------------------------------- def method_missing(method_symbol, *parameters)#:nodoc: @@ -160,7 +156,7 @@ def method_missing(method_symbol, *parameters)#:nodoc: end ------------------------------------------------------- -Ah, this makes things so much clearer :) so if the method name starts with deliver_ followed by any combination of lowercase letters or underscore, method missing calls new on your mailer class (UserMailer in our example above), sending the combination of lower case letters or underscore, along with the parameter. The resulting object is then sent the deliver! method, which well... delivers it. +Hence, if the method name starts with "deliver_" followed by any combination of lowercase letters or underscore, `method_missing+ calls `new` on your mailer class (UserMailer in our example above), sending the combination of lower case letters or underscore, along with the parameters. The resulting object is then sent the `deliver!` method, which well... delivers it. === Complete List of ActionMailer user-settable attributes @@ -480,4 +476,4 @@ class UserMailerTest < ActionMailer::TestCase What have we done? Well, we sent the email and stored the returned object in the email variable. We then ensured that it was sent (the first assert), then, in the second batch of assertion, we ensure that the email does indeed contain the values that we expect. == Epilogue -This guide presented how to create a mailer and how to test it. In reality, you may find that writing your tests before you actually write your code to be a rewarding experience. It may take some time to get used to TDD (Test Driven Development), but coding this way achieves two major benefits. Firstly, you know that the code does indeed work, because the tests fail (because there's no code), then they pass, because the code that satisfies the tests was written. Secondly, when you start with the tests, you don't have to make time AFTER you write the code, to write the tests, then never get around to it. The tests are already there and testing has now become part of your coding regimen. \ No newline at end of file +This guide presented how to create a mailer and how to test it. In reality, you may find that writing your tests before you actually write your code to be a rewarding experience. It may take some time to get used to TDD (Test Driven Development), but coding this way achieves two major benefits. Firstly, you know that the code does indeed work, because the tests fail (because there's no code), then they pass, because the code that satisfies the tests was written. Secondly, when you start with the tests, you don't have to make time AFTER you write the code, to write the tests, then never get around to it. The tests are already there and testing has now become part of your coding regimen. diff --git a/railties/doc/guides/source/association_basics.txt b/railties/doc/guides/source/association_basics.txt index 95d7397558..80605c1e83 100644 --- a/railties/doc/guides/source/association_basics.txt +++ b/railties/doc/guides/source/association_basics.txt @@ -624,7 +624,7 @@ end The +belongs_to+ association supports these options: -// * +:accessible+ +* +:autosave+ * +:class_name+ * +:conditions+ * +:counter_cache+ @@ -636,10 +636,10 @@ The +belongs_to+ association supports these options: * +:select+ * +:validate+ -// ===== +:accessible+ -// -// The +:accessible+ option is the association version of +ActiveRecord::Base#attr_accessible+. If you set the +:accessible+ option to true, then mass // assignment is allowed for this association. -// +===== +:autosave+ + +If you set the +:autosave+ option to +true+, Rails will save any loaded members and destroy members that are marked for destruction whenever you save the parent object. + ===== +:class_name+ If the name of the other model cannot be derived from the association name, you can use the +:class_name+ option to supply the model name. For example, if an order belongs to a customer, but the actual name of the model containing customers is +Patron+, you'd set things up this way: @@ -877,8 +877,8 @@ end The +has_one+ association supports these options: -// * +:accessible+ * +:as+ +* +:autosave+ * +:class_name+ * +:conditions+ * +:dependent+ @@ -893,14 +893,14 @@ The +has_one+ association supports these options: * +:through+ * +:validate+ -// ===== +:accessible+ -// -// The +:accessible+ option is the association version of +ActiveRecord::Base#attr_accessible+. If you set the +:accessible+ option to true, then mass // assignment is allowed for this association. -// ===== +:as+ Setting the +:as+ option indicates that this is a polymorphic association. Polymorphic associations are discussed in detail later in this guide. +===== +:autosave+ + +If you set the +:autosave+ option to +true+, Rails will save any loaded members and destroy members that are marked for destruction whenever you save the parent object. + ===== +:class_name+ If the name of the other model cannot be derived from the association name, you can use the +:class_name+ option to supply the model name. For example, if a supplier has an account, but the actual name of the model containing accounts is Billing, you'd set things up this way: @@ -1181,8 +1181,8 @@ end The +has_many+ association supports these options: -// * +:accessible+ * +:as+ +* +:autosave+ * +:class_name+ * +:conditions+ * +:counter_sql+ @@ -1204,14 +1204,14 @@ The +has_many+ association supports these options: * +:uniq+ * +:validate+ -// ===== +:accessible+ -// -// The +:accessible+ option is the association version of +ActiveRecord::Base#attr_accessible+. If you set the +:accessible+ option to true, then mass // assignment is allowed for this association. -// ===== +:as+ Setting the +:as+ option indicates that this is a polymorphic association, as discussed earlier in this guide. +===== +:autosave+ + +If you set the +:autosave+ option to +true+, Rails will save any loaded members and destroy members that are marked for destruction whenever you save the parent object. + ===== +:class_name+ If the name of the other model cannot be derived from the association name, you can use the +:class_name+ option to supply the model name. For example, if a customer has many orders, but the actual name of the model containing orders is +Transaction+, you'd set things up this way: @@ -1566,8 +1566,8 @@ end The +has_and_belongs_to_many+ association supports these options: -// * +:accessible+ * +:association_foreign_key+ +* +:autosave+ * +:class_name+ * +:conditions+ * +:counter_sql+ @@ -1587,10 +1587,6 @@ The +has_and_belongs_to_many+ association supports these options: * +:uniq+ * +:validate+ -// ===== +:accessible+ -// -// The +:accessible+ option is the association version of +ActiveRecord::Base#attr_accessible+. If you set the +:accessible+ option to true, then mass // assignment is allowed for this association. -// ===== +:association_foreign_key+ By convention, Rails guesses that the column in the join table used to hold the foreign key pointing to the other model is the name of that model with the suffix +_id+ added. The +:association_foreign_key+ option lets you set the name of the foreign key directly: @@ -1605,6 +1601,10 @@ class User < ActiveRecord::Base end ------------------------------------------------------- +===== +:autosave+ + +If you set the +:autosave+ option to +true+, Rails will save any loaded members and destroy members that are marked for destruction whenever you save the parent object. + ===== +:class_name+ If the name of the other model cannot be derived from the association name, you can use the +:class_name+ option to supply the model name. For example, if a part has many assemblies, but the actual name of the model containing assemblies is +Gadget+, you'd set things up this way: @@ -1841,6 +1841,7 @@ Extensions can refer to the internals of the association proxy using these three http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/11[Lighthouse ticket] +* February 1, 2009: Added +:autosave+ option link:../authors.html#mgunderloy[Mike Gunderloy] * September 28, 2008: Corrected +has_many :through+ diagram, added polymorphic diagram, some reorganization by link:../authors.html#mgunderloy[Mike Gunderloy] . First release version. * September 22, 2008: Added diagrams, misc. cleanup by link:../authors.html#mgunderloy[Mike Gunderloy] (not yet approved for publication) * September 14, 2008: initial version by link:../authors.html#mgunderloy[Mike Gunderloy] (not yet approved for publication) diff --git a/railties/doc/guides/source/command_line.txt b/railties/doc/guides/source/command_line.txt index 8a887bd001..4f1dd49f35 100644 --- a/railties/doc/guides/source/command_line.txt +++ b/railties/doc/guides/source/command_line.txt @@ -56,7 +56,7 @@ Let's try it! The `server` command launches a small web server named WEBrick whi NOTE: WEBrick isn't your only option for serving Rails. We'll get to that in a later section. [XXX: which section] -Here we'll flex our `server` command, which without any prodding of any kind will run our new shiny Rails app: +Without any prodding of any kind, `server` will run our new shiny Rails app: [source,shell] ------------------------------------------------------ @@ -318,7 +318,7 @@ $ ./script/destroy model Oops ------------------------------------------------------ === about === -Check it: Version numbers for Ruby, RubyGems, Rails, the Rails subcomponents, your application's folder, the current Rails environment name, your app's database adapter, and schema version! `about` is useful when you need to ask help, check if a security patch might affect you, or when you need some stats for an existing Rails installation. +Check it: Version numbers for Ruby, RubyGems, Rails, the Rails subcomponents, your application's folder, the current Rails environment name, your app's database adapter, and schema version! `about` is useful when you need to ask for help, check if a security patch might affect you, or when you need some stats for an existing Rails installation. [source,shell] ------------------------------------------------------ @@ -337,4 +337,66 @@ Application root /home/commandsapp Environment development Database adapter sqlite3 Database schema version 20081217073400 ------------------------------------------------------- \ No newline at end of file +------------------------------------------------------ + +== The Rails Advanced Command Line (RACL!) == +The more advanced uses of the command line are focused around finding useful (even surprising at times) options in the utilities, and fitting utilities to your needs and specific work flow. Listed here are some tricks up Rails' sleeve. + +=== rails with databases and SCM === +When creating a new Rails application, you have the option to specify what kind of database and what kind of source code management system your application is going to use. This will save you a few minutes, and certainly many keystrokes. + +Let's see what a `--git` option and a `--database=postgresql` option will do for us: + +[source,shell] +------------------------------------------------------ +$ mkdir gitapp +$ cd gitapp +$ git init +Initialized empty Git repository in .git/ +$ rails . --git --database=postgresql + exists + create app/controllers + create app/helpers +... +... + create tmp/cache + create tmp/pids + create Rakefile +add 'Rakefile' + create README +add 'README' + create app/controllers/application.rb +add 'app/controllers/application.rb' + create app/helpers/application_helper.rb +... + create log/test.log +add 'log/test.log' +------------------------------------------------------ + +We had to create the *gitapp* directory and initialize an empty git repository before Rails would add files it created to our repository. Let's see what it put in our database configuration: + +[source,shell] +------------------------------------------------------ +$ cat config/database.yml +# PostgreSQL. Versions 7.4 and 8.x are supported. +# +# Install the ruby-postgres driver: +# gem install ruby-postgres +# On Mac OS X: +# gem install ruby-postgres -- --include=/usr/local/pgsql +# On Windows: +# gem install ruby-postgres +# Choose the win32 build. +# Install PostgreSQL and put its /bin directory on your path. +development: + adapter: postgresql + encoding: unicode + database: gitapp_development + pool: 5 + username: gitapp + password: +... +... +------------------------------------------------------ + +It also generated some lines in our database.yml configuration corresponding to our choice of PostgreSQL for database. The only catch with using the SCM options is that you have to make your application's directory first, then initialize your SCM, then you can run the `rails` command to generate the basis of your app. \ No newline at end of file diff --git a/railties/doc/guides/source/getting_started_with_rails.txt b/railties/doc/guides/source/getting_started_with_rails.txt index 43322822fe..34bd6ca5e5 100644 --- a/railties/doc/guides/source/getting_started_with_rails.txt +++ b/railties/doc/guides/source/getting_started_with_rails.txt @@ -1342,7 +1342,7 @@ Rails also comes with built-in help that you can generate using the rake command http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/2[Lighthouse ticket] -* +* February 1, 2009: Updated for Rails 2.3 by link:../authors.html#mgunderloy[Mike Gunderloy] * November 3, 2008: Formatting patch from Dave Rothlisberger * November 1, 2008: First approved version by link:../authors.html#mgunderloy[Mike Gunderloy] * October 16, 2008: Revised based on feedback from Pratik Naik by link:../authors.html#mgunderloy[Mike Gunderloy] (not yet approved for publication) diff --git a/railties/doc/guides/source/i18n.txt b/railties/doc/guides/source/i18n.txt index 4465a77289..19393f4527 100644 --- a/railties/doc/guides/source/i18n.txt +++ b/railties/doc/guides/source/i18n.txt @@ -315,7 +315,7 @@ def extract_locale_from_accept_language_header end ------------------------------------------------------- -Of course, in production environment you would need much robust code, and could use a plugin such as Iaian Hecker's http://github.com/iain/http_accept_language[http_accept_language]. +Of course, in production environment you would need much robust code, and could use a plugin such as Iaian Hecker's http://github.com/iain/http_accept_language[http_accept_language] or even Rack middleware such as Ryan Tomayko's http://github.com/rtomayko/rack-contrib/blob/master/lib/rack/locale.rb[locale]. ==== Using GeoIP (or similar) database @@ -403,6 +403,8 @@ image:images/i18n/demo_translated_pirate.png[rails i18n demo translated to pirat NOTE: You need to restart the server when you add new locale files. +You may use YAML (+.yml+) or plain Ruby (+.rb+) files for storing your translations in SimpleStore. YAML is the preffered option among Rails developers, has one big disadvantage, though. YAML is very sensitive to whitespace and special characters, so the application may not load your dictionary properly. Ruby files will crash your application on first request, so you may easily find what's wrong. (If you encounter any "weird issues" with YAML dictionaries, try putting the relevant portion of your dictionary into Ruby file.) + === Adding Date/Time formats OK! Now let's add a timestamp to the view, so we can demo the *date/time localization* feature as well. To localize the time format you pass the Time object to +I18n.l+ or (preferably) use Rails' +#l+ helper. You can pick a format by passing the +:format+ option -- by default the +:default+ format is used. -- cgit v1.2.3