I’m coming across some odd behaviour when I try to delete a user from my project in Rails.
Briefly, I have a link in my view which allows an admin user to delete another user:
<%= link_to 'Delete User', user, :method => :delete, :confirm => "Are you sure?" %>
And here is the code in my user controller:
# DELETE /users/1
# DELETE /users/1.json
def destroy
@user = User.find(params[:id])
@user.destroy
respond_to do |format|
format.html { redirect_to users_url }
format.json { head :no_content }
end
end
However, while the user is deleted, the popup “Are you sure?” box appears twice, and then the following error appears:
ActiveRecord::RecordNotFound in UsersController#destroy
Couldn't find User with id=xxx
Where “xxx” is the user ID that has just been deleted.
Doing a bit of searching, some people have suggested that this is to do with the way JavaScript is loaded in my project, and possibly a conflict with jQuery (I am using the jquery-rails gem). However, my application.html.erb file seems fine:
<%= stylesheet_link_tag "application","http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/smoothness/jquery-ui.css" %>
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
As does my application.js file:
//= require jquery
//= require jquery_ujs
//= require jquery-ui
//= require_tree .
So, I’m not sure why this is happening – this is the first time I’ve encountered the error.
Anyone like to point me in the right direction?
Thanks!
You probably have one of your library loading jQuery twice. Happened to me before, I think it was ActiveAdmin at the time that had a different jQuery dependency and thus jQuery loaded twice and doubled all calls.
Your user error is because the user has already been deleted so the second time, it can’t find it.