sequelize model:generate --name Category --attributes title:string,color:string
sequelize db:migrate
const Category = require('../models').Category; | |
module.exports = { | |
create: function(req,res){ | |
Category.create({ | |
title: req.body.title, | |
color: req.body.color | |
}).then(result =>{ | |
res.json(result); | |
}).catch(err=>{ | |
console.log(err); | |
res.json(err); | |
}); | |
}, | |
new: function(req,res){ | |
res.render('categories/new'); | |
}, | |
index: function(req,res){ | |
Category.findAll().then(function(categories){ | |
res.render('categories/index',{categories}); | |
}); | |
}, | |
show: function(req,res){ | |
Category.findByPk(req.params.id).then(function(category){ | |
res.render('categories/show',{category}); | |
}); | |
}, | |
update: function(req,res){ | |
Category.update({ | |
title: req.body.title, | |
color: req.body.color | |
},{ | |
where: {id: req.params.id} | |
}).then(function(response){ | |
res.redirect('/categories/'+req.params.id); | |
}); | |
}, | |
edit: function(req,res){ | |
Category.findByPk(req.params.id).then(function(category){ | |
res.render('categories/edit',{category:category}); | |
}); | |
}, | |
destroy: function(req,res){ | |
Category.destroy({ | |
where: {id: req.params.id} | |
}).then(function(contadorElementosEliminados){ | |
res.redirect('/categories'); | |
}) | |
} | |
}; |
const express = require('express'); | |
let CategoriesController = require('../controllers/categories'); | |
let router = express.Router(); | |
router.route('/categories').get(CategoriesController.index).post(CategoriesController.create); | |
router.get('/categories/new',CategoriesController.new); | |
router.get('/categories/:id/edit',CategoriesController.edit); | |
router.route('/categories/:id').get(CategoriesController.show).put(CategoriesController.update).delete(CategoriesController.destroy); | |
module.exports = router; |
doctype html | |
html | |
head | |
title Editar Categoria | |
body | |
form(action=`/categories/${category.id}?_method=PUT` method="POST") | |
textarea(name="title" placeholder="Qué categoria vas a guardar")=category.title | |
input(type="submit" value="Guardar") |
doctype html | |
html | |
head | |
title Categorías | |
body | |
ul | |
each category in categories | |
li=category.title | |
doctype html | |
html | |
head | |
title Nueva Categorias | |
body | |
form(action="/categories" method="POST") | |
textarea(name="title" placeholder="Qué Categoria vas a guardar") | |
br | |
textarea(name="color" placeholder="color") | |
input(type="submit" value="Guardar") |
doctype html | |
html | |
head | |
title Categorías | |
body | |
h1= category.title | |
form(action=`/categories/${category.id}?_method=DELETE` method="POST") | |
input(type="submit" value="Eliminar") |
Muchas gracias.
Sólo cambiaría lo siguiente en el archivo new.pug
doctype html
html
head
title Nuevas Categorias
body
form(action="/categories" method="POST")
input(type='text' name="title" placeholder='Título de la categoría')
input(type='color' name="color" placeholder="color")
input(type="submit" value="Guardar")
Thanks!
Excelente.....