Rails Resources3:50 with Jay McGavren
We have routes leading to seven action methods on our controller... It took us seven lines of code in our routes.rb file to set all these routes up. But because these are the seven conventional routes for any given Rails resource, and you're going to need to set up these same seven routes for most resources you create, there's a shortcut you can use instead.
These are the seven conventional routes that almost any Rails resource will have:
Rails.application.routes.draw do get '/pages', to: 'pages#index' post '/pages', to: 'pages#create' get '/pages/new', to: 'pages#new', as: 'new_page' get '/pages/:id', to: 'pages#show', as: 'page' get '/pages/:id/edit', to: 'pages#edit', as: 'edit_page' patch '/pages/:id', to: 'pages#update' delete '/pages/:id', to: 'pages#destroy' end
You can replace all of the above with a single line:
Rails.application.routes.draw do resources :pages end
resources :pages will create all the same routes (named in the same way) as the preceding seven lines of code.
So now we can create, read, update, and delete pages. 0:00 We've set up routes with all the crowd operations for our new resource. 0:03 We can get a list of all the routes we've configured by visiting 0:08 the /rails/info/routes path, while our server is running. 0:11 We can also get the same list in the terminal with the rails routes command. 0:18 Stop the server if it's running and type bin/rails routes. 0:22 Each of the configured routes will be listed on its own line. 0:27 First, you'll see all the routes for posts. 0:30 Then all the routes for pages. 0:33 There are several columns in the listing. 0:36 First is the prefix. 0:38 If you've named the round this is the prefix that will be added to the names of 0:40 path and URL helper methods. 0:43 So if there is a prefix of pages it means there is a page's path method available 0:45 if there is a prefix of new page. 0:51 It means there is a new page path method available at set or some. 0:53 Other way to refer to an HTTP request type or HTTP request method is as an HTTP verb. 0:57 So that's what the verb column shows we have get requests for 1:04 the list of all pages as well as individual pages. 1:07 We also have get requests for the new page and edit page forms. 1:11 We have a post request for creating a page. 1:16 We have a patch request for updating an existing page and 1:19 we have a delete request for destroying a page. 1:22 The URI pattern column shows or requests path. 1:25 The path is considered together with the HTTP verb when matching a route. 1:29 So, a GET request for 1:33 the pages path would be routed to the pages controllers index action method. 1:34 But, a POST request for 1:40 the pages path would be routed to the create action method. 1:41 And the last that column has the controller and action method 1:46 in the same format we've been using within the routes that are B file. 1:50 If a route gets matched this is the controller in action that it will 1:53 be sent to. 1:56 We have routes leading to seven action methods on our controller. 1:58 Index and show for reading pages. 2:01 New and create for creating new pages. 2:05 Edit and update for updating existing pages. 2:08 And destroy for deleting pages. 2:13 It took us seven lines of code in our outside. 2:15 Our be filed to set all these routes up, and it's good that we did in the future. 2:18 You're going to need to know how to manually set up get 2:22 routes post routes etc. 2:24 But because these are the seven conventional routes for any given rails 2:27 resource and you're going to need to set up these same seven routes for 2:30 most resources you create there's a shortcut you can use instead. 2:34 You can replace all the routes for pages with a single line. 2:38 Resources and a symbol of pages. 2:42 The resources method takes a ruby symbol with a controller name and 2:47 sets up routes to those same seven action methods on that controller. 2:50 Index, show, new, create, edit, update, and destroy. 2:54 It also sets up names for the routes where appropriate. 2:59 So that the path helper methods like page path, 3:01 and edit page path are still available. 3:04 If we rerun bin/rails routes, 3:07 you'll see that the list of routes is nearly identical. 3:11 The only difference is that the resources method maps both put and 3:14 patch requests for updating an existing page. 3:17 Let's relaunch the server and confirm that everything still works the same. 3:21 We can create a new page. 3:29 We can update it using the update form, and we can delete it. 3:33 Everything's still being routed to the same controller actions. 3:41 But with much less code in our routes that are BE file. 3:44 So that's our first code saving improvement. 3:47 Just one more to go. 3:49
You need to sign up for Treehouse in order to download course files.Sign up