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
开发者_JS百科
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
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
Have you installed jquery-rails gem?, also verify that you have:
csrf_meta_tag
javascript_include_tag :defaults
in your layout
P.S seems to be your controller cannot respond to js format, you can also try to add
respond_to :html, :js in your controller's class like this:
class MyController < ActionController
respond_to :html, :js
....
def destroy
...
end
end
Please let me know if it helps you
Check the output of native rails helper for delete links it returns sth like
<a onclick="if (confirm('Are you sure?'))
{ var f = document.createElement('form');
f.style.display = 'none';
this.parentNode.appendChild(f);
f.method = 'POST';
f.action = this.href;
var m = document.createElement('input');
m.setAttribute('type', 'hidden');
m.setAttribute('name', '_method');
m.setAttribute('value', 'delete');
f.appendChild(m);
var s = document.createElement('input');
s.setAttribute('type', 'hidden');
s.setAttribute('name', 'authenticity_token');
s.setAttribute('value', 'GAsTX1/XJAAoXBxJuE28EPW9jraQd5N39WeeuvakLWA=');
f.appendChild(s);
f.submit(); };
return false;"
href="/categories/1">Destroy</a>
精彩评论