rails 4.0.0 → 4.0.1.rc1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of rails might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/guides/CHANGELOG.md +6 -0
- data/guides/assets/images/akshaysurve.jpg +0 -0
- data/guides/bug_report_templates/action_controller_gem.rb +42 -0
- data/guides/bug_report_templates/action_controller_master.rb +51 -0
- data/guides/code/getting_started/Gemfile +1 -1
- data/guides/code/getting_started/Gemfile.lock +76 -106
- data/guides/code/getting_started/app/controllers/comments_controller.rb +7 -1
- data/guides/code/getting_started/app/controllers/posts_controller.rb +8 -2
- data/guides/code/getting_started/app/views/posts/_form.html.erb +3 -3
- data/guides/code/getting_started/app/views/welcome/index.html.erb +1 -0
- data/guides/code/getting_started/test/fixtures/comments.yml +1 -1
- data/guides/code/getting_started/test/fixtures/posts.yml +1 -1
- data/guides/rails_guides.rb +20 -1
- data/guides/source/3_0_release_notes.md +0 -2
- data/guides/source/4_0_release_notes.md +30 -13
- data/guides/source/_welcome.html.erb +4 -1
- data/guides/source/action_controller_overview.md +2 -2
- data/guides/source/action_mailer_basics.md +6 -21
- data/guides/source/action_view_overview.md +2 -71
- data/guides/source/active_record_basics.md +9 -9
- data/guides/source/active_record_callbacks.md +1 -0
- data/guides/source/active_record_querying.md +45 -5
- data/guides/source/active_record_validations.md +6 -9
- data/guides/source/active_support_core_extensions.md +50 -1
- data/guides/source/asset_pipeline.md +10 -4
- data/guides/source/association_basics.md +8 -4
- data/guides/source/command_line.md +8 -8
- data/guides/source/configuring.md +4 -2
- data/guides/source/contributing_to_ruby_on_rails.md +60 -10
- data/guides/source/credits.html.erb +5 -1
- data/guides/source/debugging_rails_applications.md +4 -2
- data/guides/source/documents.yaml +7 -0
- data/guides/source/form_helpers.md +1 -1
- data/guides/source/generators.md +21 -6
- data/guides/source/getting_started.md +61 -54
- data/guides/source/i18n.md +7 -6
- data/guides/source/layout.html.erb +6 -8
- data/guides/source/layouts_and_rendering.md +4 -4
- data/guides/source/maintenance_policy.md +56 -0
- data/guides/source/migrations.md +13 -11
- data/guides/source/plugins.md +9 -3
- data/guides/source/rails_on_rack.md +5 -1
- data/guides/source/security.md +1 -1
- data/guides/source/testing.md +83 -23
- data/guides/source/upgrading_ruby_on_rails.md +36 -12
- metadata +17 -13
@@ -155,7 +155,7 @@ To begin with, let's get some text up on screen quickly. To do this, you need to
|
|
155
155
|
|
156
156
|
### Starting up the Web Server
|
157
157
|
|
158
|
-
You actually have a functional Rails application already. To see it, you need to start a web server on your development machine. You can do this by running:
|
158
|
+
You actually have a functional Rails application already. To see it, you need to start a web server on your development machine. You can do this by running the following in the root directory of your rails application:
|
159
159
|
|
160
160
|
```bash
|
161
161
|
$ rails server
|
@@ -233,13 +233,13 @@ Blog::Application.routes.draw do
|
|
233
233
|
# root to: "welcome#index"
|
234
234
|
```
|
235
235
|
|
236
|
-
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
|
236
|
+
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` and uncomment it. It should look something like the following:
|
237
237
|
|
238
238
|
```ruby
|
239
|
-
root
|
239
|
+
root "welcome#index"
|
240
240
|
```
|
241
241
|
|
242
|
-
The `root
|
242
|
+
The `root "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`).
|
243
243
|
|
244
244
|
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.
|
245
245
|
|
@@ -264,7 +264,7 @@ Blog::Application.routes.draw do
|
|
264
264
|
end
|
265
265
|
```
|
266
266
|
|
267
|
-
If you run `rake routes`, you'll see that all the routes for the
|
267
|
+
If you run `rake routes`, you'll see that all the routes for the
|
268
268
|
standard RESTful actions.
|
269
269
|
|
270
270
|
```bash
|
@@ -307,6 +307,10 @@ end
|
|
307
307
|
|
308
308
|
A controller is simply a class that is defined to inherit from `ApplicationController`. It's inside this class that you'll define methods that will become the actions for this controller. These actions will perform CRUD operations on the posts within our system.
|
309
309
|
|
310
|
+
NOTE: There are `public`, `private` and `protected` methods in `Ruby`
|
311
|
+
(for more details you can check on [Programming Ruby](http://www.ruby-doc.org/docs/ProgrammingRuby/)).
|
312
|
+
But only `public` methods can be actions for controllers.
|
313
|
+
|
310
314
|
If you refresh <http://localhost:3000/posts/new> now, you'll get a new error:
|
311
315
|
|
312
316
|
![Unknown action new for PostsController!](images/getting_started/unknown_action_new_for_posts.png)
|
@@ -531,43 +535,70 @@ and change the `create` action to look like this:
|
|
531
535
|
|
532
536
|
```ruby
|
533
537
|
def create
|
534
|
-
@post = Post.new(
|
535
|
-
|
538
|
+
@post = Post.new(params[:post])
|
536
539
|
@post.save
|
537
540
|
redirect_to @post
|
538
541
|
end
|
539
|
-
|
540
|
-
private
|
541
|
-
def post_params
|
542
|
-
params.require(:post).permit(:title, :text)
|
543
|
-
end
|
544
542
|
```
|
545
543
|
|
546
544
|
Here's what's going on: every Rails model can be initialized with its
|
547
545
|
respective attributes, which are automatically mapped to the respective
|
548
546
|
database columns. In the first line we do just that (remember that
|
549
|
-
`
|
547
|
+
`params[:post]` contains the attributes we're interested in). Then,
|
550
548
|
`@post.save` is responsible for saving the model in the database.
|
551
549
|
Finally, we redirect the user to the `show` action,
|
552
550
|
which we'll define later.
|
553
551
|
|
554
|
-
TIP: Note that `def post_params` is private. This new approach prevents an attacker from setting the model's attributes by manipulating the hash passed to the model. For more information, refer to [this blog post about Strong Parameters](http://weblog.rubyonrails.org/2012/3/21/strong-parameters/).
|
555
|
-
|
556
552
|
TIP: As we'll see later, `@post.save` returns a boolean indicating
|
557
553
|
whether the model was saved or not.
|
558
554
|
|
555
|
+
If you now go to
|
556
|
+
<http://localhost:3000/posts/new> you'll *almost* be able to create a post. Try
|
557
|
+
it! You should get an error that looks like this:
|
558
|
+
|
559
|
+
![Forbidden attributes for new post](images/getting_started/forbidden_attributes_for_new_post.png)
|
560
|
+
|
561
|
+
Rails has several security features that help you write secure applications,
|
562
|
+
and you're running into one of them now. This one is called
|
563
|
+
`strong_parameters`, which requires us to tell Rails exactly which parameters
|
564
|
+
we want to accept in our controllers. In this case, we want to allow the
|
565
|
+
`title` and `text` parameters, so change your `create` controller action to
|
566
|
+
look like this:
|
567
|
+
|
568
|
+
```ruby
|
569
|
+
def create
|
570
|
+
@post = Post.new(post_params)
|
571
|
+
|
572
|
+
@post.save
|
573
|
+
redirect_to @post
|
574
|
+
end
|
575
|
+
|
576
|
+
private
|
577
|
+
def post_params
|
578
|
+
params.require(:post).permit(:title, :text)
|
579
|
+
end
|
580
|
+
```
|
581
|
+
|
582
|
+
See the `permit`? It allows us to accept both `title` and `text` in this
|
583
|
+
action.
|
584
|
+
|
585
|
+
TIP: Note that `def post_params` is private. This new approach prevents an attacker from
|
586
|
+
setting the model's attributes by manipulating the hash passed to the model.
|
587
|
+
For more information, refer to
|
588
|
+
[this blog post about Strong Parameters](http://weblog.rubyonrails.org/2012/3/21/strong-parameters/).
|
589
|
+
|
559
590
|
### Showing Posts
|
560
591
|
|
561
592
|
If you submit the form again now, Rails will complain about not finding
|
562
593
|
the `show` action. That's not very useful though, so let's add the
|
563
|
-
`show` action before proceeding.
|
594
|
+
`show` action before proceeding.
|
564
595
|
|
565
596
|
```ruby
|
566
597
|
post GET /posts/:id(.:format) posts#show
|
567
598
|
```
|
568
599
|
|
569
600
|
The special syntax `:id` tells rails that this route expects an `:id`
|
570
|
-
parameter, which in our case will be the id of the post.
|
601
|
+
parameter, which in our case will be the id of the post.
|
571
602
|
|
572
603
|
As we did before, we need to add the `show` action in
|
573
604
|
`app/controllers/posts_controller.rb` and its respective view.
|
@@ -598,37 +629,14 @@ content:
|
|
598
629
|
</p>
|
599
630
|
```
|
600
631
|
|
601
|
-
|
602
|
-
<http://localhost:3000/posts/new> you'll *almost* be able to create a post. Try
|
603
|
-
it! You should get an error that looks like this:
|
604
|
-
|
605
|
-
![Forbidden attributes for new post](images/getting_started/forbidden_attributes_for_new_post.png)
|
606
|
-
|
607
|
-
Rails has several security features that help you write secure applications,
|
608
|
-
and you're running into one of them now. This one is called
|
609
|
-
'strong_parameters,' which requires us to tell Rails exactly which parameters
|
610
|
-
we want to accept in our controllers. In this case, we want to allow the
|
611
|
-
'title' and 'text' parameters, so change your `create` controller action to
|
612
|
-
look like this:
|
613
|
-
|
614
|
-
```
|
615
|
-
def create
|
616
|
-
@post = Post.new(params[:post].permit(:title, :text))
|
617
|
-
|
618
|
-
@post.save
|
619
|
-
redirect_to @post
|
620
|
-
end
|
621
|
-
```
|
622
|
-
|
623
|
-
See the `permit`? It allows us to accept both `title` and `text` in this
|
624
|
-
action. With this change, you should finally be able to create new `Post`s.
|
632
|
+
With this change, you should finally be able to create new posts.
|
625
633
|
Visit <http://localhost:3000/posts/new> and give it a try!
|
626
634
|
|
627
635
|
![Show action for posts](images/getting_started/show_action_for_posts.png)
|
628
636
|
|
629
637
|
### Listing all posts
|
630
638
|
|
631
|
-
We still need a way to list all our posts, so let's do that.
|
639
|
+
We still need a way to list all our posts, so let's do that.
|
632
640
|
We'll use a specific route from `config/routes.rb`:
|
633
641
|
|
634
642
|
```ruby
|
@@ -770,7 +778,7 @@ def create
|
|
770
778
|
@post = Post.new(params[:post].permit(:title, :text))
|
771
779
|
|
772
780
|
if @post.save
|
773
|
-
redirect_to @post
|
781
|
+
redirect_to @post
|
774
782
|
else
|
775
783
|
render 'new'
|
776
784
|
end
|
@@ -793,7 +801,7 @@ something went wrong. To do that, you'll modify
|
|
793
801
|
```html+erb
|
794
802
|
<%= form_for :post, url: posts_path do |f| %>
|
795
803
|
<% if @post.errors.any? %>
|
796
|
-
<div id="
|
804
|
+
<div id="error_explanation">
|
797
805
|
<h2><%= pluralize(@post.errors.count, "error") %> prohibited
|
798
806
|
this post from being saved:</h2>
|
799
807
|
<ul>
|
@@ -860,10 +868,9 @@ it look as follows:
|
|
860
868
|
```html+erb
|
861
869
|
<h1>Editing post</h1>
|
862
870
|
|
863
|
-
<%= form_for :post, url: post_path(@post
|
864
|
-
method: :patch do |f| %>
|
871
|
+
<%= form_for :post, url: post_path(@post), method: :patch do |f| %>
|
865
872
|
<% if @post.errors.any? %>
|
866
|
-
<div id="
|
873
|
+
<div id="error_explanation">
|
867
874
|
<h2><%= pluralize(@post.errors.count, "error") %> prohibited
|
868
875
|
this post from being saved:</h2>
|
869
876
|
<ul>
|
@@ -941,7 +948,7 @@ appear next to the "Show" link:
|
|
941
948
|
<tr>
|
942
949
|
<td><%= post.title %></td>
|
943
950
|
<td><%= post.text %></td>
|
944
|
-
<td><%= link_to 'Show',
|
951
|
+
<td><%= link_to 'Show', post %></td>
|
945
952
|
<td><%= link_to 'Edit', edit_post_path(post) %></td>
|
946
953
|
</tr>
|
947
954
|
<% end %>
|
@@ -979,7 +986,7 @@ content:
|
|
979
986
|
```html+erb
|
980
987
|
<%= form_for @post do |f| %>
|
981
988
|
<% if @post.errors.any? %>
|
982
|
-
<div id="
|
989
|
+
<div id="error_explanation">
|
983
990
|
<h2><%= pluralize(@post.errors.count, "error") %> prohibited
|
984
991
|
this post from being saved:</h2>
|
985
992
|
<ul>
|
@@ -1083,7 +1090,7 @@ together.
|
|
1083
1090
|
<tr>
|
1084
1091
|
<td><%= post.title %></td>
|
1085
1092
|
<td><%= post.text %></td>
|
1086
|
-
<td><%= link_to 'Show', post_path %></td>
|
1093
|
+
<td><%= link_to 'Show', post_path(post) %></td>
|
1087
1094
|
<td><%= link_to 'Edit', edit_post_path(post) %></td>
|
1088
1095
|
<td><%= link_to 'Destroy', post_path(post),
|
1089
1096
|
method: :delete, data: { confirm: 'Are you sure?' } %></td>
|
@@ -1092,7 +1099,7 @@ together.
|
|
1092
1099
|
</table>
|
1093
1100
|
```
|
1094
1101
|
|
1095
|
-
Here we're using `link_to` in a different way. We pass the named route as the first argument,
|
1102
|
+
Here we're using `link_to` in a different way. We pass the named route as the first argument,
|
1096
1103
|
and then the final two keys as another argument. The `:method` and `:'data-confirm'`
|
1097
1104
|
options are used as HTML5 attributes so that when the link is clicked,
|
1098
1105
|
Rails will first show a confirm dialog to the user, and then submit the link with method `delete`.
|
@@ -1103,7 +1110,7 @@ generated the application. Without this file, the confirmation dialog box wouldn
|
|
1103
1110
|
![Confirm Dialog](images/getting_started/confirm_dialog.png)
|
1104
1111
|
|
1105
1112
|
Congratulations, you can now create, show, list, update and destroy
|
1106
|
-
posts.
|
1113
|
+
posts.
|
1107
1114
|
|
1108
1115
|
TIP: In general, Rails encourages the use of resources objects in place
|
1109
1116
|
of declaring routes manually.
|
@@ -1297,8 +1304,8 @@ So first, we'll wire up the Post show template
|
|
1297
1304
|
</p>
|
1298
1305
|
<% end %>
|
1299
1306
|
|
1300
|
-
<%= link_to '
|
1301
|
-
<%= link_to '
|
1307
|
+
<%= link_to 'Back', posts_path %>
|
1308
|
+
| <%= link_to 'Edit', edit_post_path(@post) %>
|
1302
1309
|
```
|
1303
1310
|
|
1304
1311
|
This adds a form on the `Post` show page that creates a new comment by
|
data/guides/source/i18n.md
CHANGED
@@ -92,7 +92,7 @@ en:
|
|
92
92
|
hello: "Hello world"
|
93
93
|
```
|
94
94
|
|
95
|
-
This means, that in the `:en` locale, the key _hello_ will map to the _Hello world_ string. Every string inside Rails is internationalized in this way, see for instance Active
|
95
|
+
This means, that in the `:en` locale, the key _hello_ will map to the _Hello world_ string. Every string inside Rails is internationalized in this way, see for instance Active Model validation messages in the [`activemodel/lib/active_model/locale/en.yml`](https://github.com/rails/rails/blob/master/activemodel/lib/active_model/locale/en.yml) file or time and date formats in the [`activesupport/lib/active_support/locale/en.yml`](https://github.com/rails/rails/blob/master/activesupport/lib/active_support/locale/en.yml) file. You can use YAML or standard Ruby Hashes to store translations in the default (Simple) backend.
|
96
96
|
|
97
97
|
The I18n library will use **English** as a **default locale**, i.e. if you don't set a different locale, `:en` will be used for looking up translations.
|
98
98
|
|
@@ -132,7 +132,7 @@ If you want to translate your Rails application to a **single language other tha
|
|
132
132
|
|
133
133
|
However, you would probably like to **provide support for more locales** in your application. In such case, you need to set and pass the locale between requests.
|
134
134
|
|
135
|
-
WARNING: You may be tempted to store the chosen locale in a _session_ or a <em>cookie</em>, however **do not do this**. The locale should be transparent and a part of the URL. This way you won't break people's basic assumptions about the web itself: if you send a URL to a friend, they should see the same page and content as you. A fancy word for this would be that you're being [<em>RESTful</em>](http://en.wikipedia.org/wiki/Representational_State_Transfer. Read more about the RESTful approach in [Stefan Tilkov's articles](http://www.infoq.com/articles/rest-introduction). Sometimes there are exceptions to this rule and those are discussed below.
|
135
|
+
WARNING: You may be tempted to store the chosen locale in a _session_ or a <em>cookie</em>, however **do not do this**. The locale should be transparent and a part of the URL. This way you won't break people's basic assumptions about the web itself: if you send a URL to a friend, they should see the same page and content as you. A fancy word for this would be that you're being [<em>RESTful</em>](http://en.wikipedia.org/wiki/Representational_State_Transfer). Read more about the RESTful approach in [Stefan Tilkov's articles](http://www.infoq.com/articles/rest-introduction). Sometimes there are exceptions to this rule and those are discussed below.
|
136
136
|
|
137
137
|
The _setting part_ is easy. You can set the locale in a `before_action` in the `ApplicationController` like this:
|
138
138
|
|
@@ -253,7 +253,7 @@ You would probably need to map URLs like these:
|
|
253
253
|
|
254
254
|
```ruby
|
255
255
|
# config/routes.rb
|
256
|
-
|
256
|
+
get '/:locale' => 'dashboard#index'
|
257
257
|
```
|
258
258
|
|
259
259
|
Do take special care about the **order of your routes**, so this route declaration does not "eat" other ones. (You may want to add it directly before the `root :to` declaration.)
|
@@ -417,7 +417,7 @@ So that would give you:
|
|
417
417
|
|
418
418
|
![rails i18n demo localized time to pirate](images/i18n/demo_localized_pirate.png)
|
419
419
|
|
420
|
-
TIP: Right now you might need to add some more date/time formats in order to make the I18n backend work as expected (at least for the 'pirate' locale). Of course, there's a great chance that somebody already did all the work by **translating Rails' defaults for your locale**. See the [rails-i18n repository at
|
420
|
+
TIP: Right now you might need to add some more date/time formats in order to make the I18n backend work as expected (at least for the 'pirate' locale). Of course, there's a great chance that somebody already did all the work by **translating Rails' defaults for your locale**. See the [rails-i18n repository at GitHub](https://github.com/svenfuchs/rails-i18n/tree/master/rails/locale) for an archive of various locale files. When you put such file(s) in `config/locales/` directory, they will automatically be ready for use.
|
421
421
|
|
422
422
|
### Inflection Rules For Other Locales
|
423
423
|
|
@@ -813,6 +813,7 @@ So, for example, instead of the default error message `"can not be blank"` you c
|
|
813
813
|
| numericality | :equal_to | :equal_to | count |
|
814
814
|
| numericality | :less_than | :less_than | count |
|
815
815
|
| numericality | :less_than_or_equal_to | :less_than_or_equal_to | count |
|
816
|
+
| numericality | :only_integer | :not_an_integer | - |
|
816
817
|
| numericality | :odd | :odd | - |
|
817
818
|
| numericality | :even | :even | - |
|
818
819
|
|
@@ -980,8 +981,8 @@ Resources
|
|
980
981
|
|
981
982
|
* [rails-i18n.org](http://rails-i18n.org) - Homepage of the rails-i18n project. You can find lots of useful resources on the [wiki](http://rails-i18n.org/wiki).
|
982
983
|
* [Google group: rails-i18n](http://groups.google.com/group/rails-i18n) - The project's mailing list.
|
983
|
-
* [
|
984
|
-
* [
|
984
|
+
* [GitHub: rails-i18n](https://github.com/svenfuchs/rails-i18n/tree/master) - Code repository for the rails-i18n project. Most importantly you can find lots of [example translations](https://github.com/svenfuchs/rails-i18n/tree/master/rails/locale) for Rails that should work for your application in most cases.
|
985
|
+
* [GitHub: i18n](https://github.com/svenfuchs/i18n/tree/master) - Code repository for the i18n gem.
|
985
986
|
* [Lighthouse: rails-i18n](http://i18n.lighthouseapp.com/projects/14948-rails-i18n/overview) - Issue tracker for the rails-i18n project.
|
986
987
|
* [Lighthouse: i18n](http://i18n.lighthouseapp.com/projects/14947-ruby-i18n/overview) - Issue tracker for the i18n gem.
|
987
988
|
|
@@ -101,17 +101,15 @@
|
|
101
101
|
You're encouraged to help improve the quality of this guide.
|
102
102
|
</p>
|
103
103
|
<p>
|
104
|
-
|
105
|
-
|
106
|
-
repository and open a new pull request. You can also ask for commit rights on
|
107
|
-
<%= link_to 'docrails', 'https://github.com/rails/docrails' %> if you plan to submit
|
108
|
-
several patches. Commits are reviewed, but that happens after you've submitted your
|
109
|
-
contribution. This repository is cross-merged with master periodically.
|
104
|
+
Please contribute if you see any typos or factual errors.
|
105
|
+
To get started, you can read our <%= link_to 'documentation contributions', 'http://edgeguides.rubyonrails.org/contributing_to_ruby_on_rails.html#contributing-to-the-rails-documentation' %> section.
|
110
106
|
</p>
|
111
107
|
<p>
|
112
108
|
You may also find incomplete content, or stuff that is not up to date.
|
113
|
-
Please do add any missing documentation for master.
|
114
|
-
<%= link_to '
|
109
|
+
Please do add any missing documentation for master. Make sure to check
|
110
|
+
<%= link_to 'Edge Guides','http://edgeguides.rubyonrails.org' %> first to verify
|
111
|
+
if the issues are already fixed or not on the master branch.
|
112
|
+
Check the <%= link_to 'Ruby on Rails Guides Guidelines', 'ruby_on_rails_guides_guidelines.html' %>
|
115
113
|
for style and conventions.
|
116
114
|
</p>
|
117
115
|
<p>
|
@@ -592,7 +592,7 @@ def index
|
|
592
592
|
end
|
593
593
|
|
594
594
|
def show
|
595
|
-
@book = Book.
|
595
|
+
@book = Book.find_by(id: params[:id])
|
596
596
|
if @book.nil?
|
597
597
|
render action: "index"
|
598
598
|
end
|
@@ -607,7 +607,7 @@ def index
|
|
607
607
|
end
|
608
608
|
|
609
609
|
def show
|
610
|
-
@book = Book.
|
610
|
+
@book = Book.find_by(id: params[:id])
|
611
611
|
if @book.nil?
|
612
612
|
redirect_to action: :index
|
613
613
|
end
|
@@ -626,10 +626,10 @@ def index
|
|
626
626
|
end
|
627
627
|
|
628
628
|
def show
|
629
|
-
@book = Book.
|
629
|
+
@book = Book.find_by(id: params[:id])
|
630
630
|
if @book.nil?
|
631
631
|
@books = Book.all
|
632
|
-
flash[:alert] = "Your book was not found"
|
632
|
+
flash.now[:alert] = "Your book was not found"
|
633
633
|
render "index"
|
634
634
|
end
|
635
635
|
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
Maintenance Policy for Ruby on Rails
|
2
|
+
====================================
|
3
|
+
|
4
|
+
Support of the Rails framework is divided into four groups: New features, bug
|
5
|
+
fixes, security issues, and severe security issues. They are handled as
|
6
|
+
follows, all versions in x.y.z format
|
7
|
+
|
8
|
+
--------------------------------------------------------------------------------
|
9
|
+
|
10
|
+
New Features
|
11
|
+
------------
|
12
|
+
|
13
|
+
New features are only added to the master branch and will not be made available
|
14
|
+
in point releases.
|
15
|
+
|
16
|
+
Bug Fixes
|
17
|
+
---------
|
18
|
+
|
19
|
+
Only the latest release series will receive bug fixes. When enough bugs are
|
20
|
+
fixed and its deemed worthy to release a new gem, this is the branch it happens
|
21
|
+
from.
|
22
|
+
|
23
|
+
**Currently included series:** 4.0.z
|
24
|
+
|
25
|
+
Security Issues
|
26
|
+
---------------
|
27
|
+
|
28
|
+
The current release series and the next most recent one will receive patches
|
29
|
+
and new versions in case of a security issue.
|
30
|
+
|
31
|
+
These releases are created by taking the last released version, applying the
|
32
|
+
security patches, and releasing. Those patches are then applied to the end of
|
33
|
+
the x-y-stable branch. For example, a theoretical 1.2.3 security release would
|
34
|
+
be built from 1.2.2, and then added to the end of 1-2-stable. This means that
|
35
|
+
security releases are easy to upgrade to if you're running the latest version
|
36
|
+
of Rails.
|
37
|
+
|
38
|
+
**Currently included series:** 4.0.z, 3.2.z
|
39
|
+
|
40
|
+
Severe Security Issues
|
41
|
+
----------------------
|
42
|
+
|
43
|
+
For severe security issues we will provide new versions as above, and also the
|
44
|
+
last major release series will receive patches and new versions. The
|
45
|
+
classification of the security issue is judged by the core team.
|
46
|
+
|
47
|
+
**Currently included series:** 4.0.z, 3.2.z
|
48
|
+
|
49
|
+
Unsupported Release Series
|
50
|
+
--------------------------
|
51
|
+
|
52
|
+
When a release series is no longer supported, it's your own responsibility to
|
53
|
+
deal with bugs and security issues. We may provide backports of the fixes and
|
54
|
+
publish them to git, however there will be no new versions released. If you are
|
55
|
+
not comfortable maintaining your own versions, you should upgrade to a
|
56
|
+
supported version.
|
data/guides/source/migrations.md
CHANGED
@@ -301,6 +301,7 @@ braces. You can use the following modifiers:
|
|
301
301
|
* `precision` Defines the precision for the `decimal` fields
|
302
302
|
* `scale` Defines the scale for the `decimal` fields
|
303
303
|
* `polymorphic` Adds a `type` column for `belongs_to` associations
|
304
|
+
* `null` Allows or disallows `NULL` values in the column.
|
304
305
|
|
305
306
|
For instance, running
|
306
307
|
|
@@ -313,8 +314,8 @@ will produce a migration that looks like this
|
|
313
314
|
```ruby
|
314
315
|
class AddDetailsToProducts < ActiveRecord::Migration
|
315
316
|
def change
|
316
|
-
add_column :products, :price, precision: 5, scale: 2
|
317
|
-
add_reference :products, :
|
317
|
+
add_column :products, :price, :decimal, precision: 5, scale: 2
|
318
|
+
add_reference :products, :supplier, polymorphic: true, index: true
|
318
319
|
end
|
319
320
|
end
|
320
321
|
```
|
@@ -368,7 +369,7 @@ which creates a `categories_products` table with two columns called
|
|
368
369
|
`category_id` and `product_id`. These columns have the option `:null` set to
|
369
370
|
`false` by default.
|
370
371
|
|
371
|
-
You can pass the option `:table_name`
|
372
|
+
You can pass the option `:table_name` when you want to customize the table
|
372
373
|
name. For example,
|
373
374
|
|
374
375
|
```ruby
|
@@ -831,8 +832,7 @@ which contains a `Product` model:
|
|
831
832
|
Bob goes on vacation.
|
832
833
|
|
833
834
|
Alice creates a migration for the `products` table which adds a new column and
|
834
|
-
initializes it
|
835
|
-
column.
|
835
|
+
initializes it:
|
836
836
|
|
837
837
|
```ruby
|
838
838
|
# db/migrate/20100513121110_add_flag_to_product.rb
|
@@ -843,11 +843,12 @@ class AddFlagToProduct < ActiveRecord::Migration
|
|
843
843
|
reversible do |dir|
|
844
844
|
dir.up { Product.update_all flag: false }
|
845
845
|
end
|
846
|
-
Product.update_all flag: false
|
847
846
|
end
|
848
847
|
end
|
849
848
|
```
|
850
849
|
|
850
|
+
She also adds a validation to the `Product` model for the new column:
|
851
|
+
|
851
852
|
```ruby
|
852
853
|
# app/models/product.rb
|
853
854
|
|
@@ -856,9 +857,8 @@ class Product < ActiveRecord::Base
|
|
856
857
|
end
|
857
858
|
```
|
858
859
|
|
859
|
-
Alice adds a second migration which adds
|
860
|
-
|
861
|
-
column.
|
860
|
+
Alice adds a second migration which adds another column to the `products`
|
861
|
+
table and initializes it:
|
862
862
|
|
863
863
|
```ruby
|
864
864
|
# db/migrate/20100515121110_add_fuzz_to_product.rb
|
@@ -873,6 +873,8 @@ class AddFuzzToProduct < ActiveRecord::Migration
|
|
873
873
|
end
|
874
874
|
```
|
875
875
|
|
876
|
+
She also adds a validation to the `Product` model for the new column:
|
877
|
+
|
876
878
|
```ruby
|
877
879
|
# app/models/product.rb
|
878
880
|
|
@@ -905,7 +907,7 @@ A fix for this is to create a local model within the migration. This keeps
|
|
905
907
|
Rails from running the validations, so that the migrations run to completion.
|
906
908
|
|
907
909
|
When using a local model, it's a good idea to call
|
908
|
-
`Product.reset_column_information` to refresh the
|
910
|
+
`Product.reset_column_information` to refresh the Active Record cache for the
|
909
911
|
`Product` model prior to updating data in the database.
|
910
912
|
|
911
913
|
If Alice had done this instead, there would have been no problem:
|
@@ -958,7 +960,7 @@ other product attributes.
|
|
958
960
|
These migrations run just fine, but when Bob comes back from his vacation
|
959
961
|
and calls `rake db:migrate` to run all the outstanding migrations, he gets a
|
960
962
|
subtle bug: The descriptions have defaults, and the `fuzz` column is present,
|
961
|
-
but `fuzz` is nil on all products.
|
963
|
+
but `fuzz` is `nil` on all products.
|
962
964
|
|
963
965
|
The solution is again to use `Product.reset_column_information` before
|
964
966
|
referencing the Product model in a migration, ensuring the Active Record's
|