Ajax with layout templates

Hello, Here is the situation:

I have a List action that lists items for me (scaffold generated), now I need to the destroy method to be ajaxed, I wrote the following:

(Yes, I really am THIS far behind in list reading. *sigh*)

AN@S wrote:

Hello, Here is the situation:

I have a List action that lists items for me (scaffold generated), now I need to the destroy method to be ajaxed, I wrote the following:

################################ <%=link_to_remote ('Destroy',                             :update => "main",                             :url => {:action => 'destroy', :id => @category},                             :confirm => 'Are you sure?',                             :post => true,                             :loading => "document.getElementById('loading').style.display='inline'",                             :loaded => "document.getElementById('loading').style.display='none'")%></td> #################################   

An easier way to show your "loading" image is to use some of the Prototype helpers:

:loading => "Element.show('loading')",

OK, everything's going fine except that destroy method must -of course- redirect the page to the list action:

################################## def destroy     Category.find(params[:id]).destroy     redirect_to :action => 'list' ###################################

Now the problem is that layout is regenerated for the list action after the redirect so I get a doubled layout with the page. I may not except the list method from the layout because it will not show the page layout when listing items.

I have searched the rails documentation and found this: render :action => "list", :layout => false

But this didn't work, it caused an error.

Do you suggest some other solution?

It looks like you're kind of mixing AJAX and non-AJAX techniques here.

Your link_to_remote is expecting HTML which will replace your "main" element. Rather than re-direct to your list method you can instead use render to generate the HTML for "main". If your "list" method is simple you can just duplicate the "list" method ending with "render :action => 'list', :layout => false". (The ":layout => false" will prevent the layout from being duplicated inside your "main" section.)

If the "list" method is longer you can extract the common code into a method that both destroy and list call before each method does it's render action.

Hope that helps,

David