// Create adds a Widget to the DB. This function is mapped to the
// path POST /widgets
Create adds a Widget to the DB. This function is mapped to the
path POST /widgets
// Create adds a Widget to the DB . This function is mapped to the // path POST / widgets
func (v WidgetsResource) Create(c buffalo.Context) error {
	widget := &models.Widget{}
	if err := c.Bind(widget); err != nil {
		return errors.WithStack(err)
	}
	tx, ok := c.Value("tx").(*pop.Connection)
	if !ok {
		return errors.WithStack(errors.New("no transaction found"))
	}
	verrs, err := tx.ValidateAndCreate(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/new.html"))
	}
	c.Flash().Add("success", "Widget was created successfully")
	return c.Redirect(302, "/widgets/%s", widget.ID)
}
func ( v WidgetsResource ) Create ( c buffalo . Context ) error { widget := & models . Widget { } if err := c . Bind ( widget ) ; err != nil { return errors . WithStack ( err ) } tx , ok := c . Value ( " " ) . ( * pop . Connection ) if ! ok { return errors . WithStack ( errors . New ( " " ) ) } verrs , err := tx . ValidateAndCreate ( 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 ) }
Create adds a Widget to the DB.
Create adds a Widget to the DB .
// Allocate an empty Widget // Bind widget to the html form elements // Get the DB connection from the context // Validate the data from the html form // Make widget available inside the html template // Make the errors available inside the html template // Render again the new.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