AJAX based record deletion


#1

Hi,

New to RoR and I just wanted to check the correct process flow for
this…

I presently have a controller action:

def destroy_category_association
category = CatalogueCategory.find(params[:id])
product = Product.find(params[:product_id])
product.catalogue_categories.delete(category)
end

I display a list of categories assigned to a product, some of which I
may wish to delete using the link below. The list of categories is
stored in a


structure.

<%= link_to_remote(“Remove Association”, :complete => “new
Effect.Fade(‘cat#{associated_category.id}’)”,
:url => { :action => :destroy_category_association,
:id => associated_category.id,
:params => {:product_id => product_id}},
:confirm => “Are you sure?”) %>

Presently removing an item works correctly, including a Prototype based
Fade effect on the deleted item. However, I am concerned that the
destroy_category_association action does not render output, a fact I am
reminded of in the logs. This isn’t an issue for adding to the list as I
call a render method to insert the partial for the new record.

When deleting should I pass back the entire list of categories minus the
removed item via a render call? Something along the lines of…

render(:partial => “associated_category”, :object => categories, :locals
=> { :product_id => product.id }, :layout => false)

If so how would I handle the Fade effect on the newly deleted item?

Any pointers as to the correct way to achieve this are much appreciated.

Andrew.


#2

Presently removing an item works correctly, including a Prototype based
Fade effect on the deleted item. However, I am concerned that the
destroy_category_association action does not render output, a fact I am
reminded of in the logs. This isn’t an issue for adding to the list as I
call a render method to insert the partial for the new record.

What’s wrong with that? If the ajax call returns no output, that is
totally fine. It does it’s job and the client doesn;t need any further
info from the server about it.

You could use RJS instead. Rather than having the :complete option in
your link_to_remote call, simply create a view called
destroy_category_association.rjs, and in it put:

page[“cat#{@category.id}”].visual_effect :fade

make sure to change your category to @category in your controller

Or you can use redpond_to to delegate the response to a redirect if the
client has javascript turned off

respond_to do |type|
type.html {redirect_to(:action => ‘show_associations’)}
type.js # will render default which is “name_of_action.rjs”
end

But really, having the server return no body text on an ajax call is not
a big deal if the client doesn’t need that info.