开发者

Rails 3 :method=> :delete doesn't work in Internet Explorer

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" r开发者_如何学运维el="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


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" %>


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

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


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.


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.


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...


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.


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 %>


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.


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


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().


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 %>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜