rails 4.2.1 → 4.2.11.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/guides/CHANGELOG.md +76 -1
- data/guides/Rakefile +16 -3
- data/guides/bug_report_templates/generic_gem.rb +15 -0
- data/guides/bug_report_templates/generic_master.rb +26 -0
- data/guides/rails_guides/levenshtein.rb +0 -2
- data/guides/source/4_2_release_notes.md +24 -0
- data/guides/source/_welcome.html.erb +5 -1
- data/guides/source/action_mailer_basics.md +4 -1
- data/guides/source/action_view_overview.md +2 -61
- data/guides/source/active_job_basics.md +27 -6
- data/guides/source/active_record_basics.md +6 -6
- data/guides/source/active_record_querying.md +1 -3
- data/guides/source/active_record_validations.md +3 -2
- data/guides/source/active_support_core_extensions.md +2 -50
- data/guides/source/active_support_instrumentation.md +0 -11
- data/guides/source/api_documentation_guidelines.md +1 -1
- data/guides/source/asset_pipeline.md +5 -40
- data/guides/source/association_basics.md +14 -4
- data/guides/source/autoloading_and_reloading_constants.md +2 -1
- data/guides/source/configuring.md +29 -4
- data/guides/source/contributing_to_ruby_on_rails.md +3 -3
- data/guides/source/engines.md +10 -10
- data/guides/source/getting_started.md +19 -24
- data/guides/source/i18n.md +1 -1
- data/guides/source/initialization.md +1 -1
- data/guides/source/layout.html.erb +4 -7
- data/guides/source/layouts_and_rendering.md +8 -9
- data/guides/source/rails_on_rack.md +0 -1
- data/guides/source/routing.md +15 -1
- data/guides/source/security.md +1 -1
- data/guides/source/testing.md +1 -1
- data/guides/source/upgrading_ruby_on_rails.md +17 -1
- data/guides/source/working_with_javascript_in_rails.md +1 -1
- metadata +21 -20
@@ -332,8 +332,6 @@ User.find_each(start: 2000, batch_size: 5000) do |user|
|
|
332
332
|
end
|
333
333
|
```
|
334
334
|
|
335
|
-
Another example would be if you wanted multiple workers handling the same processing queue. You could have each worker handle 10000 records by setting the appropriate `:start` option on each worker.
|
336
|
-
|
337
335
|
#### `find_in_batches`
|
338
336
|
|
339
337
|
The `find_in_batches` method is similar to `find_each`, since both retrieve batches of records. The difference is that `find_in_batches` yields _batches_ to the block as an array of models, instead of individually. The following example will yield to the supplied block an array of up to 1000 invoices at a time, with the final block containing any remaining invoices:
|
@@ -876,7 +874,7 @@ For example:
|
|
876
874
|
Item.transaction do
|
877
875
|
i = Item.lock.first
|
878
876
|
i.name = 'Jones'
|
879
|
-
i.save
|
877
|
+
i.save!
|
880
878
|
end
|
881
879
|
```
|
882
880
|
|
@@ -944,8 +944,9 @@ own custom validators.
|
|
944
944
|
|
945
945
|
You can also create methods that verify the state of your models and add
|
946
946
|
messages to the `errors` collection when they are invalid. You must then
|
947
|
-
register these methods by using the `validate`
|
948
|
-
|
947
|
+
register these methods by using the `validate`
|
948
|
+
([API](http://api.rubyonrails.org/classes/ActiveModel/Validations/ClassMethods.html#method-i-validate))
|
949
|
+
class method, passing in the symbols for the validation methods' names.
|
949
950
|
|
950
951
|
You can pass more than one symbol for each class method and the respective
|
951
952
|
validations will be run in the same order as they were registered.
|
@@ -1760,7 +1760,7 @@ NOTE: Defined in `active_support/core_ext/string/inflections.rb`.
|
|
1760
1760
|
The method `constantize` resolves the constant reference expression in its receiver:
|
1761
1761
|
|
1762
1762
|
```ruby
|
1763
|
-
"
|
1763
|
+
"Integer".constantize # => Integer
|
1764
1764
|
|
1765
1765
|
module M
|
1766
1766
|
X = 1
|
@@ -2612,8 +2612,7 @@ To do so, the method loops over the pairs and builds nodes that depend on the _v
|
|
2612
2612
|
```ruby
|
2613
2613
|
XML_TYPE_NAMES = {
|
2614
2614
|
"Symbol" => "symbol",
|
2615
|
-
"
|
2616
|
-
"Bignum" => "integer",
|
2615
|
+
"Integer" => "integer",
|
2617
2616
|
"BigDecimal" => "decimal",
|
2618
2617
|
"Float" => "float",
|
2619
2618
|
"TrueClass" => "boolean",
|
@@ -3043,53 +3042,6 @@ The method `Range#overlaps?` says whether any two given ranges have non-void int
|
|
3043
3042
|
|
3044
3043
|
NOTE: Defined in `active_support/core_ext/range/overlaps.rb`.
|
3045
3044
|
|
3046
|
-
Extensions to `Proc`
|
3047
|
-
--------------------
|
3048
|
-
|
3049
|
-
### `bind`
|
3050
|
-
|
3051
|
-
As you surely know Ruby has an `UnboundMethod` class whose instances are methods that belong to the limbo of methods without a self. The method `Module#instance_method` returns an unbound method for example:
|
3052
|
-
|
3053
|
-
```ruby
|
3054
|
-
Hash.instance_method(:delete) # => #<UnboundMethod: Hash#delete>
|
3055
|
-
```
|
3056
|
-
|
3057
|
-
An unbound method is not callable as is, you need to bind it first to an object with `bind`:
|
3058
|
-
|
3059
|
-
```ruby
|
3060
|
-
clear = Hash.instance_method(:clear)
|
3061
|
-
clear.bind({a: 1}).call # => {}
|
3062
|
-
```
|
3063
|
-
|
3064
|
-
Active Support defines `Proc#bind` with an analogous purpose:
|
3065
|
-
|
3066
|
-
```ruby
|
3067
|
-
Proc.new { size }.bind([]).call # => 0
|
3068
|
-
```
|
3069
|
-
|
3070
|
-
As you see that's callable and bound to the argument, the return value is indeed a `Method`.
|
3071
|
-
|
3072
|
-
NOTE: To do so `Proc#bind` actually creates a method under the hood. If you ever see a method with a weird name like `__bind_1256598120_237302` in a stack trace you know now where it comes from.
|
3073
|
-
|
3074
|
-
Action Pack uses this trick in `rescue_from` for example, which accepts the name of a method and also a proc as callbacks for a given rescued exception. It has to call them in either case, so a bound method is returned by `handler_for_rescue`, thus simplifying the code in the caller:
|
3075
|
-
|
3076
|
-
```ruby
|
3077
|
-
def handler_for_rescue(exception)
|
3078
|
-
_, rescuer = Array(rescue_handlers).reverse.detect do |klass_name, handler|
|
3079
|
-
...
|
3080
|
-
end
|
3081
|
-
|
3082
|
-
case rescuer
|
3083
|
-
when Symbol
|
3084
|
-
method(rescuer)
|
3085
|
-
when Proc
|
3086
|
-
rescuer.bind(self)
|
3087
|
-
end
|
3088
|
-
end
|
3089
|
-
```
|
3090
|
-
|
3091
|
-
NOTE: Defined in `active_support/core_ext/proc.rb`.
|
3092
|
-
|
3093
3045
|
Extensions to `Date`
|
3094
3046
|
--------------------
|
3095
3047
|
|
@@ -305,17 +305,6 @@ Action Mailer
|
|
305
305
|
}
|
306
306
|
```
|
307
307
|
|
308
|
-
ActiveResource
|
309
|
-
--------------
|
310
|
-
|
311
|
-
### request.active_resource
|
312
|
-
|
313
|
-
| Key | Value |
|
314
|
-
| -------------- | -------------------- |
|
315
|
-
| `:method` | HTTP method |
|
316
|
-
| `:request_uri` | Complete URI |
|
317
|
-
| `:result` | HTTP response object |
|
318
|
-
|
319
308
|
Active Support
|
320
309
|
--------------
|
321
310
|
|
@@ -111,7 +111,7 @@ On the other hand, big chunks of structured documentation may have a separate "E
|
|
111
111
|
The results of expressions follow them and are introduced by "# => ", vertically aligned:
|
112
112
|
|
113
113
|
```ruby
|
114
|
-
# For checking if
|
114
|
+
# For checking if an integer is even or odd.
|
115
115
|
#
|
116
116
|
# 1.even? # => false
|
117
117
|
# 1.odd? # => true
|
@@ -434,11 +434,11 @@ Sprockets uses manifest files to determine which assets to include and serve.
|
|
434
434
|
These manifest files contain _directives_ - instructions that tell Sprockets
|
435
435
|
which files to require in order to build a single CSS or JavaScript file. With
|
436
436
|
these directives, Sprockets loads the files specified, processes them if
|
437
|
-
necessary, concatenates them into one single file and then compresses them
|
438
|
-
`Rails.application.config.assets.
|
439
|
-
than many, the load time of pages can be greatly reduced because
|
440
|
-
makes fewer requests. Compression also reduces file size, enabling
|
441
|
-
browser to download them faster.
|
437
|
+
necessary, concatenates them into one single file and then compresses them
|
438
|
+
(based on value of `Rails.application.config.assets.js_compressor`). By serving
|
439
|
+
one file rather than many, the load time of pages can be greatly reduced because
|
440
|
+
the browser makes fewer requests. Compression also reduces file size, enabling
|
441
|
+
the browser to download them faster.
|
442
442
|
|
443
443
|
|
444
444
|
For example, a new Rails 4 application includes a default
|
@@ -789,41 +789,6 @@ location ~ ^/assets/ {
|
|
789
789
|
}
|
790
790
|
```
|
791
791
|
|
792
|
-
#### GZip Compression
|
793
|
-
|
794
|
-
When files are precompiled, Sprockets also creates a
|
795
|
-
[gzipped](http://en.wikipedia.org/wiki/Gzip) (.gz) version of your assets. Web
|
796
|
-
servers are typically configured to use a moderate compression ratio as a
|
797
|
-
compromise, but since precompilation happens once, Sprockets uses the maximum
|
798
|
-
compression ratio, thus reducing the size of the data transfer to the minimum.
|
799
|
-
On the other hand, web servers can be configured to serve compressed content
|
800
|
-
directly from disk, rather than deflating non-compressed files themselves.
|
801
|
-
|
802
|
-
NGINX is able to do this automatically enabling `gzip_static`:
|
803
|
-
|
804
|
-
```nginx
|
805
|
-
location ~ ^/(assets)/ {
|
806
|
-
root /path/to/public;
|
807
|
-
gzip_static on; # to serve pre-gzipped version
|
808
|
-
expires max;
|
809
|
-
add_header Cache-Control public;
|
810
|
-
}
|
811
|
-
```
|
812
|
-
|
813
|
-
This directive is available if the core module that provides this feature was
|
814
|
-
compiled with the web server. Ubuntu/Debian packages, even `nginx-light`, have
|
815
|
-
the module compiled. Otherwise, you may need to perform a manual compilation:
|
816
|
-
|
817
|
-
```bash
|
818
|
-
./configure --with-http_gzip_static_module
|
819
|
-
```
|
820
|
-
|
821
|
-
If you're compiling NGINX with Phusion Passenger you'll need to pass that option
|
822
|
-
when prompted.
|
823
|
-
|
824
|
-
A robust configuration for Apache is possible but tricky; please Google around.
|
825
|
-
(Or help update this Guide if you have a good configuration example for Apache.)
|
826
|
-
|
827
792
|
### Local Precompilation
|
828
793
|
|
829
794
|
There are several reasons why you might want to precompile your assets locally.
|
@@ -169,7 +169,7 @@ class CreateCustomers < ActiveRecord::Migration
|
|
169
169
|
end
|
170
170
|
|
171
171
|
create_table :orders do |t|
|
172
|
-
t.belongs_to :customer, index:true
|
172
|
+
t.belongs_to :customer, index: true
|
173
173
|
t.datetime :order_date
|
174
174
|
t.timestamps null: false
|
175
175
|
end
|
@@ -1417,7 +1417,13 @@ The `collection_singular_ids=` method makes the collection contain only the obje
|
|
1417
1417
|
|
1418
1418
|
##### `collection.clear`
|
1419
1419
|
|
1420
|
-
The `collection.clear` method removes
|
1420
|
+
The `collection.clear` method removes all objects from the collection according to the strategy specified by the `dependent` option. If no option is given, it follows the default strategy. The default strategy for `has_many :through` associations is `delete_all`, and for `has_many` associations is to set the foreign keys to `NULL`.
|
1421
|
+
|
1422
|
+
```ruby
|
1423
|
+
@customer.orders.clear
|
1424
|
+
```
|
1425
|
+
|
1426
|
+
WARNING: Objects will be delete if they're associated with `dependent: :destroy`, just like `dependent: :delete_all`.
|
1421
1427
|
|
1422
1428
|
##### `collection.empty?`
|
1423
1429
|
|
@@ -1456,7 +1462,9 @@ The `collection.where` method finds objects within the collection based on the c
|
|
1456
1462
|
|
1457
1463
|
##### `collection.exists?(...)`
|
1458
1464
|
|
1459
|
-
The `collection.exists?` method checks whether an object meeting the supplied
|
1465
|
+
The `collection.exists?` method checks whether an object meeting the supplied
|
1466
|
+
conditions exists in the collection. It uses the same syntax and options as
|
1467
|
+
[`ActiveRecord::Base.exists?`](http://api.rubyonrails.org/classes/ActiveRecord/FinderMethods.html#method-i-exists-3F).
|
1460
1468
|
|
1461
1469
|
##### `collection.build(attributes = {}, ...)`
|
1462
1470
|
|
@@ -1949,7 +1957,9 @@ The `collection.where` method finds objects within the collection based on the c
|
|
1949
1957
|
|
1950
1958
|
##### `collection.exists?(...)`
|
1951
1959
|
|
1952
|
-
The `collection.exists?` method checks whether an object meeting the supplied
|
1960
|
+
The `collection.exists?` method checks whether an object meeting the supplied
|
1961
|
+
conditions exists in the collection. It uses the same syntax and options as
|
1962
|
+
[`ActiveRecord::Base.exists?`](http://api.rubyonrails.org/classes/ActiveRecord/FinderMethods.html#method-i-exists-3F).
|
1953
1963
|
|
1954
1964
|
##### `collection.build(attributes = {})`
|
1955
1965
|
|
@@ -461,8 +461,9 @@ Also, this collection is configurable via `config.autoload_paths`. For example,
|
|
461
461
|
by adding this to `config/application.rb`:
|
462
462
|
|
463
463
|
```ruby
|
464
|
-
config.autoload_paths
|
464
|
+
config.autoload_paths << "#{Rails.root}/lib"
|
465
465
|
```
|
466
|
+
`config.autoload_paths` is accessible from environment-specific configuration files, but any changes made to it outside `config/application.rb` don't have an effect.
|
466
467
|
|
467
468
|
The value of `autoload_paths` can be inspected. In a just generated application
|
468
469
|
it is (edited):
|
@@ -33,7 +33,7 @@ In general, the work of configuring Rails means configuring the components of Ra
|
|
33
33
|
For example, the `config/application.rb` file includes this setting:
|
34
34
|
|
35
35
|
```ruby
|
36
|
-
config.
|
36
|
+
config.time_zone = 'Central Time (US & Canada)'
|
37
37
|
```
|
38
38
|
|
39
39
|
This is a setting for Rails itself. If you want to pass settings to individual Rails components, you can do so via the same `config` object in `config/application.rb`:
|
@@ -139,8 +139,6 @@ pipeline is enabled. It is set to true by default.
|
|
139
139
|
|
140
140
|
* `config.assets.raise_runtime_errors` Set this flag to `true` to enable additional runtime error checking. Recommended in `config/environments/development.rb` to minimize unexpected behavior when deploying to `production`.
|
141
141
|
|
142
|
-
* `config.assets.compress` a flag that enables the compression of compiled assets. It is explicitly set to true in `config/environments/production.rb`.
|
143
|
-
|
144
142
|
* `config.assets.css_compressor` defines the CSS compressor to use. It is set by default by `sass-rails`. The unique alternative value at the moment is `:yui`, which uses the `yui-compressor` gem.
|
145
143
|
|
146
144
|
* `config.assets.js_compressor` defines the JavaScript compressor to use. Possible values are `:closure`, `:uglifier` and `:yui` which require the use of the `closure-compiler`, `uglifier` or `yui-compressor` gems respectively.
|
@@ -1027,7 +1025,7 @@ NOTE. If you are running in a multi-threaded environment, there could be a chanc
|
|
1027
1025
|
Custom configuration
|
1028
1026
|
--------------------
|
1029
1027
|
|
1030
|
-
You can configure your own code through the Rails configuration object with custom configuration. It works like this:
|
1028
|
+
You can configure your own code through the Rails configuration object with custom configuration under the `config.x` property. It works like this:
|
1031
1029
|
|
1032
1030
|
```ruby
|
1033
1031
|
config.x.payment_processing.schedule = :daily
|
@@ -1043,3 +1041,30 @@ These configuration points are then available through the configuration object:
|
|
1043
1041
|
Rails.configuration.x.super_debugger # => true
|
1044
1042
|
Rails.configuration.x.super_debugger.not_set # => nil
|
1045
1043
|
```
|
1044
|
+
|
1045
|
+
You can also use Rails::Application.config_for to load whole configuration files:
|
1046
|
+
|
1047
|
+
```ruby
|
1048
|
+
# config/payment.yml:
|
1049
|
+
production:
|
1050
|
+
environment: production
|
1051
|
+
merchant_id: production_merchant_id
|
1052
|
+
public_key: production_public_key
|
1053
|
+
private_key: production_private_key
|
1054
|
+
development:
|
1055
|
+
environment: sandbox
|
1056
|
+
merchant_id: development_merchant_id
|
1057
|
+
public_key: development_public_key
|
1058
|
+
private_key: development_private_key
|
1059
|
+
|
1060
|
+
# config/application.rb
|
1061
|
+
module MyApp
|
1062
|
+
class Application < Rails::Application
|
1063
|
+
config.x.payment = config_for(:payment)
|
1064
|
+
end
|
1065
|
+
end
|
1066
|
+
```
|
1067
|
+
|
1068
|
+
```ruby
|
1069
|
+
Rails.configuration.x.payment['merchant_id'] # => production_merchant_id or development_merchant_id
|
1070
|
+
```
|
@@ -119,11 +119,11 @@ Contributing to the Rails Documentation
|
|
119
119
|
Ruby on Rails has two main sets of documentation: the guides, which help you
|
120
120
|
learn about Ruby on Rails, and the API, which serves as a reference.
|
121
121
|
|
122
|
-
You can help improve the Rails guides by making them more coherent, consistent or readable, adding missing information, correcting factual errors, fixing typos, or bringing it up to date with the latest edge Rails.
|
122
|
+
You can help improve the Rails guides by making them more coherent, consistent or readable, adding missing information, correcting factual errors, fixing typos, or bringing it up to date with the latest edge Rails.
|
123
123
|
|
124
124
|
You can either open a pull request to [Rails](http://github.com/rails/rails) or
|
125
125
|
ask the [Rails core team](http://rubyonrails.org/core) for commit access on
|
126
|
-
|
126
|
+
docrails if you contribute regularly.
|
127
127
|
Please do not open pull requests in docrails, if you'd like to get feedback on your
|
128
128
|
change, ask for it in [Rails](http://github.com/rails/rails) instead.
|
129
129
|
|
@@ -281,7 +281,7 @@ You can run a single test through ruby. For instance:
|
|
281
281
|
|
282
282
|
```bash
|
283
283
|
$ cd actionmailer
|
284
|
-
$ ruby -w -Itest test/mail_layout_test.rb -n test_explicit_class_layout
|
284
|
+
$ bundle exec ruby -w -Itest test/mail_layout_test.rb -n test_explicit_class_layout
|
285
285
|
```
|
286
286
|
|
287
287
|
The `-n` option allows you to run a single method instead of the whole
|
data/guides/source/engines.md
CHANGED
@@ -589,7 +589,7 @@ the comments, however, is not quite right yet. If you were to create a comment
|
|
589
589
|
right now, you would see this error:
|
590
590
|
|
591
591
|
```
|
592
|
-
Missing partial blorgh/comments/
|
592
|
+
Missing partial blorgh/comments/_comment with {:handlers=>[:erb, :builder],
|
593
593
|
:formats=>[:html], :locale=>[:en, :en]}. Searched in: *
|
594
594
|
"/Users/ryan/Sites/side_projects/blorgh/test/dummy/app/views" *
|
595
595
|
"/Users/ryan/Sites/side_projects/blorgh/app/views"
|
@@ -598,7 +598,7 @@ Missing partial blorgh/comments/comment with {:handlers=>[:erb, :builder],
|
|
598
598
|
The engine is unable to find the partial required for rendering the comments.
|
599
599
|
Rails looks first in the application's (`test/dummy`) `app/views` directory and
|
600
600
|
then in the engine's `app/views` directory. When it can't find it, it will throw
|
601
|
-
this error. The engine knows to look for `blorgh/comments/
|
601
|
+
this error. The engine knows to look for `blorgh/comments/_comment` because the
|
602
602
|
model object it is receiving is from the `Blorgh::Comment` class.
|
603
603
|
|
604
604
|
This partial will be responsible for rendering just the comment text, for now.
|
@@ -696,8 +696,8 @@ haven't been copied over already. The first run for this command will output
|
|
696
696
|
something such as this:
|
697
697
|
|
698
698
|
```bash
|
699
|
-
Copied migration [timestamp_1]_create_blorgh_articles.rb from blorgh
|
700
|
-
Copied migration [timestamp_2]_create_blorgh_comments.rb from blorgh
|
699
|
+
Copied migration [timestamp_1]_create_blorgh_articles.blorgh.rb from blorgh
|
700
|
+
Copied migration [timestamp_2]_create_blorgh_comments.blorgh.rb from blorgh
|
701
701
|
```
|
702
702
|
|
703
703
|
The first timestamp (`[timestamp_1]`) will be the current time, and the second
|
@@ -829,11 +829,9 @@ Notice that only _one_ migration was copied over here. This is because the first
|
|
829
829
|
two migrations were copied over the first time this command was run.
|
830
830
|
|
831
831
|
```
|
832
|
-
NOTE Migration [timestamp]_create_blorgh_articles.rb from blorgh has been
|
833
|
-
skipped. Migration with the same name already exists.
|
834
|
-
[timestamp]
|
835
|
-
with the same name already exists. Copied migration
|
836
|
-
[timestamp]_add_author_id_to_blorgh_articles.rb from blorgh
|
832
|
+
NOTE Migration [timestamp]_create_blorgh_articles.blorgh.rb from blorgh has been skipped. Migration with the same name already exists.
|
833
|
+
NOTE Migration [timestamp]_create_blorgh_comments.blorgh.rb from blorgh has been skipped. Migration with the same name already exists.
|
834
|
+
Copied migration [timestamp]_add_author_id_to_blorgh_articles.blorgh.rb from blorgh
|
837
835
|
```
|
838
836
|
|
839
837
|
Run the migration using:
|
@@ -888,7 +886,9 @@ engine this would be done by changing
|
|
888
886
|
`app/controllers/blorgh/application_controller.rb` to look like:
|
889
887
|
|
890
888
|
```ruby
|
891
|
-
|
889
|
+
module Blorgh
|
890
|
+
class ApplicationController < ::ApplicationController
|
891
|
+
end
|
892
892
|
end
|
893
893
|
```
|
894
894
|
|
@@ -123,7 +123,7 @@ run the following:
|
|
123
123
|
$ rails --version
|
124
124
|
```
|
125
125
|
|
126
|
-
If it says something like "Rails 4.2.
|
126
|
+
If it says something like "Rails 4.2.1", you are ready to continue.
|
127
127
|
|
128
128
|
### Creating the Blog Application
|
129
129
|
|
@@ -318,9 +318,9 @@ root 'welcome#index'
|
|
318
318
|
application to the welcome controller's index action and `get 'welcome/index'`
|
319
319
|
tells Rails to map requests to <http://localhost:3000/welcome/index> to the
|
320
320
|
welcome controller's index action. This was created earlier when you ran the
|
321
|
-
controller generator (`rails generate controller welcome index`).
|
321
|
+
controller generator (`bin/rails generate controller welcome index`).
|
322
322
|
|
323
|
-
Launch the web server again if you stopped it to generate the controller (`rails
|
323
|
+
Launch the web server again if you stopped it to generate the controller (`bin/rails
|
324
324
|
server`) and navigate to <http://localhost:3000> in your browser. You'll see the
|
325
325
|
"Hello, Rails!" message you put into `app/views/welcome/index.html.erb`,
|
326
326
|
indicating that this new route is indeed going to `WelcomeController`'s `index`
|
@@ -353,7 +353,7 @@ Rails.application.routes.draw do
|
|
353
353
|
end
|
354
354
|
```
|
355
355
|
|
356
|
-
If you run `rake routes`, you'll see that it has defined routes for all the
|
356
|
+
If you run `bin/rake routes`, you'll see that it has defined routes for all the
|
357
357
|
standard RESTful actions. The meaning of the prefix column (and other columns)
|
358
358
|
will be seen later, but for now notice that Rails has inferred the
|
359
359
|
singular form `article` and makes meaningful use of the distinction.
|
@@ -397,7 +397,7 @@ a controller called `ArticlesController`. You can do this by running this
|
|
397
397
|
command:
|
398
398
|
|
399
399
|
```bash
|
400
|
-
$ bin/rails
|
400
|
+
$ bin/rails generate controller articles
|
401
401
|
```
|
402
402
|
|
403
403
|
If you open up the newly generated `app/controllers/articles_controller.rb`
|
@@ -551,7 +551,7 @@ this:
|
|
551
551
|
|
552
552
|
In this example, the `articles_path` helper is passed to the `:url` option.
|
553
553
|
To see what Rails will do with this, we look back at the output of
|
554
|
-
`rake routes`:
|
554
|
+
`bin/rake routes`:
|
555
555
|
|
556
556
|
```bash
|
557
557
|
$ bin/rake routes
|
@@ -661,7 +661,7 @@ models, as that will be done automatically by Active Record.
|
|
661
661
|
|
662
662
|
### Running a Migration
|
663
663
|
|
664
|
-
As we've just seen, `rails generate model` created a _database migration_ file
|
664
|
+
As we've just seen, `bin/rails generate model` created a _database migration_ file
|
665
665
|
inside the `db/migrate` directory. Migrations are Ruby classes that are
|
666
666
|
designed to make it simple to create and modify database tables. Rails uses
|
667
667
|
rake commands to run migrations, and it's possible to undo a migration after
|
@@ -714,7 +714,7 @@ NOTE. Because you're working in the development environment by default, this
|
|
714
714
|
command will apply to the database defined in the `development` section of your
|
715
715
|
`config/database.yml` file. If you would like to execute migrations in another
|
716
716
|
environment, for instance in production, you must explicitly pass it when
|
717
|
-
invoking the command: `rake db:migrate RAILS_ENV=production`.
|
717
|
+
invoking the command: `bin/rake db:migrate RAILS_ENV=production`.
|
718
718
|
|
719
719
|
### Saving data in the controller
|
720
720
|
|
@@ -801,7 +801,7 @@ If you submit the form again now, Rails will complain about not finding the
|
|
801
801
|
`show` action. That's not very useful though, so let's add the `show` action
|
802
802
|
before proceeding.
|
803
803
|
|
804
|
-
As we have seen in the output of `rake routes`, the route for `show` action is
|
804
|
+
As we have seen in the output of `bin/rake routes`, the route for `show` action is
|
805
805
|
as follows:
|
806
806
|
|
807
807
|
```
|
@@ -831,7 +831,7 @@ class ArticlesController < ApplicationController
|
|
831
831
|
def new
|
832
832
|
end
|
833
833
|
|
834
|
-
#
|
834
|
+
# snippet for brevity
|
835
835
|
```
|
836
836
|
|
837
837
|
A couple of things to note. We use `Article.find` to find the article we're
|
@@ -863,7 +863,7 @@ Visit <http://localhost:3000/articles/new> and give it a try!
|
|
863
863
|
### Listing all articles
|
864
864
|
|
865
865
|
We still need a way to list all our articles, so let's do that.
|
866
|
-
The route for this as per output of `rake routes` is:
|
866
|
+
The route for this as per output of `bin/rake routes` is:
|
867
867
|
|
868
868
|
```
|
869
869
|
articles GET /articles(.:format) articles#index
|
@@ -887,7 +887,7 @@ class ArticlesController < ApplicationController
|
|
887
887
|
def new
|
888
888
|
end
|
889
889
|
|
890
|
-
#
|
890
|
+
# snippet for brevity
|
891
891
|
```
|
892
892
|
|
893
893
|
And then finally, add the view for this action, located at
|
@@ -1357,7 +1357,7 @@ Then do the same for the `app/views/articles/edit.html.erb` view:
|
|
1357
1357
|
|
1358
1358
|
We're now ready to cover the "D" part of CRUD, deleting articles from the
|
1359
1359
|
database. Following the REST convention, the route for
|
1360
|
-
deleting articles as per output of `rake routes` is:
|
1360
|
+
deleting articles as per output of `bin/rake routes` is:
|
1361
1361
|
|
1362
1362
|
```ruby
|
1363
1363
|
DELETE /articles/:id(.:format) articles#destroy
|
@@ -1536,20 +1536,17 @@ class CreateComments < ActiveRecord::Migration
|
|
1536
1536
|
create_table :comments do |t|
|
1537
1537
|
t.string :commenter
|
1538
1538
|
t.text :body
|
1539
|
-
|
1540
|
-
# this line adds an integer column called `article_id`.
|
1541
|
-
t.references :article, index: true
|
1539
|
+
t.references :article, index: true, foreign_key: true
|
1542
1540
|
|
1543
1541
|
t.timestamps null: false
|
1544
1542
|
end
|
1545
|
-
add_foreign_key :comments, :articles
|
1546
1543
|
end
|
1547
1544
|
end
|
1548
1545
|
```
|
1549
1546
|
|
1550
|
-
The `t.references` line
|
1551
|
-
|
1552
|
-
|
1547
|
+
The `t.references` line creates an integer column called `article_id`, an index
|
1548
|
+
for it, and a foreign key constraint that points to the `articles` table. Go
|
1549
|
+
ahead and run the migration:
|
1553
1550
|
|
1554
1551
|
```bash
|
1555
1552
|
$ bin/rake db:migrate
|
@@ -1562,8 +1559,6 @@ run against the current database, so in this case you will just see:
|
|
1562
1559
|
== CreateComments: migrating =================================================
|
1563
1560
|
-- create_table(:comments)
|
1564
1561
|
-> 0.0115s
|
1565
|
-
-- add_foreign_key(:comments, :articles)
|
1566
|
-
-> 0.0000s
|
1567
1562
|
== CreateComments: migrated (0.0119s) ========================================
|
1568
1563
|
```
|
1569
1564
|
|
@@ -1993,7 +1988,7 @@ class ArticlesController < ApplicationController
|
|
1993
1988
|
@articles = Article.all
|
1994
1989
|
end
|
1995
1990
|
|
1996
|
-
#
|
1991
|
+
# snippet for brevity
|
1997
1992
|
```
|
1998
1993
|
|
1999
1994
|
We also want to allow only authenticated users to delete comments, so in the
|
@@ -2009,7 +2004,7 @@ class CommentsController < ApplicationController
|
|
2009
2004
|
# ...
|
2010
2005
|
end
|
2011
2006
|
|
2012
|
-
#
|
2007
|
+
# snippet for brevity
|
2013
2008
|
```
|
2014
2009
|
|
2015
2010
|
Now if you try to create a new article, you will be greeted with a basic HTTP
|