An Edit Route

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.

0:00

[MUSIC]

0:04

Now or users can create a new page, but
they better get it right the first time

0:07

because right now they can't go back and
change what they've entered.

0:11

Maybe we should fix that.

0:13

Let's give them a form to allow
them to update existing pages.

0:17

So now we have a form where
we can add a new page.

0:20

We need a form that's
very similar to this but

0:22

that allows us to edit an existing page.

0:25

When a user wants to create a new page,

0:27

the browser sends an HTTP get
request to retrieve a blank form.

0:31

When they submit the new form,

0:32

the browser sends an HTTP post request to
create a new page record on the server.

0:38

And when a user wants to
edit an existing page,

0:40

the browser needs to send an HTTP get
request to retrieve that form as well.

0:45

The difference is that the edit form
will be pre populated with the existing

0:49

object's data.

0:50

Another difference is that when you're
modifying existing data on the server,

0:54

rather than adding new data,

0:55

you're supposed to use a put or
patch request rather than a post request.

1:00

So when you click the submit
button on the edit form,

1:03

that's what your browser sends
to the server, a patch request.

1:07

So we'll need a controller action
that takes an HTTP get request and

1:10

responds with an HTML form,
just like the new page form.

1:14

Then we'll need a second controller action
that takes an HTTP patch or put request.

1:20

This action should take the modified form
data and update the record on the server.

1:24

In this video will setup the action
that handles get requests for

1:27

a pre populated form.

1:29

First we're going to need a route.

1:30

Just as we did with the form for
new pages,

1:33

we added it as a get route and
then we started the path with /pages.

1:38

We'll do the same for
the route to get the edit form.

1:40

We start with get, and
the route will start with /pages.

1:45

But now we get to the key difference
between the route to get the form to

1:48

create a new page and
the route to edit an existing page.

1:52

We need a way to specify
which page we're editing.

1:55

We'll do that using a URL parameter,

1:57

just like we did with the route
to show a particular page.

2:00

So we'll have a URL parameter onto
the end of this routes path ID.

2:05

We already have that route for
get requests for /pages/id though.

2:09

We need a way to distinguish this route
from the route to the page's controller

2:13

show action.

2:14

So we'll add a slash edit segment
on to the end of the path.

2:18

That way people won't be confused about
whether a particular path is to show or

2:21

edit a page.

2:23

Rails won't be confused either.

2:25

With the /edit segment we can put this
route in any order within routes.rb

2:29

that we want.

2:30

We won't have to worry about a different
route taking priority over it.

2:34

We of course need to specify
which controller and

2:36

action we're routing requests to.

2:38

We'll send them to the pages controllers
edit action by adding to: 'pages#edit.

2:45

Now let's define that edit
method on the pages controller.

2:49

App, Controllers, Pages_controller,
and we'll add a method at the bottom.

2:59

We'll need to take the ID
parameter from the URL and

3:01

look up an existing page object,
just like we do in the show action.

3:07

In fact the code will be identical.

3:09

So we'll assign to the page instance
variable and will call Page.find and

3:16

pass at the ID that we
get from (params[:id]).

3:20

Since we just learned
about strong parameters,

3:23

you may be wondering won't we get
a forbidden attributes error here?

3:26

The answer is that we're not creating or
updating a model object.

3:30

We're just finding one.

3:32

Creating or updating an object based
on parameters usually involves

3:35

reading several parameters at once.

3:37

And that offers malicious users a chance
to sneak unwanted parameters in.

3:41

But to find a record we
only need one parameter and

3:44

we explicitly specify which
parameter we're using.

3:48

So there's no need to call
permit on the parameters object.

3:51

Okay we've got a route,
we've got a controller,

3:53

and we've loaded the model object.

3:55

Now we're going to need a view template.

3:57

Since this is the pages
controllers edit action,

4:00

Rails is going to look in the app
views pages directory for

4:05

a file named edit.html.erb.

4:10

So let's create that file.

4:12

For now we'll just put some
place holder text here.

4:15

Form to edit, and we'll

4:20

embed the pages title.

4:26

Form to edit page,
title page will go here.

4:30

We should be ready to visit this path now.

4:33

The edit path for a particular record
is the same as the show path, but

4:36

with edit tacked onto the end.

4:40

So let's visit the show path for
one of our pages and

4:43

add /edit on to the end of the URL.

4:47

We'll see our temporary view template with
the title for this page embedded in it.

4:52

Of course we don't want to users to
have to alter the URL themselves.

4:56

Let's give them a link that can click.

4:58

First, we'll go into routes.rb and
set up a path helper we can use.

5:02

At the end of the edit row will have
a name for the route, as 'edit_page'.

5:08

That will create and
it page path method that we can call.

5:13

Then we'll go into the show view for
an individual page, at app, views,

5:17

pages, show.html.erb, and
will embed the edit link at the bottom.

5:23

So it will include an output
embedding tag here.

5:26

We'll call link_to, and

5:29

we'll use link text of 'Edit',
so edit this page.

5:34

To get the link path,
we'll call our new edit_page_path_method.

5:39

And we'll pass at our current page object.

5:44

Close out the embedding tag there,
save our file.

5:48

And now we can just bring up
the show view for any page.

5:51

Let's refresh it.

5:52

Whoops, looks like it might
have made a syntax error here.

5:55

Let's go back to the editor and fix this.

5:57

That's right I forgot to separate
the parameters to link to with a comma.

6:02

So first we need the link text and then we
need the link path separated by a comma.

6:08

Let's save that reload again.

6:11

There we go, there's our Edit
link at the bottom of the screen.

6:14

Now we can just show any page and click
the edit link to bring up the Edit View.

6:18

Of course we need to display an actual
form for editing the record here.