Rails 3 link_to(:method =>:delete)不起作用

在Rails中的动词遇到了麻烦 ……

查看资源(狗)的页面has_many(跳蚤)。 嵌入dog的show.html.haml是对render @dog.fleas的调用,它自动(?)找到并使用“fleas / _flea.html.haml”中的模板来列出与所述狗相关的每个跳蚤。

这显示正确。 噢! 现在,在每个跳蚤的旁边,我放了一个“Kill Flea”链接到url: //localhost:3000/dogs/1/fleas/7 。 由哪个生成:

 = link_to("Kill Flea", [ flea.dog, flea ], :method => :delete, :confirm => "Sure? A bunny will die") 

但每次点击该链接都没有确认…并且它会呈现跳蚤的show.html页面。 好像它在/dogs/1/fleas/7上使用GET而不是DELETE ?!?

ps-不担心蜘蛛和机器人在我的数据库中删除东西……我只是想学习Rails ……并了解发生了什么

Rails 3现在使用不引人注目的JavaScript。 在Rails 2.3中,erb会在onClick事件中将所有混乱的javascript直接推送到链接本身。 现在javascript已经移出链接,并进入外部js文件。 确保你的布局中有这个:

 <%= javascript_include_tag :all %> 

如果你有这个,那么可能会有更深层次的问题阻止你的javascript运行,但这是一个可以开始的地方。 让我知道结果如何。

嗨,你也可以试试这个:

application.html.erb:

 <%= javascript_include_tag 'jquery_ujs' %> OR <%= javascript_include_tag "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js", "jquery.rails.js" %> 

检查application.html.erb <%= javascript_include_tag %>

也许你错过了“应用程序”js

它必须至少看起来像

 <%= javascript_include_tag "application" %> 

在这里,我没有使用javascript_include_tag:all,:application等。我使用自己的js文件,使用另一个自定义名称:javascript_include_tag:my_custom_file,我也有这个问题。 我没有在文件中使用’= require jquery’,但是javascript / jquery正在运行。 我认为Rails默认包含jquery。 我做的是改变了

 link_to "Logout", destroy_user_session_path, :method => :delete 

  form_tag destroy_user_session_path, :method => :delete, :id => "logout_form" link_to 'Logout', "#delete", :onclick => "$('#logout_form').submit();" 

现在它的工作正常。

检查application.html.erb

 <%= javascript_include_tag :application %> 

不是:

 <%= javascript_include_tag :default %> 

看起来你的link_to方法不太正确。 请尝试使用此代码:

  <%= link_to 'Kill Flea', [flea.dog, flea], :confirm => 'Sure?', :method => :delete %>