@@ -233,16 +233,16 @@ Blog::Application.routes.draw do
# ...
# You can have the root of your site routed with "root"
# just remember to delete public/index.html.
# root :to => "welcome#index"
# root to: "welcome#index"
```
This is your application's _routing file_ which holds entries in a special DSL (domain-specific language) that tells Rails how to connect incoming requests to controllers and actions. This file contains many sample routes on commented lines, and one of them actually shows you how to connect the root of your site to a specific controller and action. Find the line beginning with `root :to` and uncomment it. It should look something like the following:
```ruby
root:to=>"welcome#index"
rootto:"welcome#index"
```
The `root :to => "welcome#index"` tells Rails to map requests to the root of the application to the welcome controller's index action and `get "welcome/index"` tells Rails to map requests to <http://localhost:3000/welcome/index> to the welcome controller's index action. This was created earlier when you ran the controller generator (`rails generate controller welcome index`).
The `root to: "welcome#index"` tells Rails to map requests to the root of the application to the welcome controller's index action and `get "welcome/index"` tells Rails to map requests to <http://localhost:3000/welcome/index> to the welcome controller's index action. This was created earlier when you ran the controller generator (`rails generate controller welcome index`).
If you navigate to <http://localhost:3000> in your browser, you'll see the `Hello, Rails!` message you put into `app/views/welcome/index.html.erb`, indicating that this new route is indeed going to `WelcomeController`'s `index` action and is rendering the view correctly.
...
...
@@ -318,7 +318,7 @@ You're getting this error now because Rails expects plain actions like this one
In the above image, the bottom line has been truncated. Let's see what the full thing looks like:
In this example, a `Hash` object is passed to the `:url` option. What Rails will do with this is that it will point the form to the `create` action of the current controller, the `PostsController`, and will send a `POST` request to that route. For this to work, you will need to add a route to `config/routes.rb`, right underneath the one for "posts/new":
...
...
@@ -417,7 +417,7 @@ When a form is submitted, the fields of the form are sent to Rails as _parameter
```ruby
defcreate
render:text=>params[:post].inspect
rendertext:params[:post].inspect
end
```
...
...
@@ -526,7 +526,7 @@ def create
@post=Post.new(params[:post])
@post.save
redirect_to:action=>:show,:id=>@post.id
redirect_toaction: :show,id:@post.id
end
```
...
...
@@ -639,7 +639,7 @@ Open `app/views/welcome/index.html.erb` and modify it as follows:
```html+erb
<h1>Hello, Rails!</h1>
<%= link_to "My Blog", :controller => "posts" %>
<%= link_to "My Blog", controller: "posts" %>
```
The `link_to` method is one of Rails' built-in view helpers. It creates a
...
...
@@ -649,7 +649,7 @@ for posts.
Let's add links to the other views as well, starting with adding this "New Post" link to `app/views/posts/index.html.erb`, placing it above the `<table>` tag:
```erb
<%=link_to'New post',:action=>:new%>
<%=link_to'New post',action: :new%>
```
This link will allow you to bring up the form that lets you create a new post. You should also add a link to this template -- `app/views/posts/new.html.erb` -- to go back to the `index` action. Do this by adding this underneath the form in this template:
...
...
@@ -659,7 +659,7 @@ This link will allow you to bring up the form that lets you create a new post. Y
...
<%end%>
<%=link_to'Back',:action=>:index%>
<%=link_to'Back',action: :index%>
```
Finally, add another link to the `app/views/posts/show.html.erb` template to go back to the `index` action as well, so that people who are viewing a single post can go back and view the whole list again:
...
...
@@ -675,7 +675,7 @@ Finally, add another link to the `app/views/posts/show.html.erb` template to go
<%= @post.text %>
</p>
<%= link_to 'Back', :action => :index %>
<%= link_to 'Back', action: :index %>
```
TIP: If you want to link to an action in the same controller, you don't
...
...
@@ -723,8 +723,8 @@ Open the `app/models/post.rb` file and edit it:
classPost<ActiveRecord::Base
attr_accessible:text,:title
validates:title,:presence=>true,
:length=>{:minimum=>5}
validates:title,presence:true,
length: {minimum:5}
end
```
...
...
@@ -749,7 +749,7 @@ def create
@post=Post.new(params[:post])
if@post.save
redirect_to:action=>:show,:id=>@post.id
redirect_toaction: :show,id:@post.id
else
render'new'
end
...
...
@@ -770,7 +770,7 @@ something went wrong. To do that, you'll modify
`app/views/posts/new.html.erb` to check for error messages:
@@ -205,7 +205,7 @@ The most usual way of setting (and passing) the locale would be to include it in
This approach has almost the same set of advantages as setting the locale from the domain name: namely that it's RESTful and in accord with the rest of the World Wide Web. It does require a little bit more work to implement, though.
Getting the locale from `params` and setting it accordingly is not hard; including it in every URL and thus **passing it through the requests** is. To include an explicit option in every URL (e.g. `link_to( books_url(:locale => I18n.locale))`) would be tedious and probably impossible, of course.
Getting the locale from `params` and setting it accordingly is not hard; including it in every URL and thus **passing it through the requests** is. To include an explicit option in every URL (e.g. `link_to( books_url(locale: I18n.locale))`) would be tedious and probably impossible, of course.
Rails contains infrastructure for "centralizing dynamic decisions about the URLs" in its [`ApplicationController#default_url_options`](http://api.rubyonrails.org/classes/ActionController/Base.html#M000515, which is useful precisely in this scenario: it enables us to set "defaults" for [`url_for`](http://api.rubyonrails.org/classes/ActionController/Base.html#M000503) and helper methods dependent on it (by implementing/overriding this method).
...
...
@@ -215,7 +215,7 @@ We can include something like this in our `ApplicationController` then:
# app/controllers/application_controller.rb
defdefault_url_options(options={})
logger.debug"default_url_options is passed options: #{options.inspect}\n"
{:locale=>I18n.locale}
{locale:I18n.locale}
end
```
...
...
@@ -238,14 +238,14 @@ If you don't want to force the use of a locale in your routes you can use an opt
```ruby
# config/routes.rb
scope"(:locale)",:locale=>/en|nl/do
scope"(:locale)",locale:/en|nl/do
resources:books
end
```
With this approach you will not get a `Routing Error` when accessing your resources such as `http://localhost:3001/books` without a locale. This is useful for when you want to use the default locale when one is not specified.
Of course, you need to take special care of the root URL (usually "homepage" or "dashboard") of your application. An URL like `http://localhost:3001/nl` will not work automatically, because the `root :to => "books#index"` declaration in your `routes.rb` doesn't take locale into account. (And rightly so: there's only one "root" URL.)
Of course, you need to take special care of the root URL (usually "homepage" or "dashboard") of your application. An URL like `http://localhost:3001/nl` will not work automatically, because the `root to: "books#index"` declaration in your `routes.rb` doesn't take locale into account. (And rightly so: there's only one "root" URL.)
You would probably need to map URLs like these:
...
...
@@ -303,7 +303,7 @@ You most probably have something like this in one of your applications:
```ruby
# config/routes.rb
Yourapp::Application.routes.drawdo
root:to=>"home#index"
rootto:"home#index"
end
```
...
...
@@ -381,7 +381,7 @@ You can use variables in the translation messages and pass their values from the
@@ -398,7 +398,7 @@ OK! Now let's add a timestamp to the view, so we can demo the **date/time locali
# app/views/home/index.html.erb
<h1><%=t:hello_world%></h1>
<p><%=flash[:notice]%></p
<p><%=lTime.now,:format=>:short%></p>
<p><%=lTime.now,format: :short%></p>
```
And in our pirate translations file let's add a time format (it's already there in Rails' defaults for English):
...
...
@@ -495,7 +495,7 @@ I18n.t 'message'
The `translate` method also takes a `:scope` option which can contain one or more additional keys that will be used to specify a “namespace” or scope for a translation key:
When a `:default` option is given, its value will be returned if the translation is missing:
```ruby
I18n.t:missing,:default=>'Not here'
I18n.t:missing,default:'Not here'
# => 'Not here'
```
...
...
@@ -529,7 +529,7 @@ If the `:default` value is a Symbol, it will be used as a key and translated. On
E.g., the following first tries to translate the key `:missing` and then the key `:also_missing.` As both do not yield a result, the string "Not here" will be returned:
@@ -587,14 +587,14 @@ In English there are only one singular and one plural form for a given string, e
The `:count` interpolation variable has a special role in that it both is interpolated to the translation and used to pick a pluralization from the translations according to the pluralization rules defined by CLDR:
```ruby
I18n.backend.store_translations:en,:inbox=>{
:one=>'one message',
:other=>'%{count} messages'
I18n.backend.store_translations:en,inbox:{
one:'one message',
other:'%{count} messages'
}
I18n.translate:inbox,:count=>2
I18n.translate:inbox,count:2
# => '2 messages'
I18n.translate:inbox,:count=>1
I18n.translate:inbox,count:1
# => 'one message'
```
...
...
@@ -623,8 +623,8 @@ I18n.l Time.now
Explicitly passing a locale:
```ruby
I18n.t:foo,:locale=>:de
I18n.lTime.now,:locale=>:de
I18n.t:foo,locale: :de
I18n.lTime.now,locale: :de
```
The `I18n.locale` defaults to `I18n.default_locale` which defaults to :`en`. The default locale can be set like this:
...
...
@@ -665,9 +665,9 @@ For example a Ruby Hash providing translations can look like this:
```ruby
{
:pt=>{
:foo=>{
:bar=>"baz"
pt:{
foo:{
bar:"baz"
}
}
}
...
...
@@ -698,9 +698,9 @@ So, all of the following equivalent lookups will return the `:short` date format
```ruby
I18n.t'date.formats.short'
I18n.t'formats.short',:scope=>:date
I18n.t:short,:scope=>'date.formats'
I18n.t:short,:scope=>[:date,:formats]
I18n.t'formats.short',scope: :date
I18n.t:short,scope:'date.formats'
I18n.t:short,scope:[:date,:formats]
```
Generally we recommend using YAML as a format for storing translations. There are cases, though, where you want to store Ruby lambdas as part of your locale data, e.g. for special date formats.
...
...
@@ -734,7 +734,7 @@ Consider a User model with a validation for the name attribute like this:
```ruby
classUser<ActiveRecord::Base
validates:name,:presence=>true
validates:name,presence:true
end
```
...
...
@@ -764,7 +764,7 @@ For example, you might have an Admin model inheriting from User:
```ruby
classAdmin<User
validates:name,:presence=>true
validates:name,presence:true
end
```
...
...
@@ -930,7 +930,7 @@ Another example where the default behaviour is less desirable is the Rails Trans
To do so, the helper forces `I18n#translate` to raise exceptions no matter what exception handler is defined by setting the `:raise` option:
```ruby
I18n.t:foo,:raise=>true# always re-raises exceptions from the backend
I18n.t:foo,raise:true# always re-raises exceptions from the backend