Skip to content

Commit de49a77

Browse files
authored
Update README.md
routes update
1 parent 6ba4e5b commit de49a77

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

README.md

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,17 +162,53 @@ mkdir mkdir app/controllers/api && mkdir app/controllers/api/v1
162162
If everything looks right you should see your directory identical as below. <br><br>
163163
<a href="http://tinypic.com?ref=3589c11" target="_blank"><img src="http://i67.tinypic.com/3589c11.png" height="280" width="280" border="0" alt="Image and video hosting by TinyPic"></a>
164164

165-
Now that our versioning is complete, let's test out a model and controller to work with our new url of ```localhost:3000/api/v1```. Let's scaffold a test model/controller and call it ```movies```
165+
Now that our versioning is complete, let's test out a model and controller to work with our new url of ```localhost:3000/api/v1```.
166+
167+
2. Let's scaffold a test model/controller and call it ```movies```
166168

167169
```ruby
168170
rails g scaffold Movies name:string rating:integer
169171

170172
rails db:migrate
171173
```
172174

173-
The Rails engine creates your controller in the default ```/controllers``` directory but we need to move our new controller into the ```api/v1``` directory. You can either move it manually or the following:
175+
The Rails engine creates your controller in the default ```/controllers``` directory but we need to move our new controller into the ```api/v1``` directory.
176+
177+
3. You can either move it manually or the following:
174178

175179
```shell
176180
mv app/controllers/movies_controller.rb app/controllers/api/v1
177181
```
182+
4. Update the Movies Controller
183+
184+
Our newly generated controller does not properly inherit from the namespace api/v1 (We will update the routes later in the tutorial) so let's change our controller class from
178185

186+
```ruby
187+
class MoviesController < ApplicationController
188+
```
189+
TO
190+
191+
```ruby
192+
class Api::V1::MoviesController < ApplicationController
193+
```
194+
195+
5. Update the Routes
196+
Locate to your config folder and open your ```routes.rb``` file.
197+
198+
```ruby
199+
Rails.application.routes.draw do
200+
resources :movies
201+
end
202+
```
203+
204+
If we go to ```localhost:3000/movies``` we will not call the controller. We must update our Routes to:
205+
```ruby
206+
Rails.application.routes.draw do
207+
namespace :api do
208+
namespace :v1 do
209+
resources :movies
210+
end
211+
end
212+
end
213+
```
214+
which allows us to call the json data from ```ruby localhost:3000/api/v1/movies```

0 commit comments

Comments
 (0)