Ruby-on-rails Rails 3 link_to (:method => :delete) 不工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4342083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:47:20  来源:igfitidea点击:

Rails 3 link_to (:method => :delete) not working

ruby-on-railsroutingrails-routing

提问by Meltemi

I'm having trouble with my verbs in Rails...

在 Rails 中的动词问题...

viewing a page for a resource (Dog) which has_many (Fleas). Embedded in dog's show.html.hamlis a call to render @dog.fleaswhich automatically(?) finds & uses the template in "fleas/_flea.html.haml" to list each flea associated with said dog.

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

this displays correctly. whew! Now, next to each flea I've put a "Kill Flea" link that goes to a url: //localhost:3000/dogs/1/fleas/7. Which is generated by:

这显示正确。哇!现在,在每个跳蚤旁边我放了一个“杀死跳蚤”链接,该链接指向一个 url: //localhost:3000/dogs/1/fleas/7。由以下生成:

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

but every time that link is clicked there is no confirmation... and it renders the flea's show.htmlpage. it's as if it's using GETon /dogs/1/fleas/7instead of DELETE?!?

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

ps- not worried about spiders & robots deleting things in my database... i'm just trying to learn Rails..and understand what's happening

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

回答by Jaime Bellmyer

Rails 3 uses unobtrusive javascript now. In Rails 2.3, erb would just shove all that messy javascript right into the link itself, in an onClick event. Now the javascript has been moved out of the link, and into external js files. Make sure you have this in your layout:

Rails 3 现在使用不显眼的 javascript。在 Rails 2.3 中,erb 只会在 onClick 事件中将所有杂乱的 javascript 直接推入链接本身。现在 javascript 已从链接中移出,并移入外部 js 文件中。确保您的布局中有这个:

<%= javascript_include_tag :all %>

If you do have this, there might be deeper problems keeping your javascript from running, but this is the place to start. Let me know how it turns out.

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

回答by AllenC

Hi you can also try this:

您好,您也可以试试这个:

application.html.erb:

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

回答by Victor Augusto

Check your <%= javascript_include_tag %>in application.html.erb

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

Perhaps you are missing the "application" js

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

It must look at least like

它必须至少看起来像

<%= javascript_include_tag "application" %>

回答by Abhi

Here I am not using the javascript_include_tag :all, :application etc. I am using my own js files, with another custom name: javascript_include_tag :my_custom_file and I also had this issue. And I am not using '= require jquery' in the file, but javascript/jquery is working. I think Rails includes jquery by default. What I done is I changed the

在这里,我没有使用 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 

to

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

Now its working fine.

现在它工作正常。

回答by Mohamed Yakout

check if application.html.erb

检查是否 application.html.erb

<%= javascript_include_tag :application %>

not as:

不是:

<%= javascript_include_tag :default %>

回答by Sam Ritchie

Looks like your link_to method isn't quite right. Try using this code instead:

看起来您的 link_to 方法不太正确。尝试改用此代码:

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