An Edit Route6:24 with Jay McGavren
Now our users can create a new Page. But they'd better get it right the first time, because right now they can't go back and change what they've entered... Maybe we should fix that. Let's give them a form to allow them to update existing Pages.
When a user wants to create a new model object, the browser sends an HTTP GET request to retrieve a blank form. When they submit the new form, the browser sends an HTTP POST request to create a new record on the server.
And when a user wants to edit an existing model object, the browser needs to send an HTTP GET request to retrieve that form as well. The difference is that the edit form will be pre-populated with the existing object's data.
Another difference is that when you're modifying existing data on the server (rather than adding new data), you're supposed to use a PUT or PATCH request rather than a POST request. So when you click the submit button on the edit form, that's what your browser sends to the server: a PATCH request.
[MUSIC] 0:00 Now or users can create a new page, but they better get it right the first time 0:04 because right now they can't go back and change what they've entered. 0:07 Maybe we should fix that. 0:11 Let's give them a form to allow them to update existing pages. 0:13 So now we have a form where we can add a new page. 0:17 We need a form that's very similar to this but 0:20 that allows us to edit an existing page. 0:22 When a user wants to create a new page, 0:25 the browser sends an HTTP get request to retrieve a blank form. 0:27 When they submit the new form, 0:31 the browser sends an HTTP post request to create a new page record on the server. 0:32 And when a user wants to edit an existing page, 0:38 the browser needs to send an HTTP get request to retrieve that form as well. 0:40 The difference is that the edit form will be pre populated with the existing 0:45 object's data. 0:49 Another difference is that when you're modifying existing data on the server, 0:50 rather than adding new data, 0:54 you're supposed to use a put or patch request rather than a post request. 0:55 So when you click the submit button on the edit form, 1:00 that's what your browser sends to the server, a patch request. 1:03 So we'll need a controller action that takes an HTTP get request and 1:07 responds with an HTML form, just like the new page form. 1:10 Then we'll need a second controller action that takes an HTTP patch or put request. 1:14 This action should take the modified form data and update the record on the server. 1:20 In this video will setup the action that handles get requests for 1:24 a pre populated form. 1:27 First we're going to need a route. 1:29 Just as we did with the form for new pages, 1:30 we added it as a get route and then we started the path with /pages. 1:33 We'll do the same for the route to get the edit form. 1:38 We start with get, and the route will start with /pages. 1:40 But now we get to the key difference between the route to get the form to 1:45 create a new page and the route to edit an existing page. 1:48 We need a way to specify which page we're editing. 1:52 We'll do that using a URL parameter, 1:55 just like we did with the route to show a particular page. 1:57 So we'll have a URL parameter onto the end of this routes path ID. 2:00 We already have that route for get requests for /pages/id though. 2:05 We need a way to distinguish this route from the route to the page's controller 2:09 show action. 2:13 So we'll add a slash edit segment on to the end of the path. 2:14 That way people won't be confused about whether a particular path is to show or 2:18 edit a page. 2:21 Rails won't be confused either. 2:23 With the /edit segment we can put this route in any order within routes.rb 2:25 that we want. 2:29 We won't have to worry about a different route taking priority over it. 2:30 We of course need to specify which controller and 2:34 action we're routing requests to. 2:36 We'll send them to the pages controllers edit action by adding to: 'pages#edit. 2:38 Now let's define that edit method on the pages controller. 2:45 App, Controllers, Pages_controller, and we'll add a method at the bottom. 2:49 We'll need to take the ID parameter from the URL and 2:59 look up an existing page object, just like we do in the show action. 3:01 In fact the code will be identical. 3:07 So we'll assign to the page instance variable and will call Page.find and 3:09 pass at the ID that we get from (params[:id]). 3:16 Since we just learned about strong parameters, 3:20 you may be wondering won't we get a forbidden attributes error here? 3:23 The answer is that we're not creating or updating a model object. 3:26 We're just finding one. 3:30 Creating or updating an object based on parameters usually involves 3:32 reading several parameters at once. 3:35 And that offers malicious users a chance to sneak unwanted parameters in. 3:37 But to find a record we only need one parameter and 3:41 we explicitly specify which parameter we're using. 3:44 So there's no need to call permit on the parameters object. 3:48 Okay we've got a route, we've got a controller, 3:51 and we've loaded the model object. 3:53 Now we're going to need a view template. 3:55 Since this is the pages controllers edit action, 3:57 Rails is going to look in the app views pages directory for 4:00 a file named edit.html.erb. 4:05 So let's create that file. 4:10 For now we'll just put some place holder text here. 4:12 Form to edit, and we'll 4:15 embed the pages title. 4:20 Form to edit page, title page will go here. 4:26 We should be ready to visit this path now. 4:30 The edit path for a particular record is the same as the show path, but 4:33 with edit tacked onto the end. 4:36 So let's visit the show path for one of our pages and 4:40 add /edit on to the end of the URL. 4:43 We'll see our temporary view template with the title for this page embedded in it. 4:47 Of course we don't want to users to have to alter the URL themselves. 4:52 Let's give them a link that can click. 4:56 First, we'll go into routes.rb and set up a path helper we can use. 4:58 At the end of the edit row will have a name for the route, as 'edit_page'. 5:02 That will create and it page path method that we can call. 5:08 Then we'll go into the show view for an individual page, at app, views, 5:13 pages, show.html.erb, and will embed the edit link at the bottom. 5:17 So it will include an output embedding tag here. 5:23 We'll call link_to, and 5:26 we'll use link text of 'Edit', so edit this page. 5:29 To get the link path, we'll call our new edit_page_path_method. 5:34 And we'll pass at our current page object. 5:39 Close out the embedding tag there, save our file. 5:44 And now we can just bring up the show view for any page. 5:48 Let's refresh it. 5:51 Whoops, looks like it might have made a syntax error here. 5:52 Let's go back to the editor and fix this. 5:55 That's right I forgot to separate the parameters to link to with a comma. 5:57 So first we need the link text and then we need the link path separated by a comma. 6:02 Let's save that reload again. 6:08 There we go, there's our Edit link at the bottom of the screen. 6:11 Now we can just show any page and click the edit link to bring up the Edit View. 6:14 Of course we need to display an actual form for editing the record here. 6:18 Setting that up will be our next task. 6:22
You need to sign up for Treehouse in order to download course files.Sign up