Search⌘ K
AI Features

Notes Edit Page

Understand how to implement editing functionality in a Beego-based notes application. Learn to define routes for editing notes, create handler functions to fetch and update note data, and design view templates with forms for note modification. This lesson guides you through managing URL parameters, processing form submissions, and updating database entries effectively.

Editing a note

Now, we are going to implement routes/API to edit the details of a note.

First, we will create a page that will have a form filled with the details of the note. These details can be edited, and when we submit the form, it updates the note.

Let’s start by defining a new route in the routers/route.go file.

Go (1.18.2)
package routers
import (
"beego_notes/controllers"
beego "github.com/beego/beego/v2/server/web"
)
func init() {
beego.Router("/", &controllers.MainController{})
beego.Router("/notes", &controllers.NotesController{}, "get:NotesIndex")
beego.Router("/notes/new", &controllers.NotesController{}, "get:NotesNewForm")
beego.Router("/notes", &controllers.NotesController{}, "post:NotesCreate")
beego.Router("/notes/:id([0-9]+)", &controllers.NotesController{}, "get:NotesShow")
beego.Router("/notes/edit/:id([0-9]+)", &controllers.NotesController{}, "get:NotesEditPage")
beego.Router("/notes/:id", &controllers.NotesController{}, "post:NotesUpdate")
}

We added two routes to the project: /notes/edit/:id([0-9]+) and /notes/:id. The :id part of the route is a placeholder for the note Id. The [0-9]+ part of the route specifies that the note Id must be a number.

Here are the details of the new routes:

  • Line 15: This route has the get method and the NotesEditPage action. This route means that when the /notes/edit/:id([0-9]+) path is requested, the NotesEditPage() action in the NotesController controller will be executed. The NotesEditPage() action will get the note with the specified id from the database and render the edit note page.

  • Line 16: The /notes/:id route has the post method and the ...