Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Our users can create new wiki pages, but they can't go back and edit the page after that. Our next step should be to provide them with a form that allows editing. First, we'll need to present them with a form that contains the existing page text, so they can modify it.
-
0:00
[MUSIC]
-
0:04
Our users can create new wiki pages, but they can't go back and
-
0:07
edit the page after that.
-
0:09
Our next step should be to provide the multiple form that allows editing.
-
0:13
In the previous stage, we set up a Sinatra route that lets users send a GET request
-
0:17
to retrieve an HTML form for a new wiki page.
-
0:20
Then we added another route that accepts a POST request with the data from
-
0:23
the completed form and saves it as a new page.
-
0:27
In this stage, we'll need to set up another pair of new Sinatra routes.
-
0:31
The first route will accept GET requests and return an HTML form for
-
0:34
a wiki page, just like before.
-
0:36
The difference is that this HTML form will be pre-populated
-
0:40
with an existing page's content.
-
0:42
The second route will accept the completed form data, also just like before.
-
0:46
But there will be a difference here, too.
-
0:48
Since we're updating existing data on the server rather than adding new data,
-
0:53
we need to send a put request, not a post request.
-
0:56
We'll talk more about put requests in a bit.
-
0:59
Let's start by adding an Edit link to the show.erb template.
-
1:02
That will let users view any page and click a link to edit that page.
-
1:06
We're going to want some CSS styling later to visually separate the Edit link from
-
1:10
the page content.
-
1:11
So we'll wrap the link in a DIV element to make it easier to style.
-
1:14
Within the DIV, we'll add an anchor tag to create the link.
-
1:19
We'll set the link text to edit this page.
-
1:26
We need to set the anchor's href attribute to specify what path
-
1:29
the browser should request.
-
1:33
The path needs to start with a slash.
-
1:35
Since this will send a GET request for an HTML form to edit this page, our app needs
-
1:39
to be able to determine which page we're editing based solely on the request path.
-
1:44
So we use an ERB output tag to embed the current page title in the link path.
-
1:49
We set the title instance variable to the current page title in our Sinatra route
-
1:53
for use in the page heading.
-
1:54
We can just reuse that same variable here in the link path.
-
1:58
And will end the path with another segment saying slash edit so
-
2:02
it's clear were editing the page and not viewing it.
-
2:05
Let's start our server app and try this out.
-
2:09
We'll launch preview on port 4567.
-
2:13
So now, if we go view and edit page Type in the path for
-
2:18
an existing page, we'll see an Edit link at the bottom.
-
2:23
If we click it though, we'll see Sinatra doesn't know this ditty.
-
2:26
We'll need to set up a route to accept requests for the edit path.
-
2:30
You remember our get new route within the app.
-
2:32
It renders an ERB template with an HTML form to create a new wiki page.
-
2:38
You also remember our Get title row,
-
2:40
which uses a URL parameter to get a page's title, loads that pages content, and
-
2:45
renders an ERB template to show that content.
-
2:48
This route we're going to create will be like a cross between these two
-
2:51
existing routes.
-
2:51
It'll get a page title from a URL parameter, load content for
-
2:56
that page, and then render an HTML form containing that content.
-
3:02
We're not submitting the HTML form right now, we're getting it.
-
3:05
So we'll be expecting an HTTP GET request.
-
3:08
We'll define our new route using the GET method.
-
3:11
The path will start with a slash, followed by the page title,
-
3:14
which will use a URL parameter the capture title.
-
3:18
Then another slash, and the word edit.
-
3:21
The slash edit segment at the end of the path will keep edit
-
3:23
requests from getting confused with the GET title route.
-
3:26
So it's okay to define this route after the GET title route.
-
3:30
Now, we'll need a block for the edit route.
-
3:34
And in that block, we're going to set up instance variables to hold the page title
-
3:37
and name, just like we did in the route to show a page.
-
3:41
So to get the page title, we're gonna take the title parameter from the URL.
-
3:45
So, params, title,
-
3:47
which will contain whatever segment of the URL was at this position.
-
3:54
We'll assign that to an instance variable called Title.
-
3:57
Then we need to use that title to load content for the page.
-
4:00
We'll call the page content method.
-
4:04
And we'll just reuse the title instance variable as a parameter to page content.
-
4:11
We'll assign the content we get back to an instance variable named content.
-
4:17
Lastly, we're going to use those values in those instance variables to render an ERB
-
4:22
template named edit.
-
4:25
Let's be sure to save that.
-
4:28
That call to ERB edit will look for a file within the views directory named Edit.ERB.
-
4:33
So let's create that file now.
-
4:39
In that file, we'll need to set up an HTML form to edit an existing wiki page.
-
4:44
That form will actually look very similar to our form to create a new page.
-
4:48
So let's go into the new .ERB template,
-
4:52
copy everything, and paste it into edit.erb.
-
4:56
Then we'll go through and modify it for editing a page instead of creating one.
-
5:01
The heading at the top of the page currently says we're adding a new page,
-
5:04
but we're actually editing one.
-
5:05
So let's update that to say edit page
-
5:10
Followed by the title of the page we're editing
-
5:15
which will use an ERB embed tag to insert.
-
5:20
We'll access the value of the title instance variable which was set
-
5:23
back in our route.
-
5:25
We're going to need to make some changes to how the form is submitted later, so
-
5:28
let's just remove the method and action attributes from the form tag.
-
5:34
We're not going to want users to change the page title as that affects what
-
5:37
file it will be saved into.
-
5:38
So, let's remove the entire field set for the title field.
-
5:43
We will still want the content field and the submit button.
-
5:46
So we're going to leave those alone.
-
5:48
Be sure to save that when we're done.
-
5:50
Let's see how our edit form looks so far.
-
5:52
We'll restart our app.
-
5:57
Visit an existing page, reload it, and click the edit link.
-
6:02
Our edit route is invoked, and it loads our form template.
-
6:05
You can see the page title appear at the top.
-
6:09
The content text area is blank, though.
-
6:11
We're going to want it pre-populated with the existing page content so
-
6:15
the user can make changes to it.
-
6:18
The browser will treat any text you place between the opening text area tag and
-
6:21
the closing text area tag as the initial field value,
-
6:24
the value the field has before you make any edits.
-
6:28
For the new wiki page form, the element was empty and so the field was empty.
-
6:32
But this time, we're going to need some text in there.
-
6:34
Let's try adding a placeholder now.
-
6:36
Initial, text.
-
6:41
If we reload the template, we'll see initial text in the form field.
-
6:46
We can use this to set our existing page content as the initial text area value.
-
6:51
Back in our Sinatra route,
-
6:52
we store the existing page content in the content instance variable.
-
6:56
So we can just place an ERB output tag inside of the text area element.
-
7:06
And use it to embed the value of the content instance variable into the HTML
-
7:11
output.
-
7:13
Save that, reload our view.
-
7:17
And we'll see our existing page contents preloaded in the text area, ready for
-
7:21
us to edit them.
-
7:23
As before though, if we submit the form, the data isn't saved.
-
7:26
We're going to need a separate Sinatra route to receive and
-
7:29
process the form data.
-
7:30
We'll set that up next.
You need to sign up for Treehouse in order to download course files.
Sign up