// Update changes a Widget in the DB. This function is mapped to
// the path PUT /widgets/{widget_id}
Update changes a Widget in the DB. This function is mapped to
the path PUT /widgets/{widget_id}
// Update changes a Widget in the DB . This function is mapped to // the path PUT / widgets / { widget_id }
func (v WidgetsResource) Update(c buffalo.Context) error {
	tx, ok := c.Value("tx").(*pop.Connection)
	if !ok {
		return errors.WithStack(errors.New("no transaction found"))
	}
	widget := &models.Widget{}
	if err := tx.Find(widget, c.Param("widget_id")); err != nil {
		return c.Error(404, err)
	}
	if err := c.Bind(widget); err != nil {
		return errors.WithStack(err)
	}
	verrs, err := tx.ValidateAndUpdate(widget)
	if err != nil {
		return errors.WithStack(err)
	}
	if verrs.HasAny() {
		c.Set("widget", widget)
		c.Set("errors", verrs)
		return c.Render(422, render.R.HTML("widgets/edit.html"))
	}
	c.Flash().Add("success", "Widget was updated successfully")
	return c.Redirect(302, "/widgets/%s", widget.ID)
}
func ( v WidgetsResource ) Update ( c buffalo . Context ) error { tx , ok := c . Value ( " " ) . ( * pop . Connection ) if ! ok { return errors . WithStack ( errors . New ( " " ) ) } widget := & models . Widget { } if err := tx . Find ( widget , c . Param ( " " ) ) ; err != nil { return c . Error ( 404 , err ) } if err := c . Bind ( widget ) ; err != nil { return errors . WithStack ( err ) } verrs , err := tx . ValidateAndUpdate ( widget ) if err != nil { return errors . WithStack ( err ) } if verrs . HasAny ( ) { c . Set ( " " , widget ) c . Set ( " " , verrs ) return c . Render ( 422 , render . R . HTML ( " " ) ) } c . Flash ( ) . Add ( " " , " " ) return c . Redirect ( 302 , " " , widget . ID ) }
Update changes a Widget in the DB.
Update changes a Widget in the DB .
// Get the DB connection from the context // Allocate an empty Widget // Bind Widget to the html form elements // Make widget available inside the html template // Make the errors available inside the html template // Render again the edit.html template that the user can // correct the input. // If there are no errors set a success message // and redirect to the widgets index page