Reputation: 44066
Ok so I have a delete users button
<button id="delete" data="<%= user_path(@user) %>">Delete User</button>
which is using the jquery ui
$("#delete").button();
and i want the clicking to delete the user so i have
$("#delete").click(function(){
if(confirm("Are you sure?")){
var url = $(this).attr('data');
$.post(url, {_method:'delete'}, null, "script");
document.location = '<%= users_path %>';
return false;
} else {
return false;
}
});
All works great but when the page refreshes I get no flash from my destroy action
def destroy
User.find(params[:id]).destroy
flash[:success] = "User deleted."
redirect_to users_path
end
Any suggestions on how to fix this or restructure what i have done. I cant just use a link_to or button_to for styling purposes
Upvotes: 0
Views: 267
Reputation: 239301
Flash data only exists for the following request. It is being "eaten" by your AJAX postback.
When you post
via $.post
, you're actually making two requests; the delete
requests follows the redirect and issues a get
request to users_path
. That request sees the flash data. When you then set your document.location
to users_path
, there is no longer any flash data to render.
You absolutely should not be handling this through hand-rolled AJAX. Allow your link to be clicked like a regular link and allow Rails' UJS to handle the post-back/confirm dialog. This should in no way impact your ability to style the link unless you're doing something very wrong.
Upvotes: 3