-1

I'm using rails 4 and backbone in my app. I created delete method with Backbone, but when I try to delete using it, I get this error:

DELETE http://localhost:3000/[object%20Object] 400 (Bad Request)

My delete method:

  deleteBook: (ev) ->
    @model.destroy()
)

Events:

  events:
    "click .delete": "deleteBook"

Pressing on that error, it shows that model type is text/html. So I suppose I have to convert it to json?

If I do like this:

  deleteBook: (ev) ->
    @model.toJSON().destroy()
)

Update:

My destroy method in rails controller (if it matters):

@book = Book.find(params[:id])
@book.destroy

redirect_to '/'

Update2:

My Backbone model:

  $(document).ready ->
    window.Book = Backbone.Model.extend(url: ->
      (if @id then "/books/" + @id else "/books")
       urlRoot: '/books/'
    )

Then nothing happens and I won't get any error. So I think I am doing something wrong here?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Andrius
  • 19,658
  • 37
  • 143
  • 243
  • I think your problem is not in Backbone but in your server. When you use destroy in the model, it starts an ajax request to delete it persistently. – Puigcerber Oct 30 '13 at 11:39
  • @Puigcerber If it's in server how could I fix it? I updated my question with my rails destroy method if it matters for this problem. – Andrius Oct 30 '13 at 11:52
  • The URL that you set up in the model has to be the same you use in Rails. If for example you have in your book model `urlRoot: '/books/'` you need http://localhost:3000/books/[id] to point to your Ruby method. – Puigcerber Oct 30 '13 at 12:41
  • @Puigcerber if I use rails method, it destroys without a problem. How can I point that way? Also I added `urlRoot: '/books/'`, instead of initializing url, so then it gives this error now: `Uncaught TypeError: string is not a function` – Andrius Oct 30 '13 at 12:57
  • are you using MongoDB? @id may actually be an object, which would explain why the string concatenation fails – Micah Roberson Oct 30 '13 at 13:14
  • @MicahRoberson No I'm using Sqlite – Andrius Oct 30 '13 at 13:21
  • I don't know much about Ruby but you can check this http://guides.rubyonrails.org/routing.html – Puigcerber Oct 30 '13 at 13:42
  • And I would remove the line `(if @id then "/books/" + @id else "/books")`. – Puigcerber Oct 30 '13 at 13:45
  • If I remove that, then destroy method stops working.. – Andrius Oct 30 '13 at 13:47
  • `urlRoot: '/books/', idAttribute: 'id',` – Puigcerber Oct 30 '13 at 13:52
  • 1
    @Puigcerber thanks, this one works. – Andrius Oct 30 '13 at 13:55

2 Answers2

1

Why in url locate [object%20Object]?

Check value of @id in this place:

(if @id then "/books/" + @id else "/books")

Value should be a number, but i think you have an object in @id

1

After been discussing in the comments I'm posting it as an answer so you can accept it to close the issue.

If you are using you Backbone model outside of a collection you have to define explicitly the URL. You can define as well the id attribute.

$(document).ready ->
  window.Book = Backbone.Model.extend(
    urlRoot: '/books/'
    idAttribute: 'id'
  )

When you call destroy() in that model Backbone will start an ajax request to delete permanently the model using an HTTP request DELETE http://localhost:3000/books/:id.

So you need to map that URL to your Ruby controller following this link.

Cheers.

mu is too short
  • 426,620
  • 70
  • 833
  • 800
Puigcerber
  • 9,814
  • 6
  • 40
  • 51