Notes Edit Page

Learn to implement an API to edit/update a new note/record in a Beego application.

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.

Press + to interact
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 ...