0

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!

Anderson Pimentel
  • 5,086
  • 2
  • 32
  • 54
Graeme
  • 481
  • 6
  • 20
  • 1
    you can check http://stackoverflow.com/questions/4475449/link-to-confirm-displays-popup-twice and http://stackoverflow.com/questions/4231885/rails-3-ujs-controller-gets-called-twice-by-link-to-remote/7778048#7778048 – siekfried Feb 05 '13 at 19:48
  • Thank you! Changing `config.assets.debug` from `true` to `false` in `\config\environments\development.rb` did the trick. – Graeme Feb 06 '13 at 17:20

1 Answers1

1

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.

mathieugagne
  • 2,465
  • 1
  • 17
  • 18