1

I'm having this annoying problem with Rails 3 (ruby 1.9.2) and nested resources. In my routes:

resources :lists do
  resources :items, only: [:destroy, :update, :create]
end

My ItemsController has respond_to :json at the beginning and #destroy looks like this:

def destroy
  @item = Item.find(params[:id])
  @item.destroy
  respond_with @list, @item
end

The link to destroy the item:

<%= link_to 'x', list_item_path(@list, item), method: :delete, remote: true %>

Which translates into correct html, like:

<a href="/lists/1/items/52" data-method="delete" data-remote="true" rel="nofollow">x</a>

When I click the link, my item is correctly deleted but the method always returns {}. I've been trying to tinker with it but can't get anything different from that empty JSON object!

Every other method (#create and #update) works and returns JSON objects as expected.

rikas
  • 482
  • 4
  • 25

1 Answers1

2

I'm not entirely sure what else you would expect it to do. If the item is deleted, the JSON response will be empty since it can't return a destroyed object.

With create and update, the item object still exists so it will return that as a JSON hash.

iwasrobbed
  • 46,496
  • 21
  • 150
  • 195
  • I believe that's not correct. The item is deleted on the DB but the `@item` object still exists. Also, if I comment the `@item.destroy` line it doesn't even delete the record from the DB and still returns {} – rikas Jan 20 '12 at 00:13
  • 1
    Ok so I kept my investigation on this issue and you are almost right. If it was a GET then I would get the object, but when you use DELETE or PUT rails only allows you to get {} and status OK as response. – rikas Jan 20 '12 at 02:58