// Create adds a Contact to the DB. This function is mapped to the
// path POST /contacts
Create adds a Contact to the DB. This function is mapped to the
path POST /contacts
// Create adds a Contact to the DB . This function is mapped to the // path POST / contacts
func (v ContactsResource) Create(c buffalo.Context) error {
	contact := &models.Contact{}
	if err := c.Bind(contact); err != nil {
		return errors.WithStack(err)
	}
	tx := c.Value("tx").(*pop.Connection)
	verrs, err := tx.ValidateAndCreate(contact)
	if err != nil {
		return errors.WithStack(err)
	}
	if verrs.HasAny() {
		c.Set("contact", contact)
		c.Set("errors", verrs)
		return c.Render(422, render.R.HTML("contacts/new.html"))
	}
	c.Flash().Add("success", "Contact was created successfully")
	return c.Redirect(302, "/")
}
func ( v ContactsResource ) Create ( c buffalo . Context ) error { contact := & models . Contact { } if err := c . Bind ( contact ) ; err != nil { return errors . WithStack ( err ) } tx := c . Value ( " " ) . ( * pop . Connection ) verrs , err := tx . ValidateAndCreate ( contact ) if err != nil { return errors . WithStack ( err ) } if verrs . HasAny ( ) { c . Set ( " " , contact ) c . Set ( " " , verrs ) return c . Render ( 422 , render . R . HTML ( " " ) ) } c . Flash ( ) . Add ( " " , " " ) return c . Redirect ( 302 , " " ) }
Create adds a Contact to the DB.
Create adds a Contact to the DB .
// Allocate an empty Contact // Bind contact to the html form elements // Get the DB connection from the context // Validate the data from the html form // Make contact 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 contacts index page