0
我正在Sails中執行一個CRUD應用程序,但刪除方法不能正常工作。在CRAIL中使用DELETE方法在SailsJS
的Homepage.ejs視圖是一個簡單的形式:
<form action="/todo" method="post">
<input type="text" name="title">
<br><br>
<input type="text" name="description">
<br><br>
<button type="submit">Create</button>
</form>
<hr>
<ul>
<% _.each(todos, function(todo){ %>
<li style="<% if(!todo.done){ %>color: red;<% } %>"><b><%= todo.title %></b> - <%= todo.description %> </li><a href="/detail/<%= todo.id %>">Detail</a>
<form action="/todo/<%= todo.id %>" method="post">
<input type="hidden" name="_method" value="put">
<button type="submit">Completed</button>
</form>
<form action="/todo/<%= todo.id %>" method="post">
<input type="hidden" name="_method" value="delete">
<button type="submit">Delete</button>
</form>
<% }) %>
</ul>
`
的DELETE和findOne方法的ToDoController:
delete:function(req, res){
ToDo.destroy(req.param('id')).exec(function(err, todo){
if(err) return res.serverError();
return res.redirect('/');
});
},
findOne: function(req, res){
ToDo.find(req.param('id')).exec(function(err, todos){
if(err) return res.serverError();
return res.view('details', { id: req.param('id'), title: req.param('title'), description:req.param('description')});
});
}
的路線:
'/': 'ToDoController.index',
'POST /todo' : 'ToDoController.create',
'PUT /todo/:id': 'ToDoController.update',
'DELETE /todo/:id' : 'ToDoController.delete',
'GET /detail/:id': 'ToDoController.findOne'
和details.ejs查看:
<h1> ID task: <%= id %> </h1>
<h3>Title: <%= title %></h3>
<p> Task: <%= description %></p>
任何想法??