Deleting objects via ajax in rails 3.1rc4
I have a rails 3.1 app and i cant seem to delete my images via ajax. my code smaple is
def destroy
@photo = Photo.find(params[:id])
@photo.destroy
respond_to do |format|
format.html { redirect_to(user_photos_path(current_user)) }
format.js { render :nothing => true }
end
end
then in my views i have
<%= link_to '(delete)', profile_photo_path(photo.profile, photo), :method => :delete if me %>
in my application.js i have
$('.delete_post').bind('ajax:success', function() {
$(this).closest('tr').fadeOut();
});
it does not work for me. it performs the action but it does not fad开发者_如何学编程e out
In your ajax:success callback this
is not bound to the .delete_post link as you expect it to be. Maybe you could add IDs to your links, then in your controller or template generate some javascript like $("#photo_17_delete").closest('tr').fadeOut();
. That's just an example of one way to handle it.
It's an old question but I just wanted to point out that it seems like it's a bad practice to use links to actions that modify or destroy your data.
You should be using to_button instead
http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-button_to
精彩评论