// view for update object func (this *CategoryAdminRouter) Update() { form := post.CategoryAdminForm{Id: this.object.Id} if this.ValidFormSets(&form) == false { return } // get changed field names changes := utils.FormChanges(&this.object, &form) url := fmt.Sprintf("/admin/category/%d", this.object.Id) // update changed fields only if len(changes) > 0 { form.SetToCategory(&this.object) if err := this.object.Update(changes...); err == nil { this.FlashRedirect(url, 302, "UpdateSuccess") return } else { beego.Error(err) this.Data["Error"] = err } } else { this.Redirect(url, 302) } }
// view for new object save func (this *CategoryAdminRouter) Save() { form := post.CategoryAdminForm{Create: true} if this.ValidFormSets(&form) == false { return } var cat models.Category form.SetToCategory(&cat) if err := cat.Insert(); err == nil { this.FlashRedirect(fmt.Sprintf("/admin/category/%d", cat.Id), 302, "CreateSuccess") return } else { beego.Error(err) this.Data["Error"] = err } }
// view for edit object func (this *CategoryAdminRouter) Edit() { form := post.CategoryAdminForm{} form.SetFromCategory(&this.object) this.SetFormSets(&form) }