rails 4.0.0.rc1 → 4.0.0.rc2
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of rails might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/guides/CHANGELOG.md +9 -2
- data/guides/assets/images/{challenge.png → getting_started/challenge.png} +0 -0
- data/guides/assets/images/getting_started/rails_welcome.png +0 -0
- data/guides/bug_report_templates/active_record_gem.rb +1 -1
- data/guides/code/getting_started/Gemfile.lock +1 -1
- data/guides/code/getting_started/config/environment.rb +2 -2
- data/guides/source/2_2_release_notes.md +1 -1
- data/guides/source/2_3_release_notes.md +2 -2
- data/guides/source/3_0_release_notes.md +3 -3
- data/guides/source/3_1_release_notes.md +1 -1
- data/guides/source/3_2_release_notes.md +3 -3
- data/guides/source/4_0_release_notes.md +4 -3
- data/guides/source/action_controller_overview.md +5 -5
- data/guides/source/action_mailer_basics.md +1 -1
- data/guides/source/active_record_querying.md +6 -1
- data/guides/source/active_record_validations.md +2 -1
- data/guides/source/active_support_core_extensions.md +9 -6
- data/guides/source/asset_pipeline.md +12 -14
- data/guides/source/caching_with_rails.md +2 -2
- data/guides/source/command_line.md +1 -1
- data/guides/source/configuring.md +1 -1
- data/guides/source/contributing_to_ruby_on_rails.md +2 -2
- data/guides/source/debugging_rails_applications.md +3 -1
- data/guides/source/development_dependencies_install.md +1 -1
- data/guides/source/getting_started.md +78 -179
- data/guides/source/i18n.md +1 -1
- data/guides/source/initialization.md +34 -36
- data/guides/source/layout.html.erb +2 -2
- data/guides/source/layouts_and_rendering.md +74 -68
- data/guides/source/migrations.md +19 -1
- data/guides/source/ruby_on_rails_guides_guidelines.md +4 -0
- data/guides/source/testing.md +25 -17
- data/guides/source/upgrading_ruby_on_rails.md +1 -1
- metadata +17 -17
- data/guides/assets/images/rails_welcome.png +0 -0
@@ -64,7 +64,7 @@ Creating a New Rails Project
|
|
64
64
|
The best way to use this guide is to follow each step as it happens, no code or
|
65
65
|
step needed to make this example application has been left out, so you can
|
66
66
|
literally follow along step by step. You can get the complete code
|
67
|
-
[here](https://github.com/
|
67
|
+
[here](https://github.com/rails/docrails/tree/master/guides/code/getting_started).
|
68
68
|
|
69
69
|
By following along with this guide, you'll create a Rails project called
|
70
70
|
`blog`, a
|
@@ -165,7 +165,7 @@ TIP: Compiling CoffeeScript to JavaScript requires a JavaScript runtime and the
|
|
165
165
|
|
166
166
|
This will fire up WEBrick, a webserver built into Ruby by default. To see your application in action, open a browser window and navigate to <http://localhost:3000>. You should see the Rails default information page:
|
167
167
|
|
168
|
-
![Welcome Aboard screenshot](images/rails_welcome.png)
|
168
|
+
![Welcome Aboard screenshot](images/getting_started/rails_welcome.png)
|
169
169
|
|
170
170
|
TIP: To stop the web server, hit Ctrl+C in the terminal window where it's running. To verify the server has stopped you should see your command prompt cursor again. For most UNIX-like systems including Mac OS X this will be a dollar sign `$`. In development mode, Rails does not generally require you to restart the server; changes you make in files will be automatically picked up by the server.
|
171
171
|
|
@@ -252,29 +252,43 @@ Now that you've seen how to create a controller, an action and a view, let's cre
|
|
252
252
|
|
253
253
|
In the Blog application, you will now create a new _resource_. A resource is the term used for a collection of similar objects, such as posts, people or animals. You can create, read, update and destroy items for a resource and these operations are referred to as _CRUD_ operations.
|
254
254
|
|
255
|
-
|
255
|
+
Rails provides a `resources` method which can be used to declare a
|
256
|
+
standard REST resource. Here's how `config/routes.rb` will look like.
|
256
257
|
|
257
|
-
|
258
|
+
```ruby
|
259
|
+
Blog::Application.routes.draw do
|
258
260
|
|
259
|
-
|
261
|
+
resources :posts
|
260
262
|
|
261
|
-
|
263
|
+
root to: "welcome#index"
|
264
|
+
end
|
265
|
+
```
|
262
266
|
|
263
|
-
|
267
|
+
If you run `rake routes`, you'll see that all the routes for the
|
268
|
+
standard RESTful actions.
|
264
269
|
|
265
|
-
|
270
|
+
```bash
|
271
|
+
$ rake routes
|
272
|
+
posts GET /posts(.:format) posts#index
|
273
|
+
POST /posts(.:format) posts#create
|
274
|
+
new_post GET /posts/new(.:format) posts#new
|
275
|
+
edit_post GET /posts/:id/edit(.:format) posts#edit
|
276
|
+
post GET /posts/:id(.:format) posts#show
|
277
|
+
PATCH /posts/:id(.:format) posts#update
|
278
|
+
PUT /posts/:id(.:format) posts#update
|
279
|
+
DELETE /posts/:id(.:format) posts#destroy
|
280
|
+
root / welcome#index
|
281
|
+
```
|
266
282
|
|
267
|
-
|
283
|
+
In the next section, you will add the ability to create new posts in your application and be able to view them. This is the "C" and the "R" from CRUD: creation and reading. The form for doing this will look like this:
|
268
284
|
|
269
|
-
|
285
|
+
![The new post form](images/getting_started/new_post.png)
|
270
286
|
|
271
|
-
|
272
|
-
get "posts/new"
|
273
|
-
```
|
287
|
+
It will look a little basic for now, but that's ok. We'll look at improving the styling for it afterwards.
|
274
288
|
|
275
|
-
|
289
|
+
### Laying down the ground work
|
276
290
|
|
277
|
-
With the route defined, requests can now be made to `/posts/new` in the application. Navigate to <http://localhost:3000/posts/new> and you'll see
|
291
|
+
The first thing that you are going to need to create a new post within the application is a place to do that. A great place for that would be at `/posts/new`. With the route already defined, requests can now be made to `/posts/new` in the application. Navigate to <http://localhost:3000/posts/new> and you'll see a routing error:
|
278
292
|
|
279
293
|
![Another routing error, uninitialized constant PostsController](images/getting_started/routing_error_no_controller.png)
|
280
294
|
|
@@ -377,14 +391,10 @@ like this is called "create", and so the form should be pointed to that action.
|
|
377
391
|
Edit the `form_for` line inside `app/views/posts/new.html.erb` to look like this:
|
378
392
|
|
379
393
|
```html+erb
|
380
|
-
<%= form_for :post, url:
|
394
|
+
<%= form_for :post, url: posts_path do |f| %>
|
381
395
|
```
|
382
396
|
|
383
|
-
In this example,
|
384
|
-
|
385
|
-
```ruby
|
386
|
-
post "posts" => "posts#create"
|
387
|
-
```
|
397
|
+
In this example, the `posts_path` helper is passed to the `:url` option. What Rails will do with this is that it will point the form to the `create` action of the current controller, the `PostsController`, and will send a `POST` request to that route.
|
388
398
|
|
389
399
|
By using the `post` method rather than the `get` method, Rails will define a route that will only respond to POST methods. The POST method is the typical method used by forms all over the web.
|
390
400
|
|
@@ -521,21 +531,28 @@ and change the `create` action to look like this:
|
|
521
531
|
|
522
532
|
```ruby
|
523
533
|
def create
|
524
|
-
@post = Post.new(
|
525
|
-
|
534
|
+
@post = Post.new(post_params)
|
535
|
+
|
526
536
|
@post.save
|
527
|
-
redirect_to
|
537
|
+
redirect_to @post
|
528
538
|
end
|
539
|
+
|
540
|
+
private
|
541
|
+
def post_params
|
542
|
+
params.require(:post).permit(:title, :text)
|
543
|
+
end
|
529
544
|
```
|
530
545
|
|
531
546
|
Here's what's going on: every Rails model can be initialized with its
|
532
547
|
respective attributes, which are automatically mapped to the respective
|
533
548
|
database columns. In the first line we do just that (remember that
|
534
|
-
`
|
549
|
+
`post_params` contains the attributes we're interested in). Then,
|
535
550
|
`@post.save` is responsible for saving the model in the database.
|
536
551
|
Finally, we redirect the user to the `show` action,
|
537
552
|
which we'll define later.
|
538
553
|
|
554
|
+
TIP: Note that `def post_params` is private. This new approach prevents an attacker from setting the model's attributes by manipulating the hash passed to the model. For more information, refer to [this blog post about Strong Parameters](http://weblog.rubyonrails.org/2012/3/21/strong-parameters/).
|
555
|
+
|
539
556
|
TIP: As we'll see later, `@post.save` returns a boolean indicating
|
540
557
|
whether the model was saved or not.
|
541
558
|
|
@@ -543,16 +560,14 @@ whether the model was saved or not.
|
|
543
560
|
|
544
561
|
If you submit the form again now, Rails will complain about not finding
|
545
562
|
the `show` action. That's not very useful though, so let's add the
|
546
|
-
`show` action before proceeding.
|
563
|
+
`show` action before proceeding.
|
547
564
|
|
548
565
|
```ruby
|
549
|
-
|
566
|
+
post GET /posts/:id(.:format) posts#show
|
550
567
|
```
|
551
568
|
|
552
569
|
The special syntax `:id` tells rails that this route expects an `:id`
|
553
|
-
parameter, which in our case will be the id of the post.
|
554
|
-
time we had to specify the actual mapping, `posts#show` because
|
555
|
-
otherwise Rails would not know which action to render.
|
570
|
+
parameter, which in our case will be the id of the post.
|
556
571
|
|
557
572
|
As we did before, we need to add the `show` action in
|
558
573
|
`app/controllers/posts_controller.rb` and its respective view.
|
@@ -601,7 +616,7 @@ look like this:
|
|
601
616
|
@post = Post.new(params[:post].permit(:title, :text))
|
602
617
|
|
603
618
|
@post.save
|
604
|
-
redirect_to
|
619
|
+
redirect_to @post
|
605
620
|
end
|
606
621
|
```
|
607
622
|
|
@@ -613,11 +628,11 @@ Visit <http://localhost:3000/posts/new> and give it a try!
|
|
613
628
|
|
614
629
|
### Listing all posts
|
615
630
|
|
616
|
-
We still need a way to list all our posts, so let's do that.
|
617
|
-
|
631
|
+
We still need a way to list all our posts, so let's do that.
|
632
|
+
We'll use a specific route from `config/routes.rb`:
|
618
633
|
|
619
634
|
```ruby
|
620
|
-
|
635
|
+
posts GET /posts(.:format) posts#index
|
621
636
|
```
|
622
637
|
|
623
638
|
And an action for that route inside the `PostsController` in the `app/controllers/posts_controller.rb` file:
|
@@ -669,7 +684,7 @@ for posts.
|
|
669
684
|
Let's add links to the other views as well, starting with adding this "New Post" link to `app/views/posts/index.html.erb`, placing it above the `<table>` tag:
|
670
685
|
|
671
686
|
```erb
|
672
|
-
<%= link_to 'New post',
|
687
|
+
<%= link_to 'New post', new_post_path %>
|
673
688
|
```
|
674
689
|
|
675
690
|
This link will allow you to bring up the form that lets you create a new post. You should also add a link to this template — `app/views/posts/new.html.erb` — to go back to the `index` action. Do this by adding this underneath the form in this template:
|
@@ -679,7 +694,7 @@ This link will allow you to bring up the form that lets you create a new post. Y
|
|
679
694
|
...
|
680
695
|
<% end %>
|
681
696
|
|
682
|
-
<%= link_to 'Back',
|
697
|
+
<%= link_to 'Back', posts_path %>
|
683
698
|
```
|
684
699
|
|
685
700
|
Finally, add another link to the `app/views/posts/show.html.erb` template to go back to the `index` action as well, so that people who are viewing a single post can go back and view the whole list again:
|
@@ -695,7 +710,7 @@ Finally, add another link to the `app/views/posts/show.html.erb` template to go
|
|
695
710
|
<%= @post.text %>
|
696
711
|
</p>
|
697
712
|
|
698
|
-
<%= link_to 'Back',
|
713
|
+
<%= link_to 'Back', posts_path %>
|
699
714
|
```
|
700
715
|
|
701
716
|
TIP: If you want to link to an action in the same controller, you don't
|
@@ -755,7 +770,7 @@ def create
|
|
755
770
|
@post = Post.new(params[:post].permit(:title, :text))
|
756
771
|
|
757
772
|
if @post.save
|
758
|
-
redirect_to
|
773
|
+
redirect_to @post
|
759
774
|
else
|
760
775
|
render 'new'
|
761
776
|
end
|
@@ -776,7 +791,7 @@ something went wrong. To do that, you'll modify
|
|
776
791
|
`app/views/posts/new.html.erb` to check for error messages:
|
777
792
|
|
778
793
|
```html+erb
|
779
|
-
<%= form_for :post, url:
|
794
|
+
<%= form_for :post, url: posts_path do |f| %>
|
780
795
|
<% if @post.errors.any? %>
|
781
796
|
<div id="errorExplanation">
|
782
797
|
<h2><%= pluralize(@post.errors.count, "error") %> prohibited
|
@@ -803,7 +818,7 @@ something went wrong. To do that, you'll modify
|
|
803
818
|
</p>
|
804
819
|
<% end %>
|
805
820
|
|
806
|
-
<%= link_to 'Back',
|
821
|
+
<%= link_to 'Back', posts_path %>
|
807
822
|
```
|
808
823
|
|
809
824
|
A few things are going on. We check if there are any errors with
|
@@ -832,14 +847,6 @@ We've covered the "CR" part of CRUD. Now let's focus on the "U" part, updating p
|
|
832
847
|
|
833
848
|
The first step we'll take is adding an `edit` action to `posts_controller`.
|
834
849
|
|
835
|
-
Start by adding a route to `config/routes.rb`:
|
836
|
-
|
837
|
-
```ruby
|
838
|
-
get "posts/:id/edit" => "posts#edit"
|
839
|
-
```
|
840
|
-
|
841
|
-
And then add the controller action:
|
842
|
-
|
843
850
|
```ruby
|
844
851
|
def edit
|
845
852
|
@post = Post.find(params[:id])
|
@@ -853,7 +860,7 @@ it look as follows:
|
|
853
860
|
```html+erb
|
854
861
|
<h1>Editing post</h1>
|
855
862
|
|
856
|
-
<%= form_for :post, url:
|
863
|
+
<%= form_for :post, url: post_path(@post.id) },
|
857
864
|
method: :patch do |f| %>
|
858
865
|
<% if @post.errors.any? %>
|
859
866
|
<div id="errorExplanation">
|
@@ -881,7 +888,7 @@ method: :patch do |f| %>
|
|
881
888
|
</p>
|
882
889
|
<% end %>
|
883
890
|
|
884
|
-
<%= link_to 'Back',
|
891
|
+
<%= link_to 'Back', posts_path %>
|
885
892
|
```
|
886
893
|
|
887
894
|
This time we point the form to the `update` action, which is not defined yet
|
@@ -893,21 +900,14 @@ via the `PATCH` HTTP method which is the HTTP method you're expected to use to
|
|
893
900
|
|
894
901
|
TIP: By default forms built with the _form_for_ helper are sent via `POST`.
|
895
902
|
|
896
|
-
Next
|
897
|
-
`config/routes.rb` will need just one more line:
|
898
|
-
|
899
|
-
```ruby
|
900
|
-
patch "posts/:id" => "posts#update"
|
901
|
-
```
|
902
|
-
|
903
|
-
And then create the `update` action in `app/controllers/posts_controller.rb`:
|
903
|
+
Next we need to create the `update` action in `app/controllers/posts_controller.rb`:
|
904
904
|
|
905
905
|
```ruby
|
906
906
|
def update
|
907
907
|
@post = Post.find(params[:id])
|
908
908
|
|
909
909
|
if @post.update(params[:post].permit(:title, :text))
|
910
|
-
redirect_to
|
910
|
+
redirect_to @post
|
911
911
|
else
|
912
912
|
render 'edit'
|
913
913
|
end
|
@@ -941,8 +941,8 @@ appear next to the "Show" link:
|
|
941
941
|
<tr>
|
942
942
|
<td><%= post.title %></td>
|
943
943
|
<td><%= post.text %></td>
|
944
|
-
<td><%= link_to 'Show',
|
945
|
-
<td><%= link_to 'Edit',
|
944
|
+
<td><%= link_to 'Show', post_path %></td>
|
945
|
+
<td><%= link_to 'Edit', edit_post_path(post) %></td>
|
946
946
|
</tr>
|
947
947
|
<% end %>
|
948
948
|
</table>
|
@@ -955,8 +955,8 @@ the template:
|
|
955
955
|
```html+erb
|
956
956
|
...
|
957
957
|
|
958
|
-
<%= link_to 'Back',
|
959
|
-
| <%= link_to 'Edit',
|
958
|
+
<%= link_to 'Back', posts_path %>
|
959
|
+
| <%= link_to 'Edit', edit_post_path(@post) %>
|
960
960
|
```
|
961
961
|
|
962
962
|
And here's how our app looks so far:
|
@@ -1016,7 +1016,7 @@ completely:
|
|
1016
1016
|
|
1017
1017
|
<%= render 'form' %>
|
1018
1018
|
|
1019
|
-
<%= link_to 'Back',
|
1019
|
+
<%= link_to 'Back', posts_path %>
|
1020
1020
|
```
|
1021
1021
|
|
1022
1022
|
Then do the same for the `app/views/posts/edit.html.erb` view:
|
@@ -1026,66 +1026,17 @@ Then do the same for the `app/views/posts/edit.html.erb` view:
|
|
1026
1026
|
|
1027
1027
|
<%= render 'form' %>
|
1028
1028
|
|
1029
|
-
<%= link_to 'Back',
|
1029
|
+
<%= link_to 'Back', posts_path %>
|
1030
1030
|
```
|
1031
1031
|
|
1032
|
-
Point your browser to <http://localhost:3000/posts/new> and
|
1033
|
-
try creating a new post. Everything still works. Now try editing the
|
1034
|
-
post and you'll receive the following error:
|
1035
|
-
|
1036
|
-
![Undefined method post_path](images/getting_started/undefined_method_post_path.png)
|
1037
|
-
|
1038
|
-
To understand this error, you need to understand how `form_for` works.
|
1039
|
-
When you pass an object to `form_for` and you don't specify a `:url`
|
1040
|
-
option, Rails will try to guess the `action` and `method` options by
|
1041
|
-
checking if the passed object is a new record or not. Rails follows the
|
1042
|
-
REST convention, so to create a new `Post` object it will look for a
|
1043
|
-
route named `posts_path`, and to update a `Post` object it will look for
|
1044
|
-
a route named `post_path` and pass the current object. Similarly, rails
|
1045
|
-
knows that it should create new objects via POST and update them via
|
1046
|
-
PATCH.
|
1047
|
-
|
1048
|
-
If you run `rake routes` from the console you'll see that we already
|
1049
|
-
have a `posts_path` route, which was created automatically by Rails when we
|
1050
|
-
defined the route for the index action.
|
1051
|
-
However, we don't have a `post_path` yet, which is the reason why we
|
1052
|
-
received an error before. With your server running you can view your routes by visiting [localhost:3000/rails/info/routes](http://localhost:3000/rails/info/routes), or you can generate them from the command line by running `rake routes`:
|
1053
|
-
|
1054
|
-
```bash
|
1055
|
-
$ rake routes
|
1056
|
-
|
1057
|
-
posts GET /posts(.:format) posts#index
|
1058
|
-
posts_new GET /posts/new(.:format) posts#new
|
1059
|
-
POST /posts(.:format) posts#create
|
1060
|
-
GET /posts/:id(.:format) posts#show
|
1061
|
-
GET /posts/:id/edit(.:format) posts#edit
|
1062
|
-
PATCH /posts/:id(.:format) posts#update
|
1063
|
-
root / welcome#index
|
1064
|
-
```
|
1065
|
-
|
1066
|
-
To fix this, open `config/routes.rb` and modify the `get "posts/:id"`
|
1067
|
-
line like this:
|
1068
|
-
|
1069
|
-
```ruby
|
1070
|
-
get "posts/:id" => "posts#show", as: :post
|
1071
|
-
```
|
1072
|
-
|
1073
|
-
The `:as` option tells the `get` method that we want to make routing helpers
|
1074
|
-
called `post_url` and `post_path` available to our application. These are
|
1075
|
-
precisely the methods that the `form_for` needs when editing a post, and so now
|
1076
|
-
you'll be able to update posts again.
|
1077
|
-
|
1078
|
-
NOTE: The `:as` option is available on the `post`, `patch`, `put`, `delete` and `match`
|
1079
|
-
routing methods also.
|
1080
|
-
|
1081
1032
|
### Deleting Posts
|
1082
1033
|
|
1083
1034
|
We're now ready to cover the "D" part of CRUD, deleting posts from the
|
1084
|
-
database. Following the REST convention,
|
1085
|
-
deleting posts
|
1035
|
+
database. Following the REST convention, the route for
|
1036
|
+
deleting posts in the `config/routes.rb` is:
|
1086
1037
|
|
1087
1038
|
```ruby
|
1088
|
-
|
1039
|
+
DELETE /posts/:id(.:format) posts#destroy
|
1089
1040
|
```
|
1090
1041
|
|
1091
1042
|
The `delete` routing method should be used for routes that destroy
|
@@ -1105,7 +1056,7 @@ def destroy
|
|
1105
1056
|
@post = Post.find(params[:id])
|
1106
1057
|
@post.destroy
|
1107
1058
|
|
1108
|
-
redirect_to
|
1059
|
+
redirect_to posts_path
|
1109
1060
|
end
|
1110
1061
|
```
|
1111
1062
|
|
@@ -1132,18 +1083,17 @@ together.
|
|
1132
1083
|
<tr>
|
1133
1084
|
<td><%= post.title %></td>
|
1134
1085
|
<td><%= post.text %></td>
|
1135
|
-
<td><%= link_to 'Show',
|
1136
|
-
<td><%= link_to 'Edit',
|
1137
|
-
<td><%= link_to 'Destroy',
|
1086
|
+
<td><%= link_to 'Show', post_path %></td>
|
1087
|
+
<td><%= link_to 'Edit', edit_post_path(post) %></td>
|
1088
|
+
<td><%= link_to 'Destroy', post_path(post),
|
1138
1089
|
method: :delete, data: { confirm: 'Are you sure?' } %></td>
|
1139
1090
|
</tr>
|
1140
1091
|
<% end %>
|
1141
1092
|
</table>
|
1142
1093
|
```
|
1143
1094
|
|
1144
|
-
Here we're using `link_to` in a different way. We
|
1145
|
-
|
1146
|
-
first as one argument, and then the final two keys as another argument. The `:method` and `:'data-confirm'`
|
1095
|
+
Here we're using `link_to` in a different way. We pass the named route as the first argument,
|
1096
|
+
and then the final two keys as another argument. The `:method` and `:'data-confirm'`
|
1147
1097
|
options are used as HTML5 attributes so that when the link is clicked,
|
1148
1098
|
Rails will first show a confirm dialog to the user, and then submit the link with method `delete`.
|
1149
1099
|
This is done via the JavaScript file `jquery_ujs` which is automatically included
|
@@ -1153,62 +1103,11 @@ generated the application. Without this file, the confirmation dialog box wouldn
|
|
1153
1103
|
![Confirm Dialog](images/getting_started/confirm_dialog.png)
|
1154
1104
|
|
1155
1105
|
Congratulations, you can now create, show, list, update and destroy
|
1156
|
-
posts.
|
1157
|
-
REST applications, and how we can refactor our Blog app to take
|
1158
|
-
advantage of it.
|
1159
|
-
|
1160
|
-
### Going Deeper into REST
|
1161
|
-
|
1162
|
-
We've now covered all the CRUD actions of a REST app. We did so by
|
1163
|
-
declaring separate routes with the appropriate verbs into
|
1164
|
-
`config/routes.rb`. Here's how that file looks so far:
|
1165
|
-
|
1166
|
-
```ruby
|
1167
|
-
get "posts" => "posts#index"
|
1168
|
-
get "posts/new"
|
1169
|
-
post "posts" => "posts#create"
|
1170
|
-
get "posts/:id" => "posts#show", as: :post
|
1171
|
-
get "posts/:id/edit" => "posts#edit"
|
1172
|
-
patch "posts/:id" => "posts#update"
|
1173
|
-
delete "posts/:id" => "posts#destroy"
|
1174
|
-
```
|
1175
|
-
|
1176
|
-
That's a lot to type for covering a single **resource**. Fortunately,
|
1177
|
-
Rails provides a `resources` method which can be used to declare a
|
1178
|
-
standard REST resource. Here's how `config/routes.rb` looks after the
|
1179
|
-
cleanup:
|
1180
|
-
|
1181
|
-
```ruby
|
1182
|
-
Blog::Application.routes.draw do
|
1183
|
-
|
1184
|
-
resources :posts
|
1185
|
-
|
1186
|
-
root to: "welcome#index"
|
1187
|
-
end
|
1188
|
-
```
|
1189
|
-
|
1190
|
-
If you run `rake routes`, you'll see that all the routes that we
|
1191
|
-
declared before are still available:
|
1192
|
-
|
1193
|
-
```bash
|
1194
|
-
$ rake routes
|
1195
|
-
posts GET /posts(.:format) posts#index
|
1196
|
-
POST /posts(.:format) posts#create
|
1197
|
-
new_post GET /posts/new(.:format) posts#new
|
1198
|
-
edit_post GET /posts/:id/edit(.:format) posts#edit
|
1199
|
-
post GET /posts/:id(.:format) posts#show
|
1200
|
-
PATCH /posts/:id(.:format) posts#update
|
1201
|
-
PUT /posts/:id(.:format) posts#update
|
1202
|
-
DELETE /posts/:id(.:format) posts#destroy
|
1203
|
-
root / welcome#index
|
1204
|
-
```
|
1205
|
-
|
1206
|
-
Also, if you go through the motions of creating, updating and deleting
|
1207
|
-
posts the app still works as before.
|
1106
|
+
posts.
|
1208
1107
|
|
1209
1108
|
TIP: In general, Rails encourages the use of resources objects in place
|
1210
|
-
of declaring routes manually.
|
1211
|
-
|
1109
|
+
of declaring routes manually.
|
1110
|
+
For more information about routing, see
|
1212
1111
|
[Rails Routing from the Outside In](routing.html).
|
1213
1112
|
|
1214
1113
|
Adding a Second Model
|
@@ -1722,7 +1621,7 @@ class CommentsController < ApplicationController
|
|
1722
1621
|
Now if you try to create a new post, you will be greeted with a basic HTTP
|
1723
1622
|
Authentication challenge
|
1724
1623
|
|
1725
|
-
![Basic HTTP Authentication Challenge](images/challenge.png)
|
1624
|
+
![Basic HTTP Authentication Challenge](images/getting_started/challenge.png)
|
1726
1625
|
|
1727
1626
|
What's Next?
|
1728
1627
|
------------
|