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

你说的曾经没有我的故事 提交于 2019-11-27 01:43:26

问题


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

回答1:


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



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




回答3:


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.




回答4:


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.




回答5:


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




回答6:


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.




回答7:


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



回答8:


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.




回答9:


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




回答10:


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




回答11:


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


来源:https://stackoverflow.com/questions/3772450/rails-3-method-delete-doesnt-work-in-internet-explorer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!