Skip to content

Instantly share code, notes, and snippets.

@neerajsingh0101
Created September 8, 2011 12:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neerajsingh0101/1203326 to your computer and use it in GitHub Desktop.
Save neerajsingh0101/1203326 to your computer and use it in GitHub Desktop.
making jquery-ujs work with delete
// index.html.erb page for users
<%= link_to 'Destroy', user, :confirm => 'Are you sure?', :method => :delete, :remote => true, 'data-type' => 'json' %>
//users_controller
def destroy
@user = User.find(params[:id])
respond_to do |format|
format.html { redirect_to(users_url) }
format.js {
render :json => {'name' => "#{@user.name} has been deleted"}
}
end
end
//application.js
$('a[data-method="delete"]').live('ajax:success',
function(e, data, textStatus, jqXHR){
alert(data.name + ' has been deleted');
}
);
@anexiole
Copy link

anexiole commented Sep 9, 2011

Lines 12 and 22 seem to be doing the same thing (ie. they have 'has been deleted' in the return string).

Did you mean to have line 12 without 'has been deleted' in the return string?

Line 12: render :json => {'name' => "#{@user.name}"}

@neerajsingh0101
Copy link
Author

You are right. Line 22 should just say

alert(data.name);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment