rails 4.0.0 → 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/README.md +30 -23
- data/guides/CHANGELOG.md +108 -6
- data/guides/Rakefile +21 -6
- data/guides/assets/images/akshaysurve.jpg +0 -0
- data/guides/assets/images/edge_badge.png +0 -0
- data/guides/assets/images/feature_tile.gif +0 -0
- data/guides/assets/images/footer_tile.gif +0 -0
- data/guides/assets/images/fxn.png +0 -0
- data/guides/assets/images/getting_started/article_with_comments.png +0 -0
- data/guides/assets/images/getting_started/challenge.png +0 -0
- data/guides/assets/images/getting_started/confirm_dialog.png +0 -0
- data/guides/assets/images/getting_started/forbidden_attributes_for_new_article.png +0 -0
- data/guides/assets/images/getting_started/form_with_errors.png +0 -0
- data/guides/assets/images/getting_started/index_action_with_edit_link.png +0 -0
- data/guides/assets/images/getting_started/new_article.png +0 -0
- data/guides/assets/images/getting_started/rails_welcome.png +0 -0
- data/guides/assets/images/getting_started/routing_error_no_controller.png +0 -0
- data/guides/assets/images/getting_started/routing_error_no_route_matches.png +0 -0
- data/guides/assets/images/getting_started/show_action_for_articles.png +0 -0
- data/guides/assets/images/getting_started/template_is_missing_articles_new.png +0 -0
- data/guides/assets/images/getting_started/unknown_action_create_for_articles.png +0 -0
- data/guides/assets/images/getting_started/unknown_action_new_for_articles.png +0 -0
- data/guides/assets/images/header_tile.gif +0 -0
- data/guides/assets/images/icons/README +1 -1
- data/guides/assets/images/icons/callouts/11.png +0 -0
- data/guides/assets/images/icons/callouts/12.png +0 -0
- data/guides/assets/images/icons/callouts/13.png +0 -0
- data/guides/assets/images/icons/callouts/15.png +0 -0
- data/guides/assets/images/icons/caution.png +0 -0
- data/guides/assets/images/icons/example.png +0 -0
- data/guides/assets/images/radar.png +0 -0
- data/guides/assets/images/rails4_features.png +0 -0
- data/guides/assets/images/rails_guides_kindle_cover.jpg +0 -0
- data/guides/assets/images/vijaydev.jpg +0 -0
- data/guides/assets/javascripts/guides.js +36 -34
- data/guides/assets/stylesheets/main.css +6 -2
- data/guides/assets/stylesheets/print.css +1 -1
- data/guides/bug_report_templates/action_controller_gem.rb +47 -0
- data/guides/bug_report_templates/action_controller_master.rb +54 -0
- data/guides/bug_report_templates/active_record_gem.rb +5 -2
- data/guides/bug_report_templates/active_record_master.rb +3 -2
- data/guides/bug_report_templates/generic_gem.rb +15 -0
- data/guides/bug_report_templates/generic_master.rb +26 -0
- data/guides/rails_guides.rb +23 -4
- data/guides/rails_guides/generator.rb +1 -1
- data/guides/rails_guides/helpers.rb +4 -2
- data/guides/rails_guides/levenshtein.rb +27 -21
- data/guides/rails_guides/markdown.rb +11 -7
- data/guides/rails_guides/markdown/renderer.rb +1 -1
- data/guides/source/2_2_release_notes.md +3 -3
- data/guides/source/2_3_release_notes.md +12 -12
- data/guides/source/3_0_release_notes.md +10 -13
- data/guides/source/3_1_release_notes.md +7 -4
- data/guides/source/3_2_release_notes.md +17 -14
- data/guides/source/4_0_release_notes.md +110 -54
- data/guides/source/4_1_release_notes.md +730 -0
- data/guides/source/4_2_release_notes.md +877 -0
- data/guides/source/_license.html.erb +1 -1
- data/guides/source/_welcome.html.erb +6 -2
- data/guides/source/action_controller_overview.md +223 -57
- data/guides/source/action_mailer_basics.md +129 -76
- data/guides/source/action_view_overview.md +247 -246
- data/guides/source/active_job_basics.md +339 -0
- data/guides/source/active_model_basics.md +374 -20
- data/guides/source/active_record_basics.md +46 -45
- data/guides/source/active_record_callbacks.md +83 -28
- data/guides/source/{migrations.md → active_record_migrations.md} +191 -275
- data/guides/source/active_record_postgresql.md +433 -0
- data/guides/source/active_record_querying.md +382 -300
- data/guides/source/active_record_validations.md +64 -55
- data/guides/source/active_support_core_extensions.md +229 -187
- data/guides/source/active_support_instrumentation.md +23 -22
- data/guides/source/api_documentation_guidelines.md +167 -15
- data/guides/source/asset_pipeline.md +768 -294
- data/guides/source/association_basics.md +188 -96
- data/guides/source/autoloading_and_reloading_constants.md +1311 -0
- data/guides/source/caching_with_rails.md +45 -11
- data/guides/source/command_line.md +96 -65
- data/guides/source/configuring.md +404 -70
- data/guides/source/contributing_to_ruby_on_rails.md +270 -130
- data/guides/source/credits.html.erb +7 -3
- data/guides/source/debugging_rails_applications.md +471 -284
- data/guides/source/development_dependencies_install.md +115 -21
- data/guides/source/documents.yaml +31 -9
- data/guides/source/engines.md +737 -291
- data/guides/source/form_helpers.md +137 -89
- data/guides/source/generators.md +60 -28
- data/guides/source/getting_started.md +1007 -596
- data/guides/source/i18n.md +178 -96
- data/guides/source/index.html.erb +2 -1
- data/guides/source/initialization.md +248 -104
- data/guides/source/kindle/toc.html.erb +1 -1
- data/guides/source/layout.html.erb +14 -22
- data/guides/source/layouts_and_rendering.md +78 -46
- data/guides/source/maintenance_policy.md +78 -0
- data/guides/source/nested_model_forms.md +10 -7
- data/guides/source/plugins.md +66 -57
- data/guides/source/rails_application_templates.md +49 -12
- data/guides/source/rails_on_rack.md +50 -60
- data/guides/source/routing.md +190 -139
- data/guides/source/ruby_on_rails_guides_guidelines.md +12 -13
- data/guides/source/security.md +134 -83
- data/guides/source/testing.md +322 -200
- data/guides/source/upgrading_ruby_on_rails.md +834 -37
- data/guides/source/working_with_javascript_in_rails.md +36 -26
- data/guides/w3c_validator.rb +2 -0
- metadata +93 -116
- data/guides/assets/images/getting_started/forbidden_attributes_for_new_post.png +0 -0
- data/guides/assets/images/getting_started/new_post.png +0 -0
- data/guides/assets/images/getting_started/post_with_comments.png +0 -0
- data/guides/assets/images/getting_started/show_action_for_posts.png +0 -0
- data/guides/assets/images/getting_started/template_is_missing_posts_new.png +0 -0
- data/guides/assets/images/getting_started/undefined_method_post_path.png +0 -0
- data/guides/assets/images/getting_started/unknown_action_create_for_posts.png +0 -0
- data/guides/assets/images/getting_started/unknown_action_new_for_posts.png +0 -0
- data/guides/assets/images/jaimeiniesta.jpg +0 -0
- data/guides/code/getting_started/Gemfile +0 -43
- data/guides/code/getting_started/Gemfile.lock +0 -150
- data/guides/code/getting_started/README.rdoc +0 -28
- data/guides/code/getting_started/Rakefile +0 -6
- data/guides/code/getting_started/app/assets/javascripts/application.js +0 -16
- data/guides/code/getting_started/app/assets/javascripts/comments.js.coffee +0 -3
- data/guides/code/getting_started/app/assets/javascripts/posts.js.coffee +0 -3
- data/guides/code/getting_started/app/assets/javascripts/welcome.js.coffee +0 -3
- data/guides/code/getting_started/app/assets/stylesheets/application.css +0 -13
- data/guides/code/getting_started/app/assets/stylesheets/comments.css.scss +0 -3
- data/guides/code/getting_started/app/assets/stylesheets/posts.css.scss +0 -3
- data/guides/code/getting_started/app/assets/stylesheets/welcome.css.scss +0 -3
- data/guides/code/getting_started/app/controllers/application_controller.rb +0 -5
- data/guides/code/getting_started/app/controllers/comments_controller.rb +0 -17
- data/guides/code/getting_started/app/controllers/posts_controller.rb +0 -47
- data/guides/code/getting_started/app/controllers/welcome_controller.rb +0 -4
- data/guides/code/getting_started/app/helpers/application_helper.rb +0 -2
- data/guides/code/getting_started/app/helpers/comments_helper.rb +0 -2
- data/guides/code/getting_started/app/helpers/posts_helper.rb +0 -2
- data/guides/code/getting_started/app/helpers/welcome_helper.rb +0 -2
- data/guides/code/getting_started/app/models/comment.rb +0 -3
- data/guides/code/getting_started/app/models/post.rb +0 -7
- data/guides/code/getting_started/app/views/comments/_comment.html.erb +0 -15
- data/guides/code/getting_started/app/views/comments/_form.html.erb +0 -13
- data/guides/code/getting_started/app/views/layouts/application.html.erb +0 -14
- data/guides/code/getting_started/app/views/posts/_form.html.erb +0 -27
- data/guides/code/getting_started/app/views/posts/edit.html.erb +0 -5
- data/guides/code/getting_started/app/views/posts/index.html.erb +0 -21
- data/guides/code/getting_started/app/views/posts/new.html.erb +0 -5
- data/guides/code/getting_started/app/views/posts/show.html.erb +0 -18
- data/guides/code/getting_started/app/views/welcome/index.html.erb +0 -3
- data/guides/code/getting_started/bin/bundle +0 -4
- data/guides/code/getting_started/bin/rails +0 -4
- data/guides/code/getting_started/bin/rake +0 -4
- data/guides/code/getting_started/config.ru +0 -4
- data/guides/code/getting_started/config/application.rb +0 -18
- data/guides/code/getting_started/config/boot.rb +0 -4
- data/guides/code/getting_started/config/database.yml +0 -25
- data/guides/code/getting_started/config/environment.rb +0 -5
- data/guides/code/getting_started/config/environments/development.rb +0 -30
- data/guides/code/getting_started/config/environments/production.rb +0 -80
- data/guides/code/getting_started/config/environments/test.rb +0 -36
- data/guides/code/getting_started/config/initializers/backtrace_silencers.rb +0 -7
- data/guides/code/getting_started/config/initializers/filter_parameter_logging.rb +0 -4
- data/guides/code/getting_started/config/initializers/inflections.rb +0 -16
- data/guides/code/getting_started/config/initializers/locale.rb +0 -9
- data/guides/code/getting_started/config/initializers/mime_types.rb +0 -5
- data/guides/code/getting_started/config/initializers/secret_token.rb +0 -12
- data/guides/code/getting_started/config/initializers/session_store.rb +0 -3
- data/guides/code/getting_started/config/initializers/wrap_parameters.rb +0 -14
- data/guides/code/getting_started/config/locales/en.yml +0 -23
- data/guides/code/getting_started/config/routes.rb +0 -7
- data/guides/code/getting_started/db/migrate/20130122042648_create_posts.rb +0 -10
- data/guides/code/getting_started/db/migrate/20130122045842_create_comments.rb +0 -11
- data/guides/code/getting_started/db/schema.rb +0 -33
- data/guides/code/getting_started/db/seeds.rb +0 -7
- data/guides/code/getting_started/public/404.html +0 -58
- data/guides/code/getting_started/public/422.html +0 -58
- data/guides/code/getting_started/public/500.html +0 -57
- data/guides/code/getting_started/public/favicon.ico +0 -0
- data/guides/code/getting_started/public/robots.txt +0 -5
- data/guides/code/getting_started/test/controllers/comments_controller_test.rb +0 -7
- data/guides/code/getting_started/test/controllers/posts_controller_test.rb +0 -7
- data/guides/code/getting_started/test/controllers/welcome_controller_test.rb +0 -9
- data/guides/code/getting_started/test/fixtures/comments.yml +0 -11
- data/guides/code/getting_started/test/fixtures/posts.yml +0 -9
- data/guides/code/getting_started/test/helpers/comments_helper_test.rb +0 -4
- data/guides/code/getting_started/test/helpers/posts_helper_test.rb +0 -4
- data/guides/code/getting_started/test/helpers/welcome_helper_test.rb +0 -4
- data/guides/code/getting_started/test/models/comment_test.rb +0 -7
- data/guides/code/getting_started/test/models/post_test.rb +0 -7
- data/guides/code/getting_started/test/test_helper.rb +0 -15
- data/guides/source/kindle/KINDLE.md +0 -26
@@ -28,15 +28,15 @@ config.action_controller.perform_caching = true
|
|
28
28
|
|
29
29
|
### Page Caching
|
30
30
|
|
31
|
-
Page caching is a Rails mechanism which allows the request for a generated page to be fulfilled by the webserver (i.e. Apache or
|
31
|
+
Page caching is a Rails mechanism which allows the request for a generated page to be fulfilled by the webserver (i.e. Apache or NGINX), without ever having to go through the Rails stack at all. Obviously, this is super-fast. Unfortunately, it can't be applied to every situation (such as pages that need authentication) and since the webserver is literally just serving a file from the filesystem, cache expiration is an issue that needs to be dealt with.
|
32
32
|
|
33
|
-
INFO: Page Caching has been removed from Rails 4. See the [actionpack-page_caching gem](https://github.com/rails/actionpack-page_caching). See [DHH's key-based cache expiration overview](http://
|
33
|
+
INFO: Page Caching has been removed from Rails 4. See the [actionpack-page_caching gem](https://github.com/rails/actionpack-page_caching). See [DHH's key-based cache expiration overview](http://signalvnoise.com/posts/3113-how-key-based-cache-expiration-works) for the newly-preferred method.
|
34
34
|
|
35
35
|
### Action Caching
|
36
36
|
|
37
37
|
Page Caching cannot be used for actions that have before filters - for example, pages that require authentication. This is where Action Caching comes in. Action Caching works like Page Caching except the incoming web request hits the Rails stack so that before filters can be run on it before the cache is served. This allows authentication and other restrictions to be run while still serving the result of the output from a cached copy.
|
38
38
|
|
39
|
-
INFO: Action Caching has been removed from Rails 4. See the [actionpack-action_caching gem](https://github.com/rails/actionpack-action_caching). See [DHH's key-based cache expiration overview](http://
|
39
|
+
INFO: Action Caching has been removed from Rails 4. See the [actionpack-action_caching gem](https://github.com/rails/actionpack-action_caching). See [DHH's key-based cache expiration overview](http://signalvnoise.com/posts/3113-how-key-based-cache-expiration-works) for the newly-preferred method.
|
40
40
|
|
41
41
|
### Fragment Caching
|
42
42
|
|
@@ -104,6 +104,15 @@ This method generates a cache key that depends on all products and can be used i
|
|
104
104
|
All available products:
|
105
105
|
<% end %>
|
106
106
|
```
|
107
|
+
|
108
|
+
If you want to cache a fragment under certain condition you can use `cache_if` or `cache_unless`
|
109
|
+
|
110
|
+
```erb
|
111
|
+
<% cache_if (condition, cache_key_for_products) do %>
|
112
|
+
All available products:
|
113
|
+
<% end %>
|
114
|
+
```
|
115
|
+
|
107
116
|
You can also use an Active Record model as the cache key:
|
108
117
|
|
109
118
|
```erb
|
@@ -131,6 +140,26 @@ You can also combine the two schemes which is called "Russian Doll Caching":
|
|
131
140
|
|
132
141
|
It's called "Russian Doll Caching" because it nests multiple fragments. The advantage is that if a single product is updated, all the other inner fragments can be reused when regenerating the outer fragment.
|
133
142
|
|
143
|
+
### Low-Level Caching
|
144
|
+
|
145
|
+
Sometimes you need to cache a particular value or query result, instead of caching view fragments. Rails caching mechanism works great for storing __any__ kind of information.
|
146
|
+
|
147
|
+
The most efficient way to implement low-level caching is using the `Rails.cache.fetch` method. This method does both reading and writing to the cache. When passed only a single argument, the key is fetched and value from the cache is returned. If a block is passed, the result of the block will be cached to the given key and the result is returned.
|
148
|
+
|
149
|
+
Consider the following example. An application has a `Product` model with an instance method that looks up the product’s price on a competing website. The data returned by this method would be perfect for low-level caching:
|
150
|
+
|
151
|
+
```ruby
|
152
|
+
class Product < ActiveRecord::Base
|
153
|
+
def competing_price
|
154
|
+
Rails.cache.fetch("#{cache_key}/competing_price", expires_in: 12.hours) do
|
155
|
+
Competitor::API.find_price(id)
|
156
|
+
end
|
157
|
+
end
|
158
|
+
end
|
159
|
+
```
|
160
|
+
|
161
|
+
NOTE: Notice that in this example we used `cache_key` method, so the resulting cache-key will be something like `products/233-20140225082222765838000/competing_price`. `cache_key` generates a string based on the model’s `id` and `updated_at` attributes. This is a common convention and has the benefit of invalidating the cache whenever the product is updated. In general, when you use low-level caching for instance level information, you need to generate a cache key.
|
162
|
+
|
134
163
|
### SQL Caching
|
135
164
|
|
136
165
|
Query caching is a Rails feature that caches the result set returned by each query so that if Rails encounters the same query again for that request, it will use the cached result set as opposed to running the query against the database again.
|
@@ -156,7 +185,7 @@ end
|
|
156
185
|
Cache Stores
|
157
186
|
------------
|
158
187
|
|
159
|
-
Rails provides different stores for the cached data created by
|
188
|
+
Rails provides different stores for the cached data created by **action** and **fragment** caches.
|
160
189
|
|
161
190
|
TIP: Page caches are always stored on disk.
|
162
191
|
|
@@ -180,7 +209,7 @@ The main methods to call are `read`, `write`, `delete`, `exist?`, and `fetch`. T
|
|
180
209
|
|
181
210
|
There are some common options used by all cache implementations. These can be passed to the constructor or the various methods to interact with entries.
|
182
211
|
|
183
|
-
* `:namespace` - This option can be used to create a namespace within the cache store. It is especially useful if your application shares a cache with other applications.
|
212
|
+
* `:namespace` - This option can be used to create a namespace within the cache store. It is especially useful if your application shares a cache with other applications.
|
184
213
|
|
185
214
|
* `:compress` - This option can be used to indicate that compression should be used in the cache. This can be useful for transferring large cache entries over a slow network.
|
186
215
|
|
@@ -216,7 +245,7 @@ This is the default cache store implementation.
|
|
216
245
|
|
217
246
|
### ActiveSupport::Cache::MemCacheStore
|
218
247
|
|
219
|
-
This cache store uses Danga's `memcached` server to provide a centralized cache for your application. Rails uses the bundled `dalli` gem by default. This is currently the most popular cache store for production websites. It can be used to provide a single, shared cache cluster with very
|
248
|
+
This cache store uses Danga's `memcached` server to provide a centralized cache for your application. Rails uses the bundled `dalli` gem by default. This is currently the most popular cache store for production websites. It can be used to provide a single, shared cache cluster with very high performance and redundancy.
|
220
249
|
|
221
250
|
When initializing the cache, you need to specify the addresses for all memcached servers in your cluster. If none is specified, it will assume memcached is running on the local host on the default port, but this is not an ideal set up for larger sites.
|
222
251
|
|
@@ -236,7 +265,7 @@ config.cache_store = :ehcache_store
|
|
236
265
|
|
237
266
|
When initializing the cache, you may use the `:ehcache_config` option to specify the Ehcache config file to use (where the default is "ehcache.xml" in your Rails config directory), and the :cache_name option to provide a custom name for your cache (the default is rails_cache).
|
238
267
|
|
239
|
-
In addition to the standard `:expires_in` option, the `write` method on this cache can also accept the additional
|
268
|
+
In addition to the standard `:expires_in` option, the `write` method on this cache can also accept the additional `:unless_exist` option, which will cause the cache store to use Ehcache's `putIfAbsent` method instead of `put`, and therefore will not overwrite an existing entry. Additionally, the `write` method supports all of the properties exposed by the [Ehcache Element class](http://ehcache.org/apidocs/net/sf/ehcache/Element.html) , including:
|
240
269
|
|
241
270
|
| Property | Argument Type | Description |
|
242
271
|
| --------------------------- | ------------------- | ----------------------------------------------------------- |
|
@@ -292,7 +321,7 @@ Conditional GET support
|
|
292
321
|
|
293
322
|
Conditional GETs are a feature of the HTTP specification that provide a way for web servers to tell browsers that the response to a GET request hasn't changed since the last request and can be safely pulled from the browser cache.
|
294
323
|
|
295
|
-
They work by using the `HTTP_IF_NONE_MATCH` and `HTTP_IF_MODIFIED_SINCE` headers to pass back and forth both a unique content identifier and the timestamp of when the content was last changed. If the browser makes a request where the content identifier (etag) or last modified since timestamp matches the server
|
324
|
+
They work by using the `HTTP_IF_NONE_MATCH` and `HTTP_IF_MODIFIED_SINCE` headers to pass back and forth both a unique content identifier and the timestamp of when the content was last changed. If the browser makes a request where the content identifier (etag) or last modified since timestamp matches the server's version then the server only needs to send back an empty response with a not modified status.
|
296
325
|
|
297
326
|
It is the server's (i.e. our) responsibility to look for a last modified timestamp and the if-none-match header and determine whether or not to send back the full response. With conditional-get support in Rails this is a pretty easy task:
|
298
327
|
|
@@ -318,18 +347,23 @@ class ProductsController < ApplicationController
|
|
318
347
|
end
|
319
348
|
```
|
320
349
|
|
321
|
-
Instead of
|
350
|
+
Instead of an options hash, you can also simply pass in a model, Rails will use the `updated_at` and `cache_key` methods for setting `last_modified` and `etag`:
|
322
351
|
|
323
352
|
```ruby
|
324
353
|
class ProductsController < ApplicationController
|
325
354
|
def show
|
326
355
|
@product = Product.find(params[:id])
|
327
|
-
|
356
|
+
|
357
|
+
if stale?(@product)
|
358
|
+
respond_to do |wants|
|
359
|
+
# ... normal response processing
|
360
|
+
end
|
361
|
+
end
|
328
362
|
end
|
329
363
|
end
|
330
364
|
```
|
331
365
|
|
332
|
-
If you don't have any special response processing and are using the default rendering mechanism (i.e. you're not using respond_to or calling render yourself) then you
|
366
|
+
If you don't have any special response processing and are using the default rendering mechanism (i.e. you're not using `respond_to` or calling render yourself) then you've got an easy helper in `fresh_when`:
|
333
367
|
|
334
368
|
```ruby
|
335
369
|
class ProductsController < ApplicationController
|
@@ -1,15 +1,12 @@
|
|
1
1
|
The Rails Command Line
|
2
2
|
======================
|
3
3
|
|
4
|
-
Rails comes with every command line tool you'll need to
|
5
|
-
|
6
4
|
After reading this guide, you will know:
|
7
5
|
|
8
6
|
* How to create a Rails application.
|
9
7
|
* How to generate models, controllers, database migrations, and unit tests.
|
10
8
|
* How to start a development server.
|
11
9
|
* How to experiment with objects through an interactive shell.
|
12
|
-
* How to profile and benchmark your new creation.
|
13
10
|
|
14
11
|
--------------------------------------------------------------------------------
|
15
12
|
|
@@ -27,6 +24,8 @@ There are a few commands that are absolutely critical to your everyday usage of
|
|
27
24
|
* `rails dbconsole`
|
28
25
|
* `rails new app_name`
|
29
26
|
|
27
|
+
All commands can run with ```-h or --help``` to list more information.
|
28
|
+
|
30
29
|
Let's create a simple Rails application to step through each of these commands in context.
|
31
30
|
|
32
31
|
### `rails new`
|
@@ -56,20 +55,18 @@ Rails will set you up with what seems like a huge amount of stuff for such a tin
|
|
56
55
|
|
57
56
|
The `rails server` command launches a small web server named WEBrick which comes bundled with Ruby. You'll use this any time you want to access your application through a web browser.
|
58
57
|
|
59
|
-
INFO: WEBrick isn't your only option for serving Rails. We'll get to that [later](#server-with-different-backends).
|
60
|
-
|
61
58
|
With no further work, `rails server` will run our new shiny Rails app:
|
62
59
|
|
63
60
|
```bash
|
64
61
|
$ cd commandsapp
|
65
|
-
$ rails server
|
62
|
+
$ bin/rails server
|
66
63
|
=> Booting WEBrick
|
67
|
-
=> Rails
|
64
|
+
=> Rails 4.2.0 application starting in development on http://localhost:3000
|
68
65
|
=> Call with -d to detach
|
69
66
|
=> Ctrl-C to shutdown server
|
70
|
-
[
|
71
|
-
[
|
72
|
-
[
|
67
|
+
[2013-08-07 02:00:01] INFO WEBrick 1.3.1
|
68
|
+
[2013-08-07 02:00:01] INFO ruby 2.0.0 (2013-06-27) [x86_64-darwin11.2.0]
|
69
|
+
[2013-08-07 02:00:01] INFO WEBrick::HTTPServer#start: pid=69680 port=3000
|
73
70
|
```
|
74
71
|
|
75
72
|
With just three commands we whipped up a Rails server listening on port 3000. Go to your browser and open [http://localhost:3000](http://localhost:3000), you will see a basic Rails app running.
|
@@ -79,10 +76,10 @@ INFO: You can also use the alias "s" to start the server: `rails s`.
|
|
79
76
|
The server can be run on a different port using the `-p` option. The default development environment can be changed using `-e`.
|
80
77
|
|
81
78
|
```bash
|
82
|
-
$ rails server -e production -p 4000
|
79
|
+
$ bin/rails server -e production -p 4000
|
83
80
|
```
|
84
81
|
|
85
|
-
The `-b` option binds Rails to the specified IP, by default it is
|
82
|
+
The `-b` option binds Rails to the specified IP, by default it is localhost. You can run a server as a daemon by passing a `-d` option.
|
86
83
|
|
87
84
|
### `rails generate`
|
88
85
|
|
@@ -91,7 +88,7 @@ The `rails generate` command uses templates to create a whole lot of things. Run
|
|
91
88
|
INFO: You can also use the alias "g" to invoke the generator command: `rails g`.
|
92
89
|
|
93
90
|
```bash
|
94
|
-
$ rails generate
|
91
|
+
$ bin/rails generate
|
95
92
|
Usage: rails generate GENERATOR [args] [options]
|
96
93
|
|
97
94
|
...
|
@@ -116,7 +113,7 @@ Let's make our own controller with the controller generator. But what command sh
|
|
116
113
|
INFO: All Rails console utilities have help text. As with most *nix utilities, you can try adding `--help` or `-h` to the end, for example `rails server --help`.
|
117
114
|
|
118
115
|
```bash
|
119
|
-
$ rails generate controller
|
116
|
+
$ bin/rails generate controller
|
120
117
|
Usage: rails generate controller NAME [action action] [options]
|
121
118
|
|
122
119
|
...
|
@@ -125,25 +122,24 @@ Usage: rails generate controller NAME [action action] [options]
|
|
125
122
|
Description:
|
126
123
|
...
|
127
124
|
|
128
|
-
To create a controller within a module, specify the controller name as a
|
129
|
-
path like 'parent_module/controller_name'.
|
125
|
+
To create a controller within a module, specify the controller name as a path like 'parent_module/controller_name'.
|
130
126
|
|
131
127
|
...
|
132
128
|
|
133
129
|
Example:
|
134
|
-
`rails generate controller
|
130
|
+
`rails generate controller CreditCards open debit credit close`
|
135
131
|
|
136
|
-
Credit card controller with URLs like /
|
137
|
-
Controller: app/controllers/
|
138
|
-
Test: test/controllers/
|
139
|
-
Views: app/views/
|
140
|
-
Helper: app/helpers/
|
132
|
+
Credit card controller with URLs like /credit_cards/debit.
|
133
|
+
Controller: app/controllers/credit_cards_controller.rb
|
134
|
+
Test: test/controllers/credit_cards_controller_test.rb
|
135
|
+
Views: app/views/credit_cards/debit.html.erb [...]
|
136
|
+
Helper: app/helpers/credit_cards_helper.rb
|
141
137
|
```
|
142
138
|
|
143
139
|
The controller generator is expecting parameters in the form of `generate controller ControllerName action1 action2`. Let's make a `Greetings` controller with an action of **hello**, which will say something nice to us.
|
144
140
|
|
145
141
|
```bash
|
146
|
-
$ rails generate controller Greetings hello
|
142
|
+
$ bin/rails generate controller Greetings hello
|
147
143
|
create app/controllers/greetings_controller.rb
|
148
144
|
route get "greetings/hello"
|
149
145
|
invoke erb
|
@@ -153,8 +149,6 @@ $ rails generate controller Greetings hello
|
|
153
149
|
create test/controllers/greetings_controller_test.rb
|
154
150
|
invoke helper
|
155
151
|
create app/helpers/greetings_helper.rb
|
156
|
-
invoke test_unit
|
157
|
-
create test/helpers/greetings_helper_test.rb
|
158
152
|
invoke assets
|
159
153
|
invoke coffee
|
160
154
|
create app/assets/javascripts/greetings.js.coffee
|
@@ -184,7 +178,7 @@ Then the view, to display our message (in `app/views/greetings/hello.html.erb`):
|
|
184
178
|
Fire up your server using `rails server`.
|
185
179
|
|
186
180
|
```bash
|
187
|
-
$ rails server
|
181
|
+
$ bin/rails server
|
188
182
|
=> Booting WEBrick...
|
189
183
|
```
|
190
184
|
|
@@ -195,7 +189,7 @@ INFO: With a normal, plain-old Rails application, your URLs will generally follo
|
|
195
189
|
Rails comes with a generator for data models too.
|
196
190
|
|
197
191
|
```bash
|
198
|
-
$ rails generate model
|
192
|
+
$ bin/rails generate model
|
199
193
|
Usage:
|
200
194
|
rails generate model NAME [field[:type][:index] field[:type][:index]] [options]
|
201
195
|
|
@@ -218,9 +212,9 @@ But instead of generating a model directly (which we'll be doing later), let's s
|
|
218
212
|
We will set up a simple resource called "HighScore" that will keep track of our highest score on video games we play.
|
219
213
|
|
220
214
|
```bash
|
221
|
-
$ rails generate scaffold HighScore game:string score:integer
|
215
|
+
$ bin/rails generate scaffold HighScore game:string score:integer
|
222
216
|
invoke active_record
|
223
|
-
create db/migrate/
|
217
|
+
create db/migrate/20130717151933_create_high_scores.rb
|
224
218
|
create app/models/high_score.rb
|
225
219
|
invoke test_unit
|
226
220
|
create test/models/high_score_test.rb
|
@@ -240,23 +234,24 @@ $ rails generate scaffold HighScore game:string score:integer
|
|
240
234
|
create test/controllers/high_scores_controller_test.rb
|
241
235
|
invoke helper
|
242
236
|
create app/helpers/high_scores_helper.rb
|
243
|
-
invoke
|
244
|
-
create
|
237
|
+
invoke jbuilder
|
238
|
+
create app/views/high_scores/index.json.jbuilder
|
239
|
+
create app/views/high_scores/show.json.jbuilder
|
245
240
|
invoke assets
|
246
241
|
invoke coffee
|
247
242
|
create app/assets/javascripts/high_scores.js.coffee
|
248
243
|
invoke scss
|
249
244
|
create app/assets/stylesheets/high_scores.css.scss
|
250
245
|
invoke scss
|
251
|
-
|
246
|
+
identical app/assets/stylesheets/scaffolds.css.scss
|
252
247
|
```
|
253
248
|
|
254
249
|
The generator checks that there exist the directories for models, controllers, helpers, layouts, functional and unit tests, stylesheets, creates the views, controller, model and database migration for HighScore (creating the `high_scores` table and fields), takes care of the route for the **resource**, and new tests for everything.
|
255
250
|
|
256
|
-
The migration requires that we **migrate**, that is, run some Ruby code (living in that `
|
251
|
+
The migration requires that we **migrate**, that is, run some Ruby code (living in that `20130717151933_create_high_scores.rb`) to modify the schema of our database. Which database? The SQLite3 database that Rails will create for you when we run the `rake db:migrate` command. We'll talk more about Rake in-depth in a little while.
|
257
252
|
|
258
253
|
```bash
|
259
|
-
$ rake db:migrate
|
254
|
+
$ bin/rake db:migrate
|
260
255
|
== CreateHighScores: migrating ===============================================
|
261
256
|
-- create_table(:high_scores)
|
262
257
|
-> 0.0017s
|
@@ -268,7 +263,7 @@ INFO: Let's talk about unit tests. Unit tests are code that tests and makes asse
|
|
268
263
|
Let's see the interface Rails created for us.
|
269
264
|
|
270
265
|
```bash
|
271
|
-
$ rails server
|
266
|
+
$ bin/rails server
|
272
267
|
```
|
273
268
|
|
274
269
|
Go to your browser and open [http://localhost:3000/high_scores](http://localhost:3000/high_scores), now we can create new high scores (55,160 on Space Invaders!)
|
@@ -282,18 +277,43 @@ INFO: You can also use the alias "c" to invoke the console: `rails c`.
|
|
282
277
|
You can specify the environment in which the `console` command should operate.
|
283
278
|
|
284
279
|
```bash
|
285
|
-
$ rails console staging
|
280
|
+
$ bin/rails console staging
|
286
281
|
```
|
287
282
|
|
288
283
|
If you wish to test out some code without changing any data, you can do that by invoking `rails console --sandbox`.
|
289
284
|
|
290
285
|
```bash
|
291
|
-
$ rails console --sandbox
|
292
|
-
Loading development environment in sandbox (Rails
|
286
|
+
$ bin/rails console --sandbox
|
287
|
+
Loading development environment in sandbox (Rails 4.2.0)
|
293
288
|
Any modifications you make will be rolled back on exit
|
294
289
|
irb(main):001:0>
|
295
290
|
```
|
296
291
|
|
292
|
+
#### The app and helper objects
|
293
|
+
|
294
|
+
Inside the `rails console` you have access to the `app` and `helper` instances.
|
295
|
+
|
296
|
+
With the `app` method you can access url and path helpers, as well as do requests.
|
297
|
+
|
298
|
+
```bash
|
299
|
+
>> app.root_path
|
300
|
+
=> "/"
|
301
|
+
|
302
|
+
>> app.get _
|
303
|
+
Started GET "/" for 127.0.0.1 at 2014-06-19 10:41:57 -0300
|
304
|
+
...
|
305
|
+
```
|
306
|
+
|
307
|
+
With the `helper` method it is possible to access Rails and your application's helpers.
|
308
|
+
|
309
|
+
```bash
|
310
|
+
>> helper.time_ago_in_words 30.days.ago
|
311
|
+
=> "about 1 month"
|
312
|
+
|
313
|
+
>> helper.my_custom_helper
|
314
|
+
=> "my custom helper"
|
315
|
+
```
|
316
|
+
|
297
317
|
### `rails dbconsole`
|
298
318
|
|
299
319
|
`rails dbconsole` figures out which database you're using and drops you into whichever command line interface you would use with it (and figures out the command line parameters to give to it, too!). It supports MySQL, PostgreSQL, SQLite and SQLite3.
|
@@ -305,7 +325,7 @@ INFO: You can also use the alias "db" to invoke the dbconsole: `rails db`.
|
|
305
325
|
`runner` runs Ruby code in the context of Rails non-interactively. For instance:
|
306
326
|
|
307
327
|
```bash
|
308
|
-
$ rails runner "Model.long_running_method"
|
328
|
+
$ bin/rails runner "Model.long_running_method"
|
309
329
|
```
|
310
330
|
|
311
331
|
INFO: You can also use the alias "r" to invoke the runner: `rails r`.
|
@@ -313,7 +333,7 @@ INFO: You can also use the alias "r" to invoke the runner: `rails r`.
|
|
313
333
|
You can specify the environment in which the `runner` command should operate using the `-e` switch.
|
314
334
|
|
315
335
|
```bash
|
316
|
-
$ rails runner -e staging "Model.long_running_method"
|
336
|
+
$ bin/rails runner -e staging "Model.long_running_method"
|
317
337
|
```
|
318
338
|
|
319
339
|
### `rails destroy`
|
@@ -323,7 +343,7 @@ Think of `destroy` as the opposite of `generate`. It'll figure out what generate
|
|
323
343
|
INFO: You can also use the alias "d" to invoke the destroy command: `rails d`.
|
324
344
|
|
325
345
|
```bash
|
326
|
-
$ rails generate model Oops
|
346
|
+
$ bin/rails generate model Oops
|
327
347
|
invoke active_record
|
328
348
|
create db/migrate/20120528062523_create_oops.rb
|
329
349
|
create app/models/oops.rb
|
@@ -332,7 +352,7 @@ $ rails generate model Oops
|
|
332
352
|
create test/fixtures/oops.yml
|
333
353
|
```
|
334
354
|
```bash
|
335
|
-
$ rails destroy model Oops
|
355
|
+
$ bin/rails destroy model Oops
|
336
356
|
invoke active_record
|
337
357
|
remove db/migrate/20120528062523_create_oops.rb
|
338
358
|
remove app/models/oops.rb
|
@@ -348,10 +368,13 @@ Rake is Ruby Make, a standalone Ruby utility that replaces the Unix utility 'mak
|
|
348
368
|
|
349
369
|
You can get a list of Rake tasks available to you, which will often depend on your current directory, by typing `rake --tasks`. Each task has a description, and should help you find the thing you need.
|
350
370
|
|
371
|
+
To get the full backtrace for running rake task you can pass the option ```--trace``` to command line, for example ```rake db:create --trace```.
|
372
|
+
|
351
373
|
```bash
|
352
|
-
$ rake --tasks
|
374
|
+
$ bin/rake --tasks
|
353
375
|
rake about # List versions of all Rails frameworks and the environment
|
354
|
-
rake assets:clean # Remove compiled assets
|
376
|
+
rake assets:clean # Remove old compiled assets
|
377
|
+
rake assets:clobber # Remove compiled assets
|
355
378
|
rake assets:precompile # Compile all the assets named in config.assets.precompile
|
356
379
|
rake db:create # Create the database from config/database.yml for the current Rails.env
|
357
380
|
...
|
@@ -361,24 +384,21 @@ rake middleware # Prints out your Rack middleware stack
|
|
361
384
|
rake tmp:clear # Clear session, cache, and socket files from tmp/ (narrow w/ tmp:sessions:clear, tmp:cache:clear, tmp:sockets:clear)
|
362
385
|
rake tmp:create # Creates tmp directories for sessions, cache, sockets, and pids
|
363
386
|
```
|
387
|
+
INFO: You can also use ```rake -T``` to get the list of tasks.
|
364
388
|
|
365
389
|
### `about`
|
366
390
|
|
367
391
|
`rake about` gives information about version numbers for Ruby, RubyGems, Rails, the Rails subcomponents, your application's folder, the current Rails environment name, your app's database adapter, and schema version. It is useful when you need to ask for help, check if a security patch might affect you, or when you need some stats for an existing Rails installation.
|
368
392
|
|
369
393
|
```bash
|
370
|
-
$ rake about
|
394
|
+
$ bin/rake about
|
371
395
|
About your application's environment
|
396
|
+
Rails version 4.2.0
|
372
397
|
Ruby version 1.9.3 (x86_64-linux)
|
373
398
|
RubyGems version 1.3.6
|
374
399
|
Rack version 1.3
|
375
|
-
Rails version 4.0.0.beta
|
376
400
|
JavaScript Runtime Node.js (V8)
|
377
|
-
|
378
|
-
Action Pack version 4.0.0.beta
|
379
|
-
Action Mailer version 4.0.0.beta
|
380
|
-
Active Support version 4.0.0.beta
|
381
|
-
Middleware ActionDispatch::Static, Rack::Lock, Rack::Runtime, Rack::MethodOverride, ActionDispatch::RequestId, Rails::Rack::Logger, ActionDispatch::ShowExceptions, ActionDispatch::DebugExceptions, ActionDispatch::RemoteIp, ActionDispatch::Reloader, ActionDispatch::Callbacks, ActiveRecord::Migration::CheckPending, ActiveRecord::ConnectionAdapters::ConnectionManagement, ActiveRecord::QueryCache, ActionDispatch::Cookies, ActionDispatch::Session::EncryptedCookieStore, ActionDispatch::Flash, ActionDispatch::ParamsParser, Rack::Head, Rack::ConditionalGet, Rack::ETag
|
401
|
+
Middleware Rack::Sendfile, ActionDispatch::Static, Rack::Lock, #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x007ffd131a7c88>, Rack::Runtime, Rack::MethodOverride, ActionDispatch::RequestId, Rails::Rack::Logger, ActionDispatch::ShowExceptions, ActionDispatch::DebugExceptions, ActionDispatch::RemoteIp, ActionDispatch::Reloader, ActionDispatch::Callbacks, ActiveRecord::Migration::CheckPending, ActiveRecord::ConnectionAdapters::ConnectionManagement, ActiveRecord::QueryCache, ActionDispatch::Cookies, ActionDispatch::Session::CookieStore, ActionDispatch::Flash, ActionDispatch::ParamsParser, Rack::Head, Rack::ConditionalGet, Rack::ETag
|
382
402
|
Application root /home/foobar/commandsapp
|
383
403
|
Environment development
|
384
404
|
Database adapter sqlite3
|
@@ -387,7 +407,9 @@ Database schema version 20110805173523
|
|
387
407
|
|
388
408
|
### `assets`
|
389
409
|
|
390
|
-
You can precompile the assets in `app/assets` using `rake assets:precompile
|
410
|
+
You can precompile the assets in `app/assets` using `rake assets:precompile`, and remove older compiled assets using `rake assets:clean`. The `assets:clean` task allows for rolling deploys that may still be linking to an old asset while the new assets are being built.
|
411
|
+
|
412
|
+
If you want to clear `public/assets` completely, you can use `rake assets:clobber`.
|
391
413
|
|
392
414
|
### `db`
|
393
415
|
|
@@ -405,10 +427,10 @@ The `doc:` namespace has the tools to generate documentation for your app, API d
|
|
405
427
|
|
406
428
|
### `notes`
|
407
429
|
|
408
|
-
`rake notes` will search through your code for comments beginning with FIXME, OPTIMIZE or TODO. The search is done in files with extension `.builder`, `.rb`, `.
|
430
|
+
`rake notes` will search through your code for comments beginning with FIXME, OPTIMIZE or TODO. The search is done in files with extension `.builder`, `.rb`, `.rake`, `.yml`, `.yaml`, `.ruby`, `.css`, `.js` and `.erb` for both default and custom annotations.
|
409
431
|
|
410
432
|
```bash
|
411
|
-
$ rake notes
|
433
|
+
$ bin/rake notes
|
412
434
|
(in /home/foobar/commandsapp)
|
413
435
|
app/controllers/admin/users_controller.rb:
|
414
436
|
* [ 20] [TODO] any other way to do this?
|
@@ -419,10 +441,16 @@ app/models/school.rb:
|
|
419
441
|
* [ 17] [FIXME]
|
420
442
|
```
|
421
443
|
|
444
|
+
You can add support for new file extensions using `config.annotations.register_extensions` option, which receives a list of the extensions with its corresponding regex to match it up.
|
445
|
+
|
446
|
+
```ruby
|
447
|
+
config.annotations.register_extensions("scss", "sass", "less") { |annotation| /\/\/\s*(#{annotation}):?\s*(.*)$/ }
|
448
|
+
```
|
449
|
+
|
422
450
|
If you are looking for a specific annotation, say FIXME, you can use `rake notes:fixme`. Note that you have to lower case the annotation's name.
|
423
451
|
|
424
452
|
```bash
|
425
|
-
$ rake notes:fixme
|
453
|
+
$ bin/rake notes:fixme
|
426
454
|
(in /home/foobar/commandsapp)
|
427
455
|
app/controllers/admin/users_controller.rb:
|
428
456
|
* [132] high priority for next deploy
|
@@ -434,9 +462,9 @@ app/models/school.rb:
|
|
434
462
|
You can also use custom annotations in your code and list them using `rake notes:custom` by specifying the annotation using an environment variable `ANNOTATION`.
|
435
463
|
|
436
464
|
```bash
|
437
|
-
$ rake notes:custom ANNOTATION=BUG
|
465
|
+
$ bin/rake notes:custom ANNOTATION=BUG
|
438
466
|
(in /home/foobar/commandsapp)
|
439
|
-
app/models/
|
467
|
+
app/models/article.rb:
|
440
468
|
* [ 23] Have to fix this one before pushing!
|
441
469
|
```
|
442
470
|
|
@@ -446,7 +474,7 @@ By default, `rake notes` will look in the `app`, `config`, `lib`, `bin` and `tes
|
|
446
474
|
|
447
475
|
```bash
|
448
476
|
$ export SOURCE_ANNOTATION_DIRECTORIES='spec,vendor'
|
449
|
-
$ rake notes
|
477
|
+
$ bin/rake notes
|
450
478
|
(in /home/foobar/commandsapp)
|
451
479
|
app/models/user.rb:
|
452
480
|
* [ 35] [FIXME] User should have a subscription at this point
|
@@ -462,18 +490,19 @@ spec/models/user_spec.rb:
|
|
462
490
|
|
463
491
|
INFO: A good description of unit testing in Rails is given in [A Guide to Testing Rails Applications](testing.html)
|
464
492
|
|
465
|
-
Rails comes with a test suite called
|
493
|
+
Rails comes with a test suite called Minitest. Rails owes its stability to the use of tests. The tasks available in the `test:` namespace helps in running the different tests you will hopefully write.
|
466
494
|
|
467
495
|
### `tmp`
|
468
496
|
|
469
|
-
The `Rails.root/tmp` directory is, like the *nix /tmp directory, the holding place for temporary files like sessions (if you're using a file store for
|
497
|
+
The `Rails.root/tmp` directory is, like the *nix /tmp directory, the holding place for temporary files like sessions (if you're using a file store for sessions), process id files, and cached actions.
|
470
498
|
|
471
|
-
The `tmp:` namespaced tasks will help you clear the `Rails.root/tmp` directory:
|
499
|
+
The `tmp:` namespaced tasks will help you clear and create the `Rails.root/tmp` directory:
|
472
500
|
|
473
501
|
* `rake tmp:cache:clear` clears `tmp/cache`.
|
474
502
|
* `rake tmp:sessions:clear` clears `tmp/sessions`.
|
475
503
|
* `rake tmp:sockets:clear` clears `tmp/sockets`.
|
476
504
|
* `rake tmp:clear` clears all the three: cache, sessions and sockets.
|
505
|
+
* `rake tmp:create` creates tmp directories for sessions, cache, sockets, and pids.
|
477
506
|
|
478
507
|
### Miscellaneous
|
479
508
|
|
@@ -483,7 +512,9 @@ The `tmp:` namespaced tasks will help you clear the `Rails.root/tmp` directory:
|
|
483
512
|
|
484
513
|
### Custom Rake Tasks
|
485
514
|
|
486
|
-
Custom rake tasks have a `.rake` extension and are placed in
|
515
|
+
Custom rake tasks have a `.rake` extension and are placed in
|
516
|
+
`Rails.root/lib/tasks`. You can create these custom rake tasks with the
|
517
|
+
`bin/rails generate task` command.
|
487
518
|
|
488
519
|
```ruby
|
489
520
|
desc "I am short, but comprehensive description for my cool task"
|
@@ -515,9 +546,9 @@ end
|
|
515
546
|
Invocation of the tasks will look like:
|
516
547
|
|
517
548
|
```bash
|
518
|
-
rake task_name
|
519
|
-
rake "task_name[value 1]" # entire argument string should be quoted
|
520
|
-
rake db:nothing
|
549
|
+
$ bin/rake task_name
|
550
|
+
$ bin/rake "task_name[value 1]" # entire argument string should be quoted
|
551
|
+
$ bin/rake db:nothing
|
521
552
|
```
|
522
553
|
|
523
554
|
NOTE: If your need to interact with your application models, perform database queries and so on, your task should depend on the `environment` task, which will load your application code.
|