4

I am going through the rails 3 tutorial at railstutorial.org. I have just created an extremely simple scaffold Users.

The scaffold-generated destroy link does not work in Internet Explorer. It redirects to the show action instead of deleting the user.

This problem only happens in IE9 and IE8 (the only IE versions I've tested so far) The problem DOES NOT happen in Firefox. Can anyone tell me why this is happening?

The view:

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

Generated HTML:

<a href="/users/1" data-confirm="Are you sure?" data-method="delete" rel="nofollow">Destroy</a>

The controller:

def destroy
    @user = User.find(params[:id])
    @user.destroy

    respond_to do |format|
      format.html { redirect_to(users_url) }
      format.xml  { head :ok }
    end
end
Sharjeel Aziz
  • 8,495
  • 5
  • 38
  • 37
louis11
  • 61
  • 2
  • 6
  • 1
    I have the same problem - but also on Chrome AND firefox. – theschmitzer Sep 22 '10 at 21:13
  • I have this issue too, but only using IE9. IE8 works for me, IE9 gives me the same symptoms as you if I use the short hostname (I'm running in development mode here), but if I use the FQDN of the dev server, the object gets deleted and the development log shows a redirect to the index, but then a request to the get for the now-deleted object. – hmallett Jul 25 '11 at 13:39

11 Answers11

8

In Rails 3.1 with the asset pipeline, all the javascript is in application.js. So, rather than :defaults, you need "application".

<%= javascript_include_tag "application" %>
George Shaw
  • 1,771
  • 1
  • 18
  • 32
2

Make sure you have <%= javascript_include_tag :defaults %>

JS is now unobtrusive in rails 3, so the include is required to make it work.

theschmitzer
  • 12,190
  • 11
  • 41
  • 49
2

Replace public/javascripts/rails.js of your application with this one:

https://github.com/rails/prototype-ujs/raw/master/src/rails.js

This is updated recently (2010/11/13).

The rails.js bundled with Rails 3.0.0/3.0.1 does not work well on Internet Explorer.

Tsutomu
  • 4,848
  • 1
  • 46
  • 68
2

You need to upgrade your prototype distribution to 1.7 instead of 1.7rc2 (which doesn't have full support for IE 9). The latest Rails gem (at time of writing( in the gem repo is bundling 1.7rc2. Go to the prototype homepage, download the new 1.7 release and replace this with the bundled prototype.js.

Matt S
  • 21
  • 1
2

I couldn't get it to work with IE9 with the default javascript libraries, so I installed jquery-rails and it works just fine. Not perhaps the ideal solution, but if it works...

hmallett
  • 1,537
  • 1
  • 10
  • 11
1

This is probably because the loading of your script (rails.js and/or application.js) happens in head. At script execution time, there are no elements in your DOM with the attributes data-confirm and data-method.

So, the solution is to move the javascript tag to the end of <body>. At this time, the DOM has most likely been loaded and the javascript will find your elements.

thomax
  • 9,213
  • 3
  • 49
  • 68
1

By default, new rails projects are created with Prototype javascript libraries, with some prototype-specific helper functions in "public/javascript/rails.js". The scaffolding relies on some of these helpers to handle some things, like destroying a record, since there isn't a good javascript-free way of making a DELETE request, etc.

Make sure that your pages are loading both the javascript libraries and the "rails.js" file, which are needed to make the scaffolding work (see theschmitzer's answer, or check in Firebug).

Second, if you are using jQuery, install the 'jquery-rails' gem and then run "rails g jquery:install". This will remove the Prototype libraries, install the jQuery libraries, and update the helpers to use jQuery.

user421944
  • 89
  • 1
  • 2
1

Try replacing your javascript default include with:

<%= javascript_include_tag "jquery" %>
<%= javascript_include_tag "jquery.min" %>
<%= javascript_include_tag "rails" %>

after following the steps above to get the latest jquery.js, jquery.min.js, and rails.js. That worked for me, anyway.

Or, replace the meaning of :defaults in application.rb:

config.action_view.javascript_expansions[:defaults] = %w(jquery jquery.min rails)

And then your application layout can still look have

    <%= javascript_include_tag :defaults %>
Adam Ehven
  • 119
  • 1
  • 3
  • having same issue on Linux and Rails 3. Have tried all day to get it working and the only thing that worked was separating the tags as above. weird behaviour. delete works again, live search works, sorting works. works in firefox and chrome and safari on linux and mac, works on chrome.firefox on windows havent tried IE. Thanks @Adam Ehven – IanN Jul 30 '11 at 11:41
  • forgot to mention I never edited the config/application.rb and it works. Maybe not the most graceful way or the best DRY, but at this point I dont care – IanN Jul 30 '11 at 11:42
0

I notice that in rails 4, link_to puts the :method as a html attribute "data-method" but :confirm goes to an attribute "confirm" where the javascript needs it to be "data-confirm".

Not sure if this is a bug in rails, but you can work around it by doing this:

<%= link_to 'Destroy', user, :data => {:confirm => 'Are you sure?'}, :method => :delete %>
Matt Connolly
  • 9,757
  • 2
  • 65
  • 61
0

I experience the same problem, regardless of web browser.

theschmitzer's answer didn't help me.

I found that as long as I am using the jquery javascript library the destroy method in the controller is never called.

In my case I had a conflict between the javascript libraries (jQuery and Prototype) which was hard to figure out for such a newbie. I changed completely to jQuery - as in the end of this railcast: http://railscasts.com/episodes/205-unobtrusive-javascript

0

I had the same problem. I was using the <%= javascript_include_tag :defaults %> as well as the jQuery library. When I removed the jQuery library, things worked. Also, you could use noConflict().

Community
  • 1
  • 1
Zabba
  • 64,285
  • 47
  • 179
  • 207