From 2610797d08c2d356e5a2e33f0d3bf6864adeddfc Mon Sep 17 00:00:00 2001 From: Pablo Torres Date: Sun, 2 Dec 2012 12:46:22 -0500 Subject: Add shallow nesting to the routing guide --- guides/source/routing.md | 85 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) (limited to 'guides/source') diff --git a/guides/source/routing.md b/guides/source/routing.md index 7d43854f2f..d670de616b 100644 --- a/guides/source/routing.md +++ b/guides/source/routing.md @@ -283,6 +283,91 @@ The corresponding route helper would be `publisher_magazine_photo_url`, requirin TIP: _Resources should never be nested more than 1 level deep._ +#### Shallow Nesting + +One way to avoid deep nesting (as recommended above) is to generate the collection actions scoped under the parent, so as to get a sense of the hierarchy, but to not nest the member actions. In other words, to only build routes with the minimal amount of information to uniquely identify the resource, like this: + +```ruby +resources :posts do + resources :comments, only: [:index, :new, :create] +end +resources :comments, only: [:show, :edit, :update, :destroy] +``` + +This idea strikes a balance between descriptive routes and deep nesting. There exists shorthand syntax to achieve just that, via the `:shallow` option: + +```ruby +resources :posts do + resources :comments, shallow: true +end +``` + +This will generate the exact same routes as the first example. You can also specify the `:shallow` option in the parent resource, in which case all of the nested resources will be shallow: + +```ruby +resources :posts, shallow: true do + resources :comments + resources :quotes + resources :drafts +end +``` + +The `shallow` method of the DSL creates a scope inside of which every nesting is shallow. This generates the same routes as the previous example: + +```ruby +shallow do + resources :posts do + resources :comments + resources :quotes + resources :drafts + end +end +``` + +There exists two options for `scope` to customize shallow routes. `:shallow_path` prefixes member paths with the specified parameter: + +```ruby +scope shallow_path: "sekret" do + resources :posts do + resources :comments, shallow: true + end +end +``` + +The comments resource here will have the following routes generated for it: + +| HTTP Verb | Path | Named Helper | +| --------- | -------------------------------------- | ------------------- | +| GET | /posts/:post_id/comments(.:format) | post_comments | +| POST | /posts/:post_id/comments(.:format) | post_comments | +| GET | /posts/:post_id/comments/new(.:format) | new_post_comment | +| GET | /sekret/comments/:id/edit(.:format) | edit_comment | +| GET | /sekret/comments/:id(.:format) | comment | +| PATCH/PUT | /sekret/comments/:id(.:format) | comment | +| DELETE | /sekret/comments/:id(.:format) | comment | + +The `:shallow_prefix` option adds the specified parameter to the named helpers: + +```ruby +scope shallow_prefix: "sekret" do + resources :posts do + resources :comments, shallow: true + end +end +``` + +The comments resource here will have the following routes generated for it: + +| HTTP Verb | Path | Named Helper | +| --------- | -------------------------------------- | ------------------- | +| GET | /posts/:post_id/comments(.:format) | post_comments | +| POST | /posts/:post_id/comments(.:format) | post_comments | +| GET | /posts/:post_id/comments/new(.:format) | new_post_comment | +| GET | /comments/:id/edit(.:format) | edit_sekret_comment | +| GET | /comments/:id(.:format) | sekret_comment | +| PATCH/PUT | /comments/:id(.:format) | sekret_comment | +| DELETE | /comments/:id(.:format) | sekret_comment | + ### Routing concerns Routing Concerns allows you to declare common routes that can be reused inside others resources and routes. -- cgit v1.2.3 From 1ba6cddf89070ec16526ab5e37c9c9fc38ede5bb Mon Sep 17 00:00:00 2001 From: Pablo Torres Date: Sun, 2 Dec 2012 12:49:29 -0500 Subject: List instructions in the order to be executed --- guides/source/ruby_on_rails_guides_guidelines.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'guides/source') diff --git a/guides/source/ruby_on_rails_guides_guidelines.md b/guides/source/ruby_on_rails_guides_guidelines.md index 6e3173cdb4..2c3bc686ef 100644 --- a/guides/source/ruby_on_rails_guides_guidelines.md +++ b/guides/source/ruby_on_rails_guides_guidelines.md @@ -62,7 +62,7 @@ HTML Guides ### Generation -To generate all the guides, just `cd` into the **`guides`** directory and execute: +To generate all the guides, just `cd` into the **`guides`** directory, run `bundle install` and execute: ``` bundle exec rake guides:generate @@ -74,8 +74,6 @@ or bundle exec rake guides:generate:html ``` -(You may need to run `bundle install` first to install the required gems.) - To process `my_guide.md` and nothing else use the `ONLY` environment variable: ``` -- cgit v1.2.3 From e309e7418964728a477aad2fda8b061657fd41df Mon Sep 17 00:00:00 2001 From: Pablo Torres Date: Sun, 2 Dec 2012 14:19:29 -0500 Subject: Capitalize all table column titles in routing guide --- guides/source/routing.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'guides/source') diff --git a/guides/source/routing.md b/guides/source/routing.md index d670de616b..329a0ad8a3 100644 --- a/guides/source/routing.md +++ b/guides/source/routing.md @@ -87,7 +87,7 @@ resources :photos creates seven different routes in your application, all mapping to the `Photos` controller: -| HTTP Verb | Path | action | used for | +| HTTP Verb | Path | Action | Used for | | --------- | ---------------- | ------- | -------------------------------------------- | | GET | /photos | index | display a list of all photos | | GET | /photos/new | new | return an HTML form for creating a new photo | @@ -144,7 +144,7 @@ resource :geocoder creates six different routes in your application, all mapping to the `Geocoders` controller: -| HTTP Verb | Path | action | used for | +| HTTP Verb | Path | Action | Used for | | --------- | -------------- | ------- | --------------------------------------------- | | GET | /geocoder/new | new | return an HTML form for creating the geocoder | | POST | /geocoder | create | create the new geocoder | @@ -175,7 +175,7 @@ end This will create a number of routes for each of the `posts` and `comments` controller. For `Admin::PostsController`, Rails will create: -| HTTP Verb | Path | action | used for | +| HTTP Verb | Path | Action | Used for | | --------- | --------------------- | ------- | ------------------------- | | GET | /admin/posts | index | admin_posts_path | | GET | /admin/posts/new | new | new_admin_post_path | @@ -215,7 +215,7 @@ resources :posts, path: '/admin/posts' In each of these cases, the named routes remain the same as if you did not use `scope`. In the last case, the following paths map to `PostsController`: -| HTTP Verb | Path | action | named helper | +| HTTP Verb | Path | Action | Named Helper | | --------- | --------------------- | ------- | ------------------- | | GET | /admin/posts | index | posts_path | | GET | /admin/posts/new | new | new_post_path | @@ -249,7 +249,7 @@ end In addition to the routes for magazines, this declaration will also route ads to an `AdsController`. The ad URLs require a magazine: -| HTTP Verb | Path | action | used for | +| HTTP Verb | Path | Action | Used for | | --------- | ------------------------------------ | ------- | -------------------------------------------------------------------------- | | GET | /magazines/:magazine_id/ads | index | display a list of all ads for a specific magazine | | GET | /magazines/:magazine_id/ads/new | new | return an HTML form for creating a new ad belonging to a specific magazine | @@ -811,7 +811,7 @@ resources :photos, controller: 'images' will recognize incoming paths beginning with `/photos` but route to the `Images` controller: -| HTTP Verb | Path | action | named helper | +| HTTP Verb | Path | Action | Named Helper | | --------- | ---------------- | ------- | -------------------- | | GET | /photos | index | photos_path | | GET | /photos/new | new | new_photo_path | @@ -856,7 +856,7 @@ resources :photos, as: 'images' will recognize incoming paths beginning with `/photos` and route the requests to `PhotosController`, but use the value of the :as option to name the helpers. -| HTTP Verb | Path | action | named helper | +| HTTP Verb | Path | Action | Named Helper | | --------- | ---------------- | ------- | -------------------- | | GET | /photos | index | images_path | | GET | /photos/new | new | new_image_path | @@ -961,7 +961,7 @@ end Rails now creates routes to the `CategoriesController`. -| HTTP Verb | Path | action | used for | +| HTTP Verb | Path | Action | Used for | | --------- | -------------------------- | ------- | ----------------------- | | GET | /kategorien | index | categories_path | | GET | /kategorien/neu | new | new_category_path | -- cgit v1.2.3 From c41576ea45c4b092c6dd105ec6bb1b9c8830016e Mon Sep 17 00:00:00 2001 From: Chris Walquist Date: Sun, 2 Dec 2012 14:37:18 -0600 Subject: match text with translation file example --- guides/source/i18n.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'guides/source') diff --git a/guides/source/i18n.md b/guides/source/i18n.md index e1cf21f039..1131b7f245 100644 --- a/guides/source/i18n.md +++ b/guides/source/i18n.md @@ -696,7 +696,7 @@ en: long: "%B %d, %Y" ``` -So, all of the following equivalent lookups will return the `:short` date format `"%B %d"`: +So, all of the following equivalent lookups will return the `:short` date format `"%b %d"`: ```ruby I18n.t 'date.formats.short' -- cgit v1.2.3 From d21a46823142f62ff744e505b7d6162ffad769c4 Mon Sep 17 00:00:00 2001 From: Pablo Torres Date: Sun, 2 Dec 2012 15:11:26 -0500 Subject: Use correct conjunction and connector words [ci skip] --- guides/source/routing.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'guides/source') diff --git a/guides/source/routing.md b/guides/source/routing.md index 329a0ad8a3..81fcee23b5 100644 --- a/guides/source/routing.md +++ b/guides/source/routing.md @@ -20,7 +20,7 @@ The Rails router recognizes URLs and dispatches them to a controller's action. I ### Connecting URLs to Code -When your Rails application receives an incoming request +When your Rails application receives an incoming request for ``` GET /patients/17 @@ -42,17 +42,19 @@ You can also generate paths and URLs. If the route above is modified to be get '/patients/:id', to: 'patients#show', as: 'patient' ``` -If your application contains this code: +and your application contains this code in the controller ```ruby @patient = Patient.find(17) ``` +and this in the corresponding view + ```erb <%= link_to 'Patient Record', patient_path(@patient) %> ``` -The router will generate the path `/patients/17`. This reduces the brittleness of your view and makes your code easier to understand. Note that the id does not need to be specified in the route helper. +then the router will generate the path `/patients/17`. This reduces the brittleness of your view and makes your code easier to understand. Note that the id does not need to be specified in the route helper. Resource Routing: the Rails Default ----------------------------------- -- cgit v1.2.3 From 624069d58a2c4b535140422cc90cd5347f18cd4f Mon Sep 17 00:00:00 2001 From: Pablo Torres Date: Sun, 2 Dec 2012 15:14:08 -0500 Subject: Move note to a meaningful section in the guides [ci skip] --- guides/source/routing.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'guides/source') diff --git a/guides/source/routing.md b/guides/source/routing.md index 81fcee23b5..2df83be650 100644 --- a/guides/source/routing.md +++ b/guides/source/routing.md @@ -99,6 +99,8 @@ creates seven different routes in your application, all mapping to the `Photos` | PATCH/PUT | /photos/:id | update | update a specific photo | | DELETE | /photos/:id | destroy | delete a specific photo | +NOTE: Because the router uses the HTTP verb and URL to match inbound requests, four URLs map to seven different actions. + NOTE: Rails routes are matched in the order they are specified, so if you have a `resources :photos` above a `get 'photos/poll'` the `show` action's route for the `resources` line will be matched before the `get` line. To fix this, move the `get` line **above** the `resources` line so that it is matched first. ### Paths and URLs @@ -112,8 +114,6 @@ Creating a resourceful route will also expose a number of helpers to the control Each of these helpers has a corresponding `_url` helper (such as `photos_url`) which returns the same path prefixed with the current host, port and path prefix. -NOTE: Because the router uses the HTTP verb and URL to match inbound requests, four URLs map to seven different actions. - ### Defining Multiple Resources at the Same Time If you need to create routes for more than one resource, you can save a bit of typing by defining them all with a single call to `resources`: -- cgit v1.2.3 From 12c7f8042d5945450d05164a3f333b9a5bea74e6 Mon Sep 17 00:00:00 2001 From: Pablo Torres Date: Sun, 2 Dec 2012 15:16:07 -0500 Subject: Clarify section titles [ci skip] --- guides/source/routing.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'guides/source') diff --git a/guides/source/routing.md b/guides/source/routing.md index 2df83be650..038f308a0f 100644 --- a/guides/source/routing.md +++ b/guides/source/routing.md @@ -103,7 +103,7 @@ NOTE: Because the router uses the HTTP verb and URL to match inbound requests, f NOTE: Rails routes are matched in the order they are specified, so if you have a `resources :photos` above a `get 'photos/poll'` the `show` action's route for the `resources` line will be matched before the `get` line. To fix this, move the `get` line **above** the `resources` line so that it is matched first. -### Paths and URLs +### Path and URL Helpers Creating a resourceful route will also expose a number of helpers to the controllers in your application. In the case of `resources :photos`: @@ -1000,7 +1000,7 @@ Inspecting and Testing Routes Rails offers facilities for inspecting and testing your routes. -### Seeing Existing Routes +### Listing Existing Routes To get a complete list of the available routes in your application, visit `http://localhost:3000/rails/info/routes` in your browser while your server is running in the **development** environment. You can also execute the `rake routes` command in your terminal to produce the same output. -- cgit v1.2.3 From 34f8cd226ac50e2270e760c15813d11470f95d45 Mon Sep 17 00:00:00 2001 From: Pablo Torres Date: Sun, 2 Dec 2012 15:43:23 -0500 Subject: Show equivalent routes definition without using concerns [ci skip] --- guides/source/routing.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'guides/source') diff --git a/guides/source/routing.md b/guides/source/routing.md index 038f308a0f..4d63921429 100644 --- a/guides/source/routing.md +++ b/guides/source/routing.md @@ -392,6 +392,19 @@ resources :messages, concerns: :commentable resources :posts, concerns: [:commentable, :image_attachable] ``` +The above is equivalent to: + +```ruby +resources :messages do + resources :comments +end + +resources :posts do + resources :comments + resources :images, only: :index +end +``` + Also you can use them in any place that you want inside the routes, for example in a scope or namespace call: ```ruby -- cgit v1.2.3 From 082f4f98cc82616c3f8d846488fdcb18ee64adf4 Mon Sep 17 00:00:00 2001 From: Pablo Torres Date: Sun, 2 Dec 2012 15:52:12 -0500 Subject: Normalize use of colon before code examples in routing guide [ci skip] --- guides/source/routing.md | 64 ++++++++++++++++++++++++------------------------ 1 file changed, 32 insertions(+), 32 deletions(-) (limited to 'guides/source') diff --git a/guides/source/routing.md b/guides/source/routing.md index 4d63921429..48e6669c9c 100644 --- a/guides/source/routing.md +++ b/guides/source/routing.md @@ -20,13 +20,13 @@ The Rails router recognizes URLs and dispatches them to a controller's action. I ### Connecting URLs to Code -When your Rails application receives an incoming request for +When your Rails application receives an incoming request for: ``` GET /patients/17 ``` -it asks the router to match it to a controller action. If the first matching route is +it asks the router to match it to a controller action. If the first matching route is: ```ruby get '/patients/:id', to: 'patients#show' @@ -36,19 +36,19 @@ the request is dispatched to the `patients` controller's `show` action with `{ i ### Generating Paths and URLs from Code -You can also generate paths and URLs. If the route above is modified to be +You can also generate paths and URLs. If the route above is modified to be: ```ruby get '/patients/:id', to: 'patients#show', as: 'patient' ``` -and your application contains this code in the controller +and your application contains this code in the controller: ```ruby @patient = Patient.find(17) ``` -and this in the corresponding view +and this in the corresponding view: ```erb <%= link_to 'Patient Record', patient_path(@patient) %> @@ -65,13 +65,13 @@ Resource routing allows you to quickly declare all of the common routes for a gi Browsers request pages from Rails by making a request for a URL using a specific HTTP method, such as `GET`, `POST`, `PATCH`, `PUT` and `DELETE`. Each method is a request to perform an operation on the resource. A resource route maps a number of related requests to actions in a single controller. -When your Rails application receives an incoming request for +When your Rails application receives an incoming request for: ``` DELETE /photos/17 ``` -it asks the router to map it to a controller action. If the first matching route is +it asks the router to map it to a controller action. If the first matching route is: ```ruby resources :photos @@ -81,7 +81,7 @@ Rails would dispatch that request to the `destroy` method on the `photos` contro ### CRUD, Verbs, and Actions -In Rails, a resourceful route provides a mapping between HTTP verbs and URLs to controller actions. By convention, each action also maps to particular CRUD operations in a database. A single entry in the routing file, such as +In Rails, a resourceful route provides a mapping between HTTP verbs and URLs to controller actions. By convention, each action also maps to particular CRUD operations in a database. A single entry in the routing file, such as: ```ruby resources :photos @@ -122,7 +122,7 @@ If you need to create routes for more than one resource, you can save a bit of t resources :photos, :books, :videos ``` -This works exactly the same as +This works exactly the same as: ```ruby resources :photos @@ -132,13 +132,13 @@ resources :videos ### Singular Resources -Sometimes, you have a resource that clients always look up without referencing an ID. For example, you would like `/profile` to always show the profile of the currently logged in user. In this case, you can use a singular resource to map `/profile` (rather than `/profile/:id`) to the `show` action. +Sometimes, you have a resource that clients always look up without referencing an ID. For example, you would like `/profile` to always show the profile of the currently logged in user. In this case, you can use a singular resource to map `/profile` (rather than `/profile/:id`) to the `show` action: ```ruby get 'profile', to: 'users#show' ``` -This resourceful route +This resourceful route: ```ruby resource :geocoder @@ -187,7 +187,7 @@ This will create a number of routes for each of the `posts` and `comments` contr | PATCH/PUT | /admin/posts/:id | update | admin_post_path(:id) | | DELETE | /admin/posts/:id | destroy | admin_post_path(:id) | -If you want to route `/posts` (without the prefix `/admin`) to `Admin::PostsController`, you could use +If you want to route `/posts` (without the prefix `/admin`) to `Admin::PostsController`, you could use: ```ruby scope module: 'admin' do @@ -195,13 +195,13 @@ scope module: 'admin' do end ``` -or, for a single case +or, for a single case: ```ruby resources :posts, module: 'admin' ``` -If you want to route `/admin/posts` to `PostsController` (without the `Admin::` module prefix), you could use +If you want to route `/admin/posts` to `PostsController` (without the `Admin::` module prefix), you could use: ```ruby scope '/admin' do @@ -209,7 +209,7 @@ scope '/admin' do end ``` -or, for a single case +or, for a single case: ```ruby resources :posts, path: '/admin/posts' @@ -275,7 +275,7 @@ resources :publishers do end ``` -Deeply-nested resources quickly become cumbersome. In this case, for example, the application would recognize paths such as +Deeply-nested resources quickly become cumbersome. In this case, for example, the application would recognize paths such as: ``` /publishers/1/magazines/2/photos/3 @@ -372,7 +372,7 @@ The comments resource here will have the following routes generated for it: ### Routing concerns -Routing Concerns allows you to declare common routes that can be reused inside others resources and routes. +Routing Concerns allows you to declare common routes that can be reused inside others resources and routes. To define a concern: ```ruby concern :commentable do @@ -384,7 +384,7 @@ concern :image_attachable do end ``` -These concerns can be used in resources to avoid code duplication and share behavior across routes. +These concerns can be used in resources to avoid code duplication and share behavior across routes: ```ruby resources :messages, concerns: :commentable @@ -423,7 +423,7 @@ resources :magazines do end ``` -When using `magazine_ad_path`, you can pass in instances of `Magazine` and `Ad` instead of the numeric IDs. +When using `magazine_ad_path`, you can pass in instances of `Magazine` and `Ad` instead of the numeric IDs: ```erb <%= link_to 'Ad details', magazine_ad_path(@magazine, @ad) %> @@ -596,7 +596,7 @@ Rails would match `photos/12` to the `show` action of `PhotosController`, and se ### Naming Routes -You can specify a name for any route using the `:as` option. +You can specify a name for any route using the `:as` option: ```ruby get 'exit', to: 'sessions#destroy', as: :logout @@ -711,7 +711,7 @@ Both the `matches?` method and the lambda gets the `request` object as an argume ### Route Globbing -Route globbing is a way to specify that a particular parameter should be matched to all the remaining parts of a route. For example +Route globbing is a way to specify that a particular parameter should be matched to all the remaining parts of a route. For example: ```ruby get 'photos/*other', to: 'photos#unknown' @@ -719,7 +719,7 @@ get 'photos/*other', to: 'photos#unknown' This route would match `photos/12` or `/photos/long/path/to/12`, setting `params[:other]` to `"12"` or `"long/path/to/12"`. -Wildcard segments can occur anywhere in a route. For example, +Wildcard segments can occur anywhere in a route. For example: ```ruby get 'books/*section/:title', to: 'books#show' @@ -727,7 +727,7 @@ get 'books/*section/:title', to: 'books#show' would match `books/some/section/last-words-a-memoir` with `params[:section]` equals `'some/section'`, and `params[:title]` equals `'last-words-a-memoir'`. -Technically a route can have even more than one wildcard segment. The matcher assigns segments to parameters in an intuitive way. For example, +Technically a route can have even more than one wildcard segment. The matcher assigns segments to parameters in an intuitive way. For example: ```ruby get '*a/foo/*b', to: 'test#index' @@ -780,7 +780,7 @@ In all of these cases, if you don't provide the leading host (`http://www.exampl ### Routing to Rack Applications -Instead of a String, like `'posts#index'`, which corresponds to the `index` action in the `PostsController`, you can specify any Rack application as the endpoint for a matcher. +Instead of a String, like `'posts#index'`, which corresponds to the `index` action in the `PostsController`, you can specify any Rack application as the endpoint for a matcher: ```ruby match '/application.js', to: Sprockets, via: :all @@ -805,7 +805,7 @@ NOTE: The `root` route only routes `GET` requests to the action. ### Unicode character routes -You can specify unicode character routes directly. For example +You can specify unicode character routes directly. For example: ```ruby get 'こんにちは', to: 'welcome#index' @@ -889,7 +889,7 @@ The `:path_names` option lets you override the automatically-generated "new" and resources :photos, path_names: { new: 'make', edit: 'change' } ``` -This would cause the routing to recognize paths such as +This would cause the routing to recognize paths such as: ``` /photos/make @@ -908,7 +908,7 @@ end ### Prefixing the Named Route Helpers -You can use the `:as` option to prefix the named route helpers that Rails generates for a route. Use this option to prevent name collisions between routes using a path scope. +You can use the `:as` option to prefix the named route helpers that Rails generates for a route. Use this option to prevent name collisions between routes using a path scope. For example: ```ruby scope 'admin' do @@ -988,7 +988,7 @@ Rails now creates routes to the `CategoriesController`. ### Overriding the Singular Form -If you want to define the singular form of a resource, you should add additional rules to the `Inflector`. +If you want to define the singular form of a resource, you should add additional rules to the `Inflector`: ```ruby ActiveSupport::Inflector.inflections do |inflect| @@ -998,7 +998,7 @@ end ### Using `:as` in Nested Resources -The `:as` option overrides the automatically-generated name for the resource in nested route helpers. For example, +The `:as` option overrides the automatically-generated name for the resource in nested route helpers. For example: ```ruby resources :magazines do @@ -1051,7 +1051,7 @@ Routes should be included in your testing strategy (just like the rest of your a #### The `assert_generates` Assertion -`assert_generates` asserts that a particular set of options generate a particular path and can be used with default routes or custom routes. +`assert_generates` asserts that a particular set of options generate a particular path and can be used with default routes or custom routes. For example: ```ruby assert_generates '/photos/1', { controller: 'photos', action: 'show', id: '1' } @@ -1060,7 +1060,7 @@ assert_generates '/about', controller: 'pages', action: 'about' #### The `assert_recognizes` Assertion -`assert_recognizes` is the inverse of `assert_generates`. It asserts that a given path is recognized and routes it to a particular spot in your application. +`assert_recognizes` is the inverse of `assert_generates`. It asserts that a given path is recognized and routes it to a particular spot in your application. For example: ```ruby assert_recognizes({ controller: 'photos', action: 'show', id: '1' }, '/photos/1') @@ -1074,7 +1074,7 @@ assert_recognizes({ controller: 'photos', action: 'create' }, { path: 'photos', #### The `assert_routing` Assertion -The `assert_routing` assertion checks the route both ways: it tests that the path generates the options, and that the options generate the path. Thus, it combines the functions of `assert_generates` and `assert_recognizes`. +The `assert_routing` assertion checks the route both ways: it tests that the path generates the options, and that the options generate the path. Thus, it combines the functions of `assert_generates` and `assert_recognizes`: ```ruby assert_routing({ path: 'photos', method: :post }, { controller: 'photos', action: 'create' }) -- cgit v1.2.3 From 7d6cb7585d08e1c76c61595f87d071d8a50c12c9 Mon Sep 17 00:00:00 2001 From: Pablo Torres Date: Sun, 2 Dec 2012 16:10:35 -0500 Subject: Convert a section to a tip to highlight it [ci skip] --- guides/source/routing.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'guides/source') diff --git a/guides/source/routing.md b/guides/source/routing.md index 48e6669c9c..e5af6b0f9b 100644 --- a/guides/source/routing.md +++ b/guides/source/routing.md @@ -515,9 +515,7 @@ end This will enable Rails to recognize paths such as `/comments/new/preview` with GET, and route to the `preview` action of `CommentsController`. It will also create the `preview_new_comment_url` and `preview_new_comment_path` route helpers. -#### A Note of Caution - -If you find yourself adding many extra actions to a resourceful route, it's time to stop and ask yourself whether you're disguising the presence of another resource. +TIP: If you find yourself adding many extra actions to a resourceful route, it's time to stop and ask yourself whether you're disguising the presence of another resource. Non-Resourceful Routes ---------------------- -- cgit v1.2.3 From c5539cdbdda302a0f6535c0b8b7e404ec57d349c Mon Sep 17 00:00:00 2001 From: Pablo Torres Date: Sun, 2 Dec 2012 16:20:48 -0500 Subject: Correct grammar [ci skip] --- guides/source/routing.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'guides/source') diff --git a/guides/source/routing.md b/guides/source/routing.md index e5af6b0f9b..170639c7da 100644 --- a/guides/source/routing.md +++ b/guides/source/routing.md @@ -552,7 +552,7 @@ NOTE: You can't use `:namespace` or `:module` with a `:controller` path segment. get ':controller(/:action(/:id))', controller: /admin\/[^\/]+/ ``` -TIP: By default dynamic segments don't accept dots - this is because the dot is used as a separator for formatted routes. If you need to use a dot within a dynamic segment, add a constraint that overrides this – for example, `id: /[^\/]+/` allows anything except a slash. +TIP: By default, dynamic segments don't accept dots - this is because the dot is used as a separator for formatted routes. If you need to use a dot within a dynamic segment, add a constraint that overrides this – for example, `id: /[^\/]+/` allows anything except a slash. ### Static Segments @@ -725,7 +725,7 @@ get 'books/*section/:title', to: 'books#show' would match `books/some/section/last-words-a-memoir` with `params[:section]` equals `'some/section'`, and `params[:title]` equals `'last-words-a-memoir'`. -Technically a route can have even more than one wildcard segment. The matcher assigns segments to parameters in an intuitive way. For example: +Technically, a route can have even more than one wildcard segment. The matcher assigns segments to parameters in an intuitive way. For example: ```ruby get '*a/foo/*b', to: 'test#index' @@ -778,7 +778,7 @@ In all of these cases, if you don't provide the leading host (`http://www.exampl ### Routing to Rack Applications -Instead of a String, like `'posts#index'`, which corresponds to the `index` action in the `PostsController`, you can specify any Rack application as the endpoint for a matcher: +Instead of a String like `'posts#index'`, which corresponds to the `index` action in the `PostsController`, you can specify any Rack application as the endpoint for a matcher: ```ruby match '/application.js', to: Sprockets, via: :all -- cgit v1.2.3 From 51b26298e699e039a78651452ec5f5a46d570d24 Mon Sep 17 00:00:00 2001 From: Pablo Torres Date: Sun, 2 Dec 2012 16:21:29 -0500 Subject: Add clarity to static segments docs in non-resourceful routes [ci skip] --- guides/source/routing.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'guides/source') diff --git a/guides/source/routing.md b/guides/source/routing.md index 170639c7da..5728a17c04 100644 --- a/guides/source/routing.md +++ b/guides/source/routing.md @@ -556,7 +556,7 @@ TIP: By default, dynamic segments don't accept dots - this is because the dot is ### Static Segments -You can specify static segments when creating a route: +You can specify static segments when creating a route by not prepending a colon to a fragment: ```ruby get ':controller/:action/:id/with_user/:user_id' -- cgit v1.2.3 From 25e75153b4a8f395bded7c82e63e2e188e7acea2 Mon Sep 17 00:00:00 2001 From: Pablo Torres Date: Sun, 2 Dec 2012 16:27:13 -0500 Subject: Convert parragraph about security to a NOTE [ci skip] --- guides/source/routing.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'guides/source') diff --git a/guides/source/routing.md b/guides/source/routing.md index 5728a17c04..81eb1e141b 100644 --- a/guides/source/routing.md +++ b/guides/source/routing.md @@ -624,7 +624,7 @@ You can match all verbs to a particular route using `via: :all`: match 'photos', to: 'photos#show', via: :all ``` -You should avoid routing all verbs to an action unless you have a good reason to, as routing both `GET` requests and `POST` requests to a single action has security implications. +NOTE: Routing both `GET` and `POST` requests to a single action has security implications. In general, you should avoid routing all verbs to an action unless you have a good reason to. ### Segment Constraints -- cgit v1.2.3 From 1d36963754d4bb0fdd563c3354ff8aae805577a0 Mon Sep 17 00:00:00 2001 From: Pablo Torres Date: Sun, 2 Dec 2012 16:31:12 -0500 Subject: Add counterexample for regex [ci skip] --- guides/source/routing.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'guides/source') diff --git a/guides/source/routing.md b/guides/source/routing.md index 81eb1e141b..45eef7443b 100644 --- a/guides/source/routing.md +++ b/guides/source/routing.md @@ -634,7 +634,7 @@ You can use the `:constraints` option to enforce a format for a dynamic segment: get 'photos/:id', to: 'photos#show', constraints: { id: /[A-Z]\d{5}/ } ``` -This route would match paths such as `/photos/A12345`. You can more succinctly express the same route this way: +This route would match paths such as `/photos/A12345`, but not `/photos/893`. You can more succinctly express the same route this way: ```ruby get 'photos/:id', to: 'photos#show', id: /[A-Z]\d{5}/ -- cgit v1.2.3 From 286e56ffe7807421b2b4a72e8386e9eb83767313 Mon Sep 17 00:00:00 2001 From: Pablo Torres Date: Sun, 2 Dec 2012 16:43:47 -0500 Subject: Standardize use of "route globbing" and "wildcard segments" [ci skip] --- guides/source/routing.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'guides/source') diff --git a/guides/source/routing.md b/guides/source/routing.md index 45eef7443b..714c0b609e 100644 --- a/guides/source/routing.md +++ b/guides/source/routing.md @@ -707,7 +707,7 @@ end Both the `matches?` method and the lambda gets the `request` object as an argument. -### Route Globbing +### Route Globbing and Wildcard Segments Route globbing is a way to specify that a particular parameter should be matched to all the remaining parts of a route. For example: @@ -715,7 +715,7 @@ Route globbing is a way to specify that a particular parameter should be matched get 'photos/*other', to: 'photos#unknown' ``` -This route would match `photos/12` or `/photos/long/path/to/12`, setting `params[:other]` to `"12"` or `"long/path/to/12"`. +This route would match `photos/12` or `/photos/long/path/to/12`, setting `params[:other]` to `"12"` or `"long/path/to/12"`. The fragments prefixed with a star are called "wildcard segments". Wildcard segments can occur anywhere in a route. For example: @@ -733,7 +733,7 @@ get '*a/foo/*b', to: 'test#index' would match `zoo/woo/foo/bar/baz` with `params[:a]` equals `'zoo/woo'`, and `params[:b]` equals `'bar/baz'`. -NOTE: Starting from Rails 3.1, wildcard routes will always match the optional format segment by default. For example if you have this route: +NOTE: Starting from Rails 3.1, wildcard segments will always match the optional format segment by default. For example if you have this route: ```ruby get '*pages', to: 'pages#show' -- cgit v1.2.3 From ef4e8352fe39a7b7495c68aeaa77c5296b9de82d Mon Sep 17 00:00:00 2001 From: Yoni Yalovitsky Date: Mon, 3 Dec 2012 02:57:40 +0200 Subject: removed an extra slash before the 'app' dir --- guides/source/getting_started.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'guides/source') diff --git a/guides/source/getting_started.md b/guides/source/getting_started.md index ceba2c65aa..eefffbe87e 100644 --- a/guides/source/getting_started.md +++ b/guides/source/getting_started.md @@ -1350,7 +1350,7 @@ the post show page to see their comment now listed. Due to this, our spam comments when they arrive. So first, we'll wire up the Post show template -(`/app/views/posts/show.html.erb`) to let us make a new comment: +(`app/views/posts/show.html.erb`) to let us make a new comment: ```html+erb

-- cgit v1.2.3 From 57c60d36d365c8722947001c42b722902b086e0a Mon Sep 17 00:00:00 2001 From: Yoni Yalovitsky Date: Mon, 3 Dec 2012 03:11:16 +0200 Subject: added full file path in all file references It's really comfy being able to copy-paste the file paths. I suppose there's also some benefit in trying to figure out the file name as a small exercise, but I don't think that sort of thing belongs in "Getting Started". --- guides/source/getting_started.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'guides/source') diff --git a/guides/source/getting_started.md b/guides/source/getting_started.md index eefffbe87e..b1ca8da292 100644 --- a/guides/source/getting_started.md +++ b/guides/source/getting_started.md @@ -520,7 +520,7 @@ invoking the command: `rake db:migrate RAILS_ENV=production`. ### Saving data in the controller Back in `posts_controller`, we need to change the `create` action -to use the new `Post` model to save the data in the database. Open that file +to use the new `Post` model to save the data in the database. Open `app/controllers/posts_controller.rb` and change the `create` action to look like this: ```ruby @@ -558,8 +558,8 @@ parameter, which in our case will be the id of the post. Note that this time we had to specify the actual mapping, `posts#show` because otherwise Rails would not know which action to render. -As we did before, we need to add the `show` action in the -`posts_controller` and its respective view. +As we did before, we need to add the `show` action in +`app/controllers/posts_controller.rb` and its respective view. ```ruby def show @@ -1216,7 +1216,7 @@ This command will generate four files: | test/models/comment_test.rb | Testing harness for the comments model | | test/fixtures/comments.yml | Sample comments for use in testing | -First, take a look at `comment.rb`: +First, take a look at `app/models/comment.rb`: ```ruby class Comment < ActiveRecord::Base @@ -1277,7 +1277,7 @@ this way: * One post can have many comments. In fact, this is very close to the syntax that Rails uses to declare this -association. You've already seen the line of code inside the `Comment` model that +association. You've already seen the line of code inside the `Comment` model (app/models/comment.rb) that makes each comment belong to a Post: ```ruby @@ -1286,7 +1286,7 @@ class Comment < ActiveRecord::Base end ``` -You'll need to edit the `post.rb` file to add the other side of the association: +You'll need to edit `app/models/post.rb` to add the other side of the association: ```ruby class Post < ActiveRecord::Base @@ -1609,7 +1609,7 @@ So first, let's add the delete link in the Clicking this new "Destroy Comment" link will fire off a `DELETE /posts/:post_id/comments/:id` to our `CommentsController`, which can then use this to find the comment we want to delete, so let's add a destroy action to our -controller: +controller (`app/controllers/comments_controller.rb`): ```ruby class CommentsController < ApplicationController @@ -1667,7 +1667,7 @@ action if that method allows it. To use the authentication system, we specify it at the top of our `PostsController`, in this case, we want the user to be authenticated on every -action, except for `index` and `show`, so we write that: +action, except for `index` and `show`, so we write that in `app/controllers/posts_controller.rb`: ```ruby class PostsController < ApplicationController @@ -1682,7 +1682,7 @@ class PostsController < ApplicationController ``` We also only want to allow authenticated users to delete comments, so in the -`CommentsController` we write: +`CommentsController` (`app/controllers/comments_controller.rb`) we write: ```ruby class CommentsController < ApplicationController -- cgit v1.2.3 From 47d95c8c3cf48005a91c5fa258ca792e70391069 Mon Sep 17 00:00:00 2001 From: 1334 <1334@shadowproject.net> Date: Mon, 3 Dec 2012 14:01:27 +0100 Subject: fix some formatting --- guides/source/migrations.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'guides/source') diff --git a/guides/source/migrations.md b/guides/source/migrations.md index 7b1ca9ea90..43aef85e8d 100644 --- a/guides/source/migrations.md +++ b/guides/source/migrations.md @@ -640,7 +640,7 @@ no such migrations, it exits. It will run these migrations in order based on the date of the migration. Note that running the `db:migrate` also invokes the `db:schema:dump` task, which -will update your db/schema.rb file to match the structure of your database. +will update your `db/schema.rb` file to match the structure of your database. If you specify a target version, Active Record will run the required migrations (up, down or change) until it has reached the specified version. The version @@ -694,7 +694,7 @@ The `rake db:reset` task will drop the database, recreate it and load the current schema into it. NOTE: This is not the same as running all the migrations. It will only use the contents -of the current schema.rb file. If a migration can't be rolled back, 'rake db:reset' +of the current `schema.rb` file. If a migration can't be rolled back, `rake db:reset` may not help you. To find out more about dumping the schema see [schema.rb](#schema-dumping-and-you). ### Running Specific Migrations -- cgit v1.2.3