rails 4.2.0 → 4.2.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/guides/CHANGELOG.md +17 -0
- data/guides/assets/images/getting_started/article_with_comments.png +0 -0
- data/guides/bug_report_templates/action_controller_gem.rb +1 -1
- data/guides/bug_report_templates/active_record_gem.rb +1 -1
- 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/3_1_release_notes.md +1 -1
- data/guides/source/4_2_release_notes.md +27 -0
- data/guides/source/action_controller_overview.md +2 -59
- data/guides/source/action_mailer_basics.md +8 -3
- data/guides/source/action_view_overview.md +2 -53
- data/guides/source/active_record_basics.md +5 -5
- data/guides/source/active_record_querying.md +1 -1
- data/guides/source/active_support_core_extensions.md +0 -47
- data/guides/source/asset_pipeline.md +2 -58
- data/guides/source/association_basics.md +15 -6
- data/guides/source/{constant_autoloading_and_reloading.md → autoloading_and_reloading_constants.md} +42 -28
- data/guides/source/configuring.md +1 -1
- data/guides/source/contributing_to_ruby_on_rails.md +4 -0
- data/guides/source/documents.yaml +3 -3
- data/guides/source/engines.md +10 -10
- data/guides/source/getting_started.md +32 -26
- data/guides/source/i18n.md +3 -2
- data/guides/source/initialization.md +1 -1
- data/guides/source/routing.md +12 -0
- data/guides/source/testing.md +11 -2
- data/guides/source/upgrading_ruby_on_rails.md +34 -2
- metadata +22 -20
@@ -207,7 +207,7 @@ precompiling works.
|
|
207
207
|
|
208
208
|
NOTE: You must have an ExecJS supported runtime in order to use CoffeeScript.
|
209
209
|
If you are using Mac OS X or Windows, you have a JavaScript runtime installed in
|
210
|
-
your operating system. Check [ExecJS](https://github.com/
|
210
|
+
your operating system. Check [ExecJS](https://github.com/rails/execjs#readme) documentation to know all supported JavaScript runtimes.
|
211
211
|
|
212
212
|
You can also disable generation of controller specific asset files by adding the
|
213
213
|
following to your `config/application.rb` configuration:
|
@@ -727,27 +727,6 @@ include, you can add them to the `precompile` array in `config/initializers/asse
|
|
727
727
|
Rails.application.config.assets.precompile += ['admin.js', 'admin.css', 'swfObject.js']
|
728
728
|
```
|
729
729
|
|
730
|
-
Or, you can opt to precompile all assets with something like this:
|
731
|
-
|
732
|
-
```ruby
|
733
|
-
# config/initializers/assets.rb
|
734
|
-
Rails.application.config.assets.precompile << Proc.new do |path|
|
735
|
-
if path =~ /\.(css|js)\z/
|
736
|
-
full_path = Rails.application.assets.resolve(path).to_path
|
737
|
-
app_assets_path = Rails.root.join('app', 'assets').to_path
|
738
|
-
if full_path.starts_with? app_assets_path
|
739
|
-
logger.info "including asset: " + full_path
|
740
|
-
true
|
741
|
-
else
|
742
|
-
logger.info "excluding asset: " + full_path
|
743
|
-
false
|
744
|
-
end
|
745
|
-
else
|
746
|
-
false
|
747
|
-
end
|
748
|
-
end
|
749
|
-
```
|
750
|
-
|
751
730
|
NOTE. Always specify an expected compiled filename that ends with .js or .css,
|
752
731
|
even if you want to add Sass or CoffeeScript files to the precompile array.
|
753
732
|
|
@@ -810,41 +789,6 @@ location ~ ^/assets/ {
|
|
810
789
|
}
|
811
790
|
```
|
812
791
|
|
813
|
-
#### GZip Compression
|
814
|
-
|
815
|
-
When files are precompiled, Sprockets also creates a
|
816
|
-
[gzipped](http://en.wikipedia.org/wiki/Gzip) (.gz) version of your assets. Web
|
817
|
-
servers are typically configured to use a moderate compression ratio as a
|
818
|
-
compromise, but since precompilation happens once, Sprockets uses the maximum
|
819
|
-
compression ratio, thus reducing the size of the data transfer to the minimum.
|
820
|
-
On the other hand, web servers can be configured to serve compressed content
|
821
|
-
directly from disk, rather than deflating non-compressed files themselves.
|
822
|
-
|
823
|
-
NGINX is able to do this automatically enabling `gzip_static`:
|
824
|
-
|
825
|
-
```nginx
|
826
|
-
location ~ ^/(assets)/ {
|
827
|
-
root /path/to/public;
|
828
|
-
gzip_static on; # to serve pre-gzipped version
|
829
|
-
expires max;
|
830
|
-
add_header Cache-Control public;
|
831
|
-
}
|
832
|
-
```
|
833
|
-
|
834
|
-
This directive is available if the core module that provides this feature was
|
835
|
-
compiled with the web server. Ubuntu/Debian packages, even `nginx-light`, have
|
836
|
-
the module compiled. Otherwise, you may need to perform a manual compilation:
|
837
|
-
|
838
|
-
```bash
|
839
|
-
./configure --with-http_gzip_static_module
|
840
|
-
```
|
841
|
-
|
842
|
-
If you're compiling NGINX with Phusion Passenger you'll need to pass that option
|
843
|
-
when prompted.
|
844
|
-
|
845
|
-
A robust configuration for Apache is possible but tricky; please Google around.
|
846
|
-
(Or help update this Guide if you have a good configuration example for Apache.)
|
847
|
-
|
848
792
|
### Local Precompilation
|
849
793
|
|
850
794
|
There are several reasons why you might want to precompile your assets locally.
|
@@ -1156,7 +1100,7 @@ The following line invokes `uglifier` for JavaScript compression.
|
|
1156
1100
|
config.assets.js_compressor = :uglifier
|
1157
1101
|
```
|
1158
1102
|
|
1159
|
-
NOTE: You will need an [ExecJS](https://github.com/
|
1103
|
+
NOTE: You will need an [ExecJS](https://github.com/rails/execjs#readme)
|
1160
1104
|
supported runtime in order to use `uglifier`. If you are using Mac OS X or
|
1161
1105
|
Windows you have a JavaScript runtime installed in your operating system.
|
1162
1106
|
|
@@ -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
|
|
@@ -1977,8 +1987,8 @@ While Rails uses intelligent defaults that will work well in most situations, th
|
|
1977
1987
|
|
1978
1988
|
```ruby
|
1979
1989
|
class Parts < ActiveRecord::Base
|
1980
|
-
has_and_belongs_to_many :assemblies,
|
1981
|
-
|
1990
|
+
has_and_belongs_to_many :assemblies, -> { readonly },
|
1991
|
+
autosave: true
|
1982
1992
|
end
|
1983
1993
|
```
|
1984
1994
|
|
@@ -1990,7 +2000,6 @@ The `has_and_belongs_to_many` association supports these options:
|
|
1990
2000
|
* `:foreign_key`
|
1991
2001
|
* `:join_table`
|
1992
2002
|
* `:validate`
|
1993
|
-
* `:readonly`
|
1994
2003
|
|
1995
2004
|
##### `:association_foreign_key`
|
1996
2005
|
|
data/guides/source/{constant_autoloading_and_reloading.md → autoloading_and_reloading_constants.md}
RENAMED
@@ -1,5 +1,5 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
Autoloading and Reloading Constants
|
2
|
+
===================================
|
3
3
|
|
4
4
|
This guide documents how constant autoloading and reloading works.
|
5
5
|
|
@@ -78,7 +78,8 @@ end
|
|
78
78
|
```
|
79
79
|
|
80
80
|
The *nesting* at any given place is the collection of enclosing nested class and
|
81
|
-
module objects outwards.
|
81
|
+
module objects outwards. The nesting at any given place can be inspected with
|
82
|
+
`Module.nesting`. For example, in the previous example, the nesting at
|
82
83
|
(1) is
|
83
84
|
|
84
85
|
```ruby
|
@@ -111,6 +112,16 @@ certain nesting does not necessarily correlate with the namespaces at the spot.
|
|
111
112
|
Even more, they are totally independent, take for instance
|
112
113
|
|
113
114
|
```ruby
|
115
|
+
module X
|
116
|
+
module Y
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
module A
|
121
|
+
module B
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
114
125
|
module X::Y
|
115
126
|
module A::B
|
116
127
|
# (3)
|
@@ -138,9 +149,10 @@ executed, and popped after it.
|
|
138
149
|
|
139
150
|
* A singleton class opened with `class << object` gets pushed, and popped later.
|
140
151
|
|
141
|
-
* When
|
152
|
+
* When `instance_eval` is called using a string argument,
|
142
153
|
the singleton class of the receiver is pushed to the nesting of the eval'ed
|
143
|
-
code.
|
154
|
+
code. When `class_eval` or `module_eval` is called using a string argument,
|
155
|
+
the receiver is pushed to the nesting of the eval'ed code.
|
144
156
|
|
145
157
|
* The nesting at the top-level of code interpreted by `Kernel#load` is empty
|
146
158
|
unless the `load` call receives a true value as second argument, in which case
|
@@ -151,8 +163,6 @@ the blocks that may be passed to `Class.new` and `Module.new` do not get the
|
|
151
163
|
class or module being defined pushed to their nesting. That's one of the
|
152
164
|
differences between defining classes and modules in one way or another.
|
153
165
|
|
154
|
-
The nesting at any given place can be inspected with `Module.nesting`.
|
155
|
-
|
156
166
|
### Class and Module Definitions are Constant Assignments
|
157
167
|
|
158
168
|
Let's suppose the following snippet creates a class (rather than reopening it):
|
@@ -186,8 +196,8 @@ Project.name # => "Project"
|
|
186
196
|
```
|
187
197
|
|
188
198
|
Constant assignment has a special rule to make that happen: if the object
|
189
|
-
being assigned is an anonymous class or module, Ruby sets
|
190
|
-
|
199
|
+
being assigned is an anonymous class or module, Ruby sets the object's name to
|
200
|
+
the name of the constant.
|
191
201
|
|
192
202
|
INFO. From then on, what happens to the constant and the instance does not
|
193
203
|
matter. For example, the constant could be deleted, the class object could be
|
@@ -221,7 +231,7 @@ assignment.
|
|
221
231
|
Thus, when one informally says "the `String` class", that really means: the
|
222
232
|
class object stored in the constant called "String" in the class object stored
|
223
233
|
in the `Object` constant. `String` is otherwise an ordinary Ruby constant and
|
224
|
-
everything related to constants
|
234
|
+
everything related to constants such as resolution algorithms applies to it.
|
225
235
|
|
226
236
|
Likewise, in the controller
|
227
237
|
|
@@ -234,7 +244,7 @@ end
|
|
234
244
|
```
|
235
245
|
|
236
246
|
`Post` is not syntax for a class. Rather, `Post` is a regular Ruby constant. If
|
237
|
-
all is good, the constant
|
247
|
+
all is good, the constant is evaluated to an object that responds to `all`.
|
238
248
|
|
239
249
|
That is why we talk about *constant* autoloading, Rails has the ability to
|
240
250
|
load constants on the fly.
|
@@ -256,7 +266,7 @@ module Colors
|
|
256
266
|
end
|
257
267
|
```
|
258
268
|
|
259
|
-
First, when the `module` keyword is processed the interpreter creates a new
|
269
|
+
First, when the `module` keyword is processed, the interpreter creates a new
|
260
270
|
entry in the constant table of the class object stored in the `Object` constant.
|
261
271
|
Said entry associates the name "Colors" to a newly created module object.
|
262
272
|
Furthermore, the interpreter sets the name of the new module object to be the
|
@@ -270,7 +280,7 @@ In particular, `Colors::RED` is totally unrelated to any other `RED` constant
|
|
270
280
|
that may live in any other class or module object. If there were any, they
|
271
281
|
would have separate entries in their respective constant tables.
|
272
282
|
|
273
|
-
|
283
|
+
Pay special attention in the previous paragraphs to the distinction between
|
274
284
|
class and module objects, constant names, and value objects associated to them
|
275
285
|
in constant tables.
|
276
286
|
|
@@ -289,12 +299,14 @@ order. The ancestors of those elements are ignored.
|
|
289
299
|
|
290
300
|
2. If not found, then the algorithm walks up the ancestor chain of the cref.
|
291
301
|
|
292
|
-
3. If not found
|
302
|
+
3. If not found and the cref is a module, the constant is looked up in `Object`.
|
303
|
+
|
304
|
+
4. If not found, `const_missing` is invoked on the cref. The default
|
293
305
|
implementation of `const_missing` raises `NameError`, but it can be overridden.
|
294
306
|
|
295
307
|
Rails autoloading **does not emulate this algorithm**, but its starting point is
|
296
308
|
the name of the constant to be autoloaded, and the cref. See more in [Relative
|
297
|
-
References](#relative-references).
|
309
|
+
References](#autoloading-algorithms-relative-references).
|
298
310
|
|
299
311
|
#### Resolution Algorithm for Qualified Constants
|
300
312
|
|
@@ -312,7 +324,7 @@ relative: `::Billing::Invoice`. That would force `Billing` to be looked up
|
|
312
324
|
only as a top-level constant.
|
313
325
|
|
314
326
|
`Invoice` on the other hand is qualified by `Billing` and we are going to see
|
315
|
-
its resolution next. Let's
|
327
|
+
its resolution next. Let's define *parent* to be that qualifying class or module
|
316
328
|
object, that is, `Billing` in the example above. The algorithm for qualified
|
317
329
|
constants goes like this:
|
318
330
|
|
@@ -328,7 +340,7 @@ checked.
|
|
328
340
|
|
329
341
|
Rails autoloading **does not emulate this algorithm**, but its starting point is
|
330
342
|
the name of the constant to be autoloaded, and the parent. See more in
|
331
|
-
[Qualified References](#qualified-references).
|
343
|
+
[Qualified References](#autoloading-algorithms-qualified-references).
|
332
344
|
|
333
345
|
|
334
346
|
Vocabulary
|
@@ -439,18 +451,19 @@ default it contains:
|
|
439
451
|
`app/controllers`. They do not need to be the default ones, any custom
|
440
452
|
directories like `app/workers` belong automatically to `autoload_paths`.
|
441
453
|
|
442
|
-
*
|
454
|
+
* Second level directories `app/{controllers,models}/concerns` in the
|
443
455
|
application and engines.
|
444
456
|
|
445
457
|
* The directory `test/mailers/previews`.
|
446
458
|
|
447
459
|
Also, this collection is configurable via `config.autoload_paths`. For example,
|
448
460
|
`lib` was in the list years ago, but no longer is. An application can opt-in
|
449
|
-
|
461
|
+
by adding this to `config/application.rb`:
|
450
462
|
|
451
463
|
```ruby
|
452
|
-
config.autoload_paths
|
464
|
+
config.autoload_paths << "#{Rails.root}/lib"
|
453
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.
|
454
467
|
|
455
468
|
The value of `autoload_paths` can be inspected. In a just generated application
|
456
469
|
it is (edited):
|
@@ -683,12 +696,12 @@ creates an empty module and assigns it to the `Admin` constant on the fly.
|
|
683
696
|
### Generic Procedure
|
684
697
|
|
685
698
|
Relative references are reported to be missing in the cref where they were hit,
|
686
|
-
and qualified references are reported to be missing in their parent
|
699
|
+
and qualified references are reported to be missing in their parent (see
|
687
700
|
[Resolution Algorithm for Relative
|
688
701
|
Constants](#resolution-algorithm-for-relative-constants) at the beginning of
|
689
702
|
this guide for the definition of *cref*, and [Resolution Algorithm for Qualified
|
690
703
|
Constants](#resolution-algorithm-for-qualified-constants) for the definition of
|
691
|
-
*parent
|
704
|
+
*parent*).
|
692
705
|
|
693
706
|
The procedure to autoload constant `C` in an arbitrary situation is as follows:
|
694
707
|
|
@@ -866,8 +879,8 @@ end
|
|
866
879
|
```
|
867
880
|
|
868
881
|
To resolve `User` Ruby checks `Admin` in the former case, but it does not in
|
869
|
-
the latter because it does not belong to the nesting
|
870
|
-
and [Resolution Algorithms](#resolution-algorithms).
|
882
|
+
the latter because it does not belong to the nesting (see [Nesting](#nesting)
|
883
|
+
and [Resolution Algorithms](#resolution-algorithms)).
|
871
884
|
|
872
885
|
Unfortunately Rails autoloading does not know the nesting in the spot where the
|
873
886
|
constant was missing and so it is not able to act as Ruby would. In particular,
|
@@ -889,7 +902,7 @@ end
|
|
889
902
|
|
890
903
|
### Autoloading and STI
|
891
904
|
|
892
|
-
Single Table Inheritance (STI) is a feature of Active Record that
|
905
|
+
Single Table Inheritance (STI) is a feature of Active Record that enables
|
893
906
|
storing a hierarchy of models in one single table. The API of such models is
|
894
907
|
aware of the hierarchy and encapsulates some common needs. For example, given
|
895
908
|
these classes:
|
@@ -1171,7 +1184,8 @@ class Hotel
|
|
1171
1184
|
end
|
1172
1185
|
```
|
1173
1186
|
|
1174
|
-
the expression `Hotel::Image` is ambiguous
|
1187
|
+
the expression `Hotel::Image` is ambiguous because it depends on the execution
|
1188
|
+
path.
|
1175
1189
|
|
1176
1190
|
As [we saw before](#resolution-algorithm-for-qualified-constants), Ruby looks
|
1177
1191
|
up the constant in `Hotel` and its ancestors. If `app/models/image.rb` has
|
@@ -1280,8 +1294,8 @@ c.user # surprisingly fine, User
|
|
1280
1294
|
c.user # NameError: uninitialized constant C::User
|
1281
1295
|
```
|
1282
1296
|
|
1283
|
-
because it detects a parent namespace already has the constant (see [Qualified
|
1284
|
-
References](#qualified-references).
|
1297
|
+
because it detects that a parent namespace already has the constant (see [Qualified
|
1298
|
+
References](#autoloading-algorithms-qualified-references)).
|
1285
1299
|
|
1286
1300
|
As with pure Ruby, within the body of a direct descendant of `BasicObject` use
|
1287
1301
|
always absolute constant paths:
|
@@ -197,7 +197,7 @@ The full set of methods that can be used in this block are as follows:
|
|
197
197
|
Every Rails application comes with a standard set of middleware which it uses in this order in the development environment:
|
198
198
|
|
199
199
|
* `ActionDispatch::SSL` forces every request to be under HTTPS protocol. Will be available if `config.force_ssl` is set to `true`. Options passed to this can be configured by using `config.ssl_options`.
|
200
|
-
* `ActionDispatch::Static` is used to serve static assets. Disabled if `config.
|
200
|
+
* `ActionDispatch::Static` is used to serve static assets. Disabled if `config.serve_static_files` is `false`.
|
201
201
|
* `Rack::Lock` wraps the app in mutex so it can only be called by a single thread at a time. Only enabled when `config.cache_classes` is `false`.
|
202
202
|
* `ActiveSupport::Cache::Strategy::LocalCache` serves as a basic memory backed cache. This cache is not thread safe and is intended only for serving as a temporary memory cache for a single thread.
|
203
203
|
* `Rack::Runtime` sets an `X-Runtime` header, containing the time (in seconds) taken to execute the request.
|
@@ -361,6 +361,10 @@ A CHANGELOG entry should summarize what was changed and should end with author's
|
|
361
361
|
|
362
362
|
Your name can be added directly after the last word if you don't provide any code examples or don't need multiple paragraphs. Otherwise, it's best to make as a new paragraph.
|
363
363
|
|
364
|
+
### Updating the Gemfile.lock
|
365
|
+
|
366
|
+
Some changes requires the dependencies to be upgraded. In these cases make sure you run `bundle update` to get the right version of the dependency and commit the `Gemfile.lock` file within your changes.
|
367
|
+
|
364
368
|
### Sanity Check
|
365
369
|
|
366
370
|
You should not be the only person who looks at the code before you submit it.
|
@@ -123,9 +123,9 @@
|
|
123
123
|
url: initialization.html
|
124
124
|
description: This guide explains the internals of the Rails initialization process as of Rails 4
|
125
125
|
-
|
126
|
-
name:
|
127
|
-
url:
|
128
|
-
description: This guide documents how
|
126
|
+
name: Autoloading and Reloading Constants
|
127
|
+
url: autoloading_and_reloading_constants.html
|
128
|
+
description: This guide documents how autoloading and reloading constants work.
|
129
129
|
-
|
130
130
|
name: Extending Rails
|
131
131
|
documents:
|
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
|
|
@@ -191,6 +191,9 @@ following in the `blog` directory:
|
|
191
191
|
$ bin/rails server
|
192
192
|
```
|
193
193
|
|
194
|
+
TIP: If you are using Windows, you have to pass the scripts under the `bin`
|
195
|
+
folder directly to the Ruby interpreter e.g. `ruby bin\rails server`.
|
196
|
+
|
194
197
|
TIP: Compiling CoffeeScript and JavaScript asset compression requires you
|
195
198
|
have a JavaScript runtime available on your system, in the absence
|
196
199
|
of a runtime you will see an `execjs` error during asset compilation.
|
@@ -199,7 +202,7 @@ Rails adds the `therubyracer` gem to the generated `Gemfile` in a
|
|
199
202
|
commented line for new apps and you can uncomment if you need it.
|
200
203
|
`therubyrhino` is the recommended runtime for JRuby users and is added by
|
201
204
|
default to the `Gemfile` in apps generated under JRuby. You can investigate
|
202
|
-
all the supported runtimes at [ExecJS](https://github.com/
|
205
|
+
all the supported runtimes at [ExecJS](https://github.com/rails/execjs#readme).
|
203
206
|
|
204
207
|
This will fire up WEBrick, a web server distributed with Ruby by default. To see
|
205
208
|
your application in action, open a browser window and navigate to
|
@@ -315,9 +318,9 @@ root 'welcome#index'
|
|
315
318
|
application to the welcome controller's index action and `get 'welcome/index'`
|
316
319
|
tells Rails to map requests to <http://localhost:3000/welcome/index> to the
|
317
320
|
welcome controller's index action. This was created earlier when you ran the
|
318
|
-
controller generator (`rails generate controller welcome index`).
|
321
|
+
controller generator (`bin/rails generate controller welcome index`).
|
319
322
|
|
320
|
-
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
|
321
324
|
server`) and navigate to <http://localhost:3000> in your browser. You'll see the
|
322
325
|
"Hello, Rails!" message you put into `app/views/welcome/index.html.erb`,
|
323
326
|
indicating that this new route is indeed going to `WelcomeController`'s `index`
|
@@ -350,7 +353,7 @@ Rails.application.routes.draw do
|
|
350
353
|
end
|
351
354
|
```
|
352
355
|
|
353
|
-
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
|
354
357
|
standard RESTful actions. The meaning of the prefix column (and other columns)
|
355
358
|
will be seen later, but for now notice that Rails has inferred the
|
356
359
|
singular form `article` and makes meaningful use of the distinction.
|
@@ -394,7 +397,7 @@ a controller called `ArticlesController`. You can do this by running this
|
|
394
397
|
command:
|
395
398
|
|
396
399
|
```bash
|
397
|
-
$ bin/rails
|
400
|
+
$ bin/rails generate controller articles
|
398
401
|
```
|
399
402
|
|
400
403
|
If you open up the newly generated `app/controllers/articles_controller.rb`
|
@@ -548,7 +551,7 @@ this:
|
|
548
551
|
|
549
552
|
In this example, the `articles_path` helper is passed to the `:url` option.
|
550
553
|
To see what Rails will do with this, we look back at the output of
|
551
|
-
`rake routes`:
|
554
|
+
`bin/rake routes`:
|
552
555
|
|
553
556
|
```bash
|
554
557
|
$ bin/rake routes
|
@@ -658,7 +661,7 @@ models, as that will be done automatically by Active Record.
|
|
658
661
|
|
659
662
|
### Running a Migration
|
660
663
|
|
661
|
-
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
|
662
665
|
inside the `db/migrate` directory. Migrations are Ruby classes that are
|
663
666
|
designed to make it simple to create and modify database tables. Rails uses
|
664
667
|
rake commands to run migrations, and it's possible to undo a migration after
|
@@ -711,7 +714,7 @@ NOTE. Because you're working in the development environment by default, this
|
|
711
714
|
command will apply to the database defined in the `development` section of your
|
712
715
|
`config/database.yml` file. If you would like to execute migrations in another
|
713
716
|
environment, for instance in production, you must explicitly pass it when
|
714
|
-
invoking the command: `rake db:migrate RAILS_ENV=production`.
|
717
|
+
invoking the command: `bin/rake db:migrate RAILS_ENV=production`.
|
715
718
|
|
716
719
|
### Saving data in the controller
|
717
720
|
|
@@ -798,7 +801,7 @@ If you submit the form again now, Rails will complain about not finding the
|
|
798
801
|
`show` action. That's not very useful though, so let's add the `show` action
|
799
802
|
before proceeding.
|
800
803
|
|
801
|
-
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
|
802
805
|
as follows:
|
803
806
|
|
804
807
|
```
|
@@ -828,7 +831,7 @@ class ArticlesController < ApplicationController
|
|
828
831
|
def new
|
829
832
|
end
|
830
833
|
|
831
|
-
#
|
834
|
+
# snippet for brevity
|
832
835
|
```
|
833
836
|
|
834
837
|
A couple of things to note. We use `Article.find` to find the article we're
|
@@ -860,7 +863,7 @@ Visit <http://localhost:3000/articles/new> and give it a try!
|
|
860
863
|
### Listing all articles
|
861
864
|
|
862
865
|
We still need a way to list all our articles, so let's do that.
|
863
|
-
The route for this as per output of `rake routes` is:
|
866
|
+
The route for this as per output of `bin/rake routes` is:
|
864
867
|
|
865
868
|
```
|
866
869
|
articles GET /articles(.:format) articles#index
|
@@ -884,7 +887,7 @@ class ArticlesController < ApplicationController
|
|
884
887
|
def new
|
885
888
|
end
|
886
889
|
|
887
|
-
#
|
890
|
+
# snippet for brevity
|
888
891
|
```
|
889
892
|
|
890
893
|
And then finally, add the view for this action, located at
|
@@ -1266,8 +1269,8 @@ bottom of the template:
|
|
1266
1269
|
```html+erb
|
1267
1270
|
...
|
1268
1271
|
|
1269
|
-
<%= link_to '
|
1270
|
-
<%= link_to '
|
1272
|
+
<%= link_to 'Edit', edit_article_path(@article) %> |
|
1273
|
+
<%= link_to 'Back', articles_path %>
|
1271
1274
|
```
|
1272
1275
|
|
1273
1276
|
And here's how our app looks so far:
|
@@ -1354,7 +1357,7 @@ Then do the same for the `app/views/articles/edit.html.erb` view:
|
|
1354
1357
|
|
1355
1358
|
We're now ready to cover the "D" part of CRUD, deleting articles from the
|
1356
1359
|
database. Following the REST convention, the route for
|
1357
|
-
deleting articles as per output of `rake routes` is:
|
1360
|
+
deleting articles as per output of `bin/rake routes` is:
|
1358
1361
|
|
1359
1362
|
```ruby
|
1360
1363
|
DELETE /articles/:id(.:format) articles#destroy
|
@@ -1539,6 +1542,7 @@ class CreateComments < ActiveRecord::Migration
|
|
1539
1542
|
|
1540
1543
|
t.timestamps null: false
|
1541
1544
|
end
|
1545
|
+
add_foreign_key :comments, :articles
|
1542
1546
|
end
|
1543
1547
|
end
|
1544
1548
|
```
|
@@ -1558,6 +1562,8 @@ run against the current database, so in this case you will just see:
|
|
1558
1562
|
== CreateComments: migrating =================================================
|
1559
1563
|
-- create_table(:comments)
|
1560
1564
|
-> 0.0115s
|
1565
|
+
-- add_foreign_key(:comments, :articles)
|
1566
|
+
-> 0.0000s
|
1561
1567
|
== CreateComments: migrated (0.0119s) ========================================
|
1562
1568
|
```
|
1563
1569
|
|
@@ -1673,8 +1679,8 @@ So first, we'll wire up the Article show template
|
|
1673
1679
|
</p>
|
1674
1680
|
<% end %>
|
1675
1681
|
|
1676
|
-
<%= link_to '
|
1677
|
-
<%= link_to '
|
1682
|
+
<%= link_to 'Edit', edit_article_path(@article) %> |
|
1683
|
+
<%= link_to 'Back', articles_path %>
|
1678
1684
|
```
|
1679
1685
|
|
1680
1686
|
This adds a form on the `Article` show page that creates a new comment by
|
@@ -1754,8 +1760,8 @@ add that to the `app/views/articles/show.html.erb`.
|
|
1754
1760
|
</p>
|
1755
1761
|
<% end %>
|
1756
1762
|
|
1757
|
-
<%= link_to 'Edit
|
1758
|
-
<%= link_to 'Back
|
1763
|
+
<%= link_to 'Edit', edit_article_path(@article) %> |
|
1764
|
+
<%= link_to 'Back', articles_path %>
|
1759
1765
|
```
|
1760
1766
|
|
1761
1767
|
Now you can add articles and comments to your blog and have them show up in the
|
@@ -1820,8 +1826,8 @@ following:
|
|
1820
1826
|
</p>
|
1821
1827
|
<% end %>
|
1822
1828
|
|
1823
|
-
<%= link_to 'Edit
|
1824
|
-
<%= link_to 'Back
|
1829
|
+
<%= link_to 'Edit', edit_article_path(@article) %> |
|
1830
|
+
<%= link_to 'Back', articles_path %>
|
1825
1831
|
```
|
1826
1832
|
|
1827
1833
|
This will now render the partial in `app/views/comments/_comment.html.erb` once
|
@@ -1870,8 +1876,8 @@ Then you make the `app/views/articles/show.html.erb` look like the following:
|
|
1870
1876
|
<h2>Add a comment:</h2>
|
1871
1877
|
<%= render 'comments/form' %>
|
1872
1878
|
|
1873
|
-
<%= link_to 'Edit
|
1874
|
-
<%= link_to 'Back
|
1879
|
+
<%= link_to 'Edit', edit_article_path(@article) %> |
|
1880
|
+
<%= link_to 'Back', articles_path %>
|
1875
1881
|
```
|
1876
1882
|
|
1877
1883
|
The second render just defines the partial template we want to render,
|
@@ -1987,7 +1993,7 @@ class ArticlesController < ApplicationController
|
|
1987
1993
|
@articles = Article.all
|
1988
1994
|
end
|
1989
1995
|
|
1990
|
-
#
|
1996
|
+
# snippet for brevity
|
1991
1997
|
```
|
1992
1998
|
|
1993
1999
|
We also want to allow only authenticated users to delete comments, so in the
|
@@ -2003,7 +2009,7 @@ class CommentsController < ApplicationController
|
|
2003
2009
|
# ...
|
2004
2010
|
end
|
2005
2011
|
|
2006
|
-
#
|
2012
|
+
# snippet for brevity
|
2007
2013
|
```
|
2008
2014
|
|
2009
2015
|
Now if you try to create a new article, you will be greeted with a basic HTTP
|