rikas
rikas

Reputation: 490

Rails 3 destroy method respond_with JS using nested resources

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.

Upvotes: 1

Views: 526

Answers (1)

iwasrobbed
iwasrobbed

Reputation: 46703

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.

Upvotes: 2

Related Questions