aboutsummaryrefslogtreecommitdiffstats
path: root/guides
diff options
context:
space:
mode:
Diffstat (limited to 'guides')
-rw-r--r--guides/source/getting_started.md88
-rw-r--r--guides/source/i18n.md66
-rw-r--r--guides/source/performance_testing.md4
3 files changed, 79 insertions, 79 deletions
diff --git a/guides/source/getting_started.md b/guides/source/getting_started.md
index 9f23f9fc42..cf11a427ad 100644
--- a/guides/source/getting_started.md
+++ b/guides/source/getting_started.md
@@ -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"
+root to: "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:
<blockquote>
-Missing template posts/new, application/new with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in: * "/path/to/blog/app/views"
+Missing template posts/new, application/new with {locale:[:en], formats:[:html], handlers:[:erb, :builder, :coffee]}. Searched in: * "/path/to/blog/app/views"
</blockquote>
That's quite a lot of text! Let's quickly go through and understand what each part of it does.
@@ -380,7 +380,7 @@ like this is called "create", and so the form should be pointed to that action.
Edit the `form_for` line inside `app/views/posts/new.html.erb` to look like this:
```html+erb
-<%= form_for :post, :url => { :action => :create } do |f| %>
+<%= form_for :post, url: { action: :create } do |f| %>
```
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
def create
- render :text => params[:post].inspect
+ render text: 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_to action: :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:
class Post < 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_to action: :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:
```html+erb
-<%= form_for :post, :url => { :action => :create } do |f| %>
+<%= form_for :post, url: { action: :create } do |f| %>
<% if @post.errors.any? %>
<div id="errorExplanation">
<h2><%= pluralize(@post.errors.count, "error") %> prohibited
@@ -797,7 +797,7 @@ something went wrong. To do that, you'll modify
</p>
<% end %>
-<%= link_to 'Back', :action => :index %>
+<%= link_to 'Back', action: :index %>
```
A few things are going on. We check if there are any errors with
@@ -847,8 +847,8 @@ it look as follows:
```html+erb
<h1>Editing post</h1>
-<%= form_for :post, :url => { :action => :update, :id => @post.id },
-:method => :put do |f| %>
+<%= form_for :post, url: { action: :update, id: @post.id },
+method: :put do |f| %>
<% if @post.errors.any? %>
<div id="errorExplanation">
<h2><%= pluralize(@post.errors.count, "error") %> prohibited
@@ -875,13 +875,13 @@ it look as follows:
</p>
<% end %>
-<%= link_to 'Back', :action => :index %>
+<%= link_to 'Back', action: :index %>
```
This time we point the form to the `update` action, which is not defined yet
but will be very soon.
-The `:method => :put` option tells Rails that we want this form to be
+The `method: :put` option tells Rails that we want this form to be
submitted via the `PUT`, HTTP method which is the HTTP method you're expected to use to
**update** resources according to the REST protocol.
@@ -901,7 +901,7 @@ def update
@post = Post.find(params[:id])
if @post.update_attributes(params[:post])
- redirect_to :action => :show, :id => @post.id
+ redirect_to action: :show, id: @post.id
else
render 'edit'
end
@@ -914,7 +914,7 @@ that you want to update. As before, if there was an error updating the
post we want to show the form back to the user.
TIP: you don't need to pass all attributes to `update_attributes`. For
-example, if you'd call `@post.update_attributes(:title => 'A new title')`
+example, if you'd call `@post.update_attributes(title: 'A new title')`
Rails would only update the `title` attribute, leaving all other
attributes untouched.
@@ -935,8 +935,8 @@ appear next to the "Show" link:
<tr>
<td><%= post.title %></td>
<td><%= post.text %></td>
- <td><%= link_to 'Show', :action => :show, :id => post.id %></td>
- <td><%= link_to 'Edit', :action => :edit, :id => post.id %></td>
+ <td><%= link_to 'Show', action: :show, id: post.id %></td>
+ <td><%= link_to 'Edit', action: :edit, id: post.id %></td>
</tr>
<% end %>
</table>
@@ -949,8 +949,8 @@ the template:
```html+erb
...
-<%= link_to 'Back', :action => :index %>
-| <%= link_to 'Edit', :action => :edit, :id => @post.id %>
+<%= link_to 'Back', action: :index %>
+| <%= link_to 'Edit', action: :edit, id: @post.id %>
```
And here's how our app looks so far:
@@ -1031,7 +1031,7 @@ completely:
<%= render 'form' %>
-<%= link_to 'Back', :action => :index %>
+<%= link_to 'Back', action: :index %>
```
Then do the same for the `app/views/posts/edit.html.erb` view:
@@ -1041,7 +1041,7 @@ Then do the same for the `app/views/posts/edit.html.erb` view:
<%= render 'form' %>
-<%= link_to 'Back', :action => :index %>
+<%= link_to 'Back', action: :index %>
```
Point your browser to <http://localhost:3000/posts/new> and
@@ -1082,7 +1082,7 @@ To fix this, open `config/routes.rb` and modify the `get "posts/:id"`
line like this:
```ruby
-get "posts/:id" => "posts#show", :as => :post
+get "posts/:id" => "posts#show", as: :post
```
The `:as` option tells the `get` method that we want to make routing helpers
@@ -1120,7 +1120,7 @@ def destroy
@post = Post.find(params[:id])
@post.destroy
- redirect_to :action => :index
+ redirect_to action: :index
end
```
@@ -1147,9 +1147,9 @@ together.
<tr>
<td><%= post.title %></td>
<td><%= post.text %></td>
- <td><%= link_to 'Show', :action => :show, :id => post.id %></td>
- <td><%= link_to 'Edit', :action => :edit, :id => post.id %></td>
- <td><%= link_to 'Destroy', { :action => :destroy, :id => post.id }, :method => :delete, :data => { :confirm => 'Are you sure?' } %></td>
+ <td><%= link_to 'Show', action: :show, id: post.id %></td>
+ <td><%= link_to 'Edit', action: :edit, id: post.id %></td>
+ <td><%= link_to 'Destroy', { action: :destroy, id: post.id }, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
@@ -1181,7 +1181,7 @@ declaring separate routes with the appropriate verbs into
get "posts" => "posts#index"
get "posts/new"
post "posts" => "posts#create"
-get "posts/:id" => "posts#show", :as => :post
+get "posts/:id" => "posts#show", as: :post
get "posts/:id/edit" => "posts#edit"
put "posts/:id" => "posts#update"
delete "posts/:id" => "posts#destroy"
@@ -1197,7 +1197,7 @@ Blog::Application.routes.draw do
resources :posts
- root :to => "welcome#index"
+ root to: "welcome#index"
end
```
@@ -1324,8 +1324,8 @@ You'll need to edit the `post.rb` file to add the other side of the association:
```ruby
class Post < ActiveRecord::Base
- validates :title, :presence => true,
- :length => { :minimum => 5 }
+ validates :title, presence: true,
+ length: { minimum: 5 }
has_many :comments
end
@@ -1635,8 +1635,8 @@ So first, let's add the delete link in the
<p>
<%= link_to 'Destroy Comment', [comment.post, comment],
- :method => :delete,
- :data => { :confirm => 'Are you sure?' } %>
+ method: :delete,
+ data: { confirm: 'Are you sure?' } %>
</p>
```
@@ -1678,9 +1678,9 @@ model, `app/models/post.rb`, as follows:
```ruby
class Post < ActiveRecord::Base
- validates :title, :presence => true,
- :length => { :minimum => 5 }
- has_many :comments, :dependent => :destroy
+ validates :title, presence: true,
+ length: { minimum: 5 }
+ has_many :comments, dependent: :destroy
end
```
@@ -1705,7 +1705,7 @@ action, except for `index` and `show`, so we write that:
```ruby
class PostsController < ApplicationController
- http_basic_authenticate_with :name => "dhh", :password => "secret", :except => [:index, :show]
+ http_basic_authenticate_with name: "dhh", password: "secret", except: [:index, :show]
def index
@posts = Post.all
@@ -1720,7 +1720,7 @@ We also only want to allow authenticated users to delete comments, so in the
```ruby
class CommentsController < ApplicationController
- http_basic_authenticate_with :name => "dhh", :password => "secret", :only => :destroy
+ http_basic_authenticate_with name: "dhh", password: "secret", only: :destroy
def create
@post = Post.find(params[:post_id])
diff --git a/guides/source/i18n.md b/guides/source/i18n.md
index eda1881e73..e916bda630 100644
--- a/guides/source/i18n.md
+++ b/guides/source/i18n.md
@@ -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
def default_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.draw do
- root :to => "home#index"
+ root to: "home#index"
end
```
@@ -381,7 +381,7 @@ You can use variables in the translation messages and pass their values from the
```erb
# app/views/home/index.html.erb
-<%=t 'greet_username', :user => "Bill", :message => "Goodbye" %>
+<%=t 'greet_username', user: "Bill", message: "Goodbye" %>
```
```yaml
@@ -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><%= l Time.now, :format => :short %></p>
+<p><%= l Time.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:
```ruby
-I18n.t :record_invalid, :scope => [:activerecord, :errors, :messages]
+I18n.t :record_invalid, scope: [:activerecord, :errors, :messages]
```
This looks up the `:record_invalid` message in the Active Record error messages.
@@ -510,9 +510,9 @@ Thus the following calls are equivalent:
```ruby
I18n.t 'activerecord.errors.messages.record_invalid'
-I18n.t 'errors.messages.record_invalid', :scope => :active_record
-I18n.t :record_invalid, :scope => 'activerecord.errors.messages'
-I18n.t :record_invalid, :scope => [:activerecord, :errors, :messages]
+I18n.t 'errors.messages.record_invalid', scope: :active_record
+I18n.t :record_invalid, scope: 'activerecord.errors.messages'
+I18n.t :record_invalid, scope: [:activerecord, :errors, :messages]
```
#### Defaults
@@ -520,7 +520,7 @@ I18n.t :record_invalid, :scope => [:activerecord, :errors, :messages]
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:
```ruby
-I18n.t :missing, :default => [:also_missing, 'Not here']
+I18n.t :missing, default: [:also_missing, 'Not here']
# => 'Not here'
```
@@ -538,7 +538,7 @@ I18n.t :missing, :default => [:also_missing, 'Not here']
To look up multiple translations at once, an array of keys can be passed:
```ruby
-I18n.t [:odd, :even], :scope => 'errors.messages'
+I18n.t [:odd, :even], scope: 'errors.messages'
# => ["must be odd", "must be even"]
```
@@ -546,7 +546,7 @@ Also, a key can translate to a (potentially nested) hash of grouped translations
```ruby
I18n.t 'activerecord.errors.messages'
-# => { :inclusion => "is not included in the list", :exclusion => ... }
+# => { inclusion: "is not included in the list", exclusion: ... }
```
#### "Lazy" Lookup
@@ -573,8 +573,8 @@ In many cases you want to abstract your translations so that **variables can be
All options besides `:default` and `:scope` that are passed to `#translate` will be interpolated to the translation:
```ruby
-I18n.backend.store_translations :en, :thanks => 'Thanks %{name}!'
-I18n.translate :thanks, :name => 'Jeremy'
+I18n.backend.store_translations :en, thanks: 'Thanks %{name}!'
+I18n.translate :thanks, name: 'Jeremy'
# => 'Thanks Jeremy!'
```
@@ -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.l Time.now, :locale => :de
+I18n.t :foo, locale: :de
+I18n.l Time.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
class User < 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
class Admin < 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
```
Conclusion
diff --git a/guides/source/performance_testing.md b/guides/source/performance_testing.md
index f111ce610f..67ab7cb5b8 100644
--- a/guides/source/performance_testing.md
+++ b/guides/source/performance_testing.md
@@ -65,8 +65,8 @@ require 'rails/performance_test_help'
class HomepageTest < ActionDispatch::PerformanceTest
# Refer to the documentation for all available options
- # self.profile_options = { :runs => 5, :metrics => [:wall_time, :memory],
- # :output => 'tmp/performance', :formats => [:flat] }
+ # self.profile_options = { runs: 5, metrics: [:wall_time, :memory],
+ # output: 'tmp/performance', formats: [:flat] }
test "homepage" do
get '/'