0

I´m doing a CRUD app in Sails, but the delete method doesn´t works fine.

The Homepage.ejs view is a simple form:

<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>

`

The ToDoController of DELETE and findOne methods:

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')});
    });
}

The routes:

'/': 'ToDoController.index',


'POST /todo'   : 'ToDoController.create',
  'PUT /todo/:id': 'ToDoController.update',
  'DELETE /todo/:id' : 'ToDoController.delete',
  'GET /detail/:id': 'ToDoController.findOne'

And the details.ejs view:

<h1> ID task: <%= id %> </h1>
<h3>Title: <%= title %></h3>
<p> Task: <%= description %></p>

Any idea??

1 Answer 1

2

Try using your console to debug your application

delete:function(req, res){
    console.log(req.param('id'));
    ToDo.destroy(req.param('id')).exec(function(err, todo){
        console.log(todo); 
        if(err) return res.serverError();
        return res.redirect('/');
    });
}

If you still have trouble, you should report what your console is outputting.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.