Route for New Pages3:08 with Jay McGavren
Our users can view a list of all Pages. They can click a link to view an individual Page. What they CAN'T do right now is create new pages; we had to go into the Rails console to do that. Our next task is to set up a form so that it's easy for our USERS to create Pages, too.
HTTP GET requests are intended for getting data from the server. HTML forms are just another type of data. So when a user wants to create a new Page, the browser should send an HTTP GET request to retrieve a blank HTML form.
When you're adding new data, though, you're supposed to use an HTTP POST request instead. So, when you click the submit button on an HTML form, that's what your browser sends to the server: a POST request with the form contents.
For this reason, setting up a form to add new instances of a resource usually requires two routes in your
Suppose we were setting up a form for new
Page records. We'd first need a route to GET the HTML form:
get '/pages/new', to: 'pages#new'
Then we'd need a second route to accept the POST request when the form is submitted:
post '/pages', to: 'pages#create'
Our users can view a list of all pages, 0:04 they can click a link to view an individual page. 0:07 What they can't do right now is create new pages. 0:10 We had to go into the rails console to do that. 0:12 Our next task is to set up a form so that it's easy for 0:15 our users to create pages too. 0:18 We're going to need to set this form up in two parts. 0:21 First we need to set up a controller action that takes an http GET request and 0:24 response with an html form. 0:29 Then we'll need a second controller action that takes an http 0:31 POST request with the form data and saves it to the database. 0:34 HTTP GET requests are intended for getting data from the server. 0:39 HTML forms are just another type of data. 0:43 So when a user wants to create a new page. 0:46 The browser should send an HTTP GET request to retrieve a blank HTML form. 0:47 When you're adding new data though, 0:53 you're supposed to use an HTTP POST request instead. 0:54 So when you click the submit button on the HTML form. 0:58 That's what your browser sends to the server a POST request with the form 1:01 contents. 1:04 The server uses those foreign contents to create a new database record. 1:06 So first we need to set up our app to handle requests for the new page form. 1:10 And the first thing we're going to need is a route for those requests. 1:14 So let's add one here in the config routes.rb file. 1:17 We're getting the form so these will be get requests. 1:22 We'll call the get method by convention the past to create 1:24 a new resource is the pluralized resource name followed by /new so 1:29 we're going to set up a path of /pages /new. 1:33 And we're going to route it to the pages controller new action. 1:38 Now let's try going to our browsers address bar and 1:47 visiting a path of pages/new. 1:49 We get a strange error and return. 1:53 A record not found error. 1:55 It says it couldn't find a page record with an ID of new. 1:57 Remember how we set up our route and routes that are be to send requests for 2:01 /pages/one/pages/two/etc to the pages controllers show action. 2:05 The problem here is that rails is seeing a request for 2:12 the /pages new path and it thinks that new is a page ID. 2:14 When multiple routes match a request path the one that was defined first 2:20 takes priority. 2:24 So the route for get requests with a path of /pages/ID 2:25 is taking priority over the route with a path of /pages/new and 2:29 the new part of our path is being treated as an ID. 2:34 To fix this we need to move the /pages/new route before 2:37 the /pages/ID route so it can take priority. 2:41 Now let's reload the page. 2:46 We'll see a different error, the action new could not be found for 2:47 PagesController. 2:51 This is a good thing. 2:53 Now the /pages/new route is taking priority and 2:54 it's trying to round to the new method on pages controller as it should be. 2:58 We just don't have a new method defined yet. 3:02 But that's easy to fix, we'll take care of that next. 3:05
You need to sign up for Treehouse in order to download course files.Sign up