Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails, def destroy, is not responding with the destroy.js.erb, why?

Rails, def destroy, is not responding with the destroy.js.erb

Here is my method:

  # DELETE /Groups/1
  # DELETE /Groups/1.xml
  def destroy

    @group = Group.find(params[:id])
    @group.destroy

    respond_to do |format|
      format.js
    end
  end

In the view I have:

<a href="/groups/122" data-confirm="Are you sure?" data-method="delete" rel="nofollow" >Delete</a>

However on delete the log shows:

S

tarted POST "/groups/128" for 127.0.0.1 at Fri Apr 22 22:21:31 -0700 2011
  Processing by GroupsController#destroy as HTML
  Parameters: {"authenticity_token"=>"J+A2DN87qoigNxw97oK6NWqPQvXt7KAwLMAM7Er/eWM=", "id"=>"128"}
.....
Completed 406 Not Acceptable in 372ms

The destory.js.erb is never being called. Any ideas why? Thanks

like image 454
AnApprentice Avatar asked Apr 23 '11 05:04

AnApprentice


1 Answers

ok, well, a couple of issues here:

first,

<a href="/groups/122" data-confirm="Are you sure?" data-method="delete" rel="nofollow" >Delete</a> 

this link is not remote, you can see it in the log you provided:

Processing by GroupsController#destroy as HTML

to make your link submit an ajax request add :remote => true ( the same way you already have :confirm => 'Are you sure?' and :method => :destroy )

second, you should disable layout rendering when responding with javascript.

So, your action might look like:

  respond_to do |format|
      format.js { render :template => 'groups/destroy.js.erb', :layout => false }
  end

To make it easier, I have added this to my contoller:

layout Proc.new { |controller| controller.request.xhr?? false : 'application' }

so that layout won't be rendered if request is of xhr type. Then you could leave you action as it is now and it should work

like image 73
Vlad Khomich Avatar answered Oct 12 '22 10:10

Vlad Khomich