[Rails3.0] When Scaffold-Generated Delete (Destroy) Links Don't Work, Check if jquery_ujs.js or rails.js is Loaded

Tadashi Shigeoka ·  Wed, November 16, 2011

In Rails3.0, when scaffold-generated delete (Destroy) links don’t work, check if jquery_ujs.js or rails.js is loaded.

※ The delete link (Destroy) is the part shown above

When using jQuery instead of prototype.js, the delete link won’t work as-is.

To make it work, you need to load jquery_ujs.js (rails.js).

<%= link_to 'Destroy', test_model, :confirm => 'Are you sure?', :method => :delete %>

In my case, the cause was that I had deleted jquery_ujs.js without understanding its role.

Incidentally, jquery_ujs.js is created together when configuring Rails3.0 to use jQuery.

jQuery Support Setup for Rails3.0

First, add the following to your Gemfile and bundle:
gem 'jquery-rails''

Execute the generator to delete unnecessary files and generate required files:

$ rails g jquery:install

This generator will delete the following prototype.js-related files:

public/javascripts/prototype.js public/javascripts/effects.js public/javascripts/dragdrop.js public/javascripts/controls.js

And generate the following jQuery-related files (jquery-rails 1.0.2 installs jQuery 1.6):

public/javascripts/jquery.js public/javascripts/jquery.min.js public/javascripts/jquery_ujs.js (sets event handlers to elements generated by various helpers; formerly rails.js)

・Source: Rails 3.1ではjQueryが標準になるそうなので、すぐに要らない知識になるかもしれませんが、Rails 3.0でのjquery-ra… - Sooey

As mentioned in the title of the source article, jQuery becomes standard in Rails 3.1, so not many people will encounter this issue, but I hope this reference is helpful.

【References】

rails/jquery-ujs - GitHub

Rails3にjQueryを導入する - ちょりぽんのふがふが日記

That’s all from the Gemba.