Notes Edit Page
Learn to implement an API to edit/update a new note/record in a Beego application.
We'll cover the following...
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.
package routersimport ("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 theNotesEditPage
action. This route means that when the/notes/edit/:id([0-9]+)
path is requested, theNotesEditPage()
action in theNotesController
controller will be executed. TheNotesEditPage()
action will get the note with the specifiedid
...