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

发布于 2024-10-05 14:03:08 字数 693 浏览 2 评论 0原文

我在 Rails 中遇到动词问题...

正在查看资源 (Dog) 的页面,该资源 (Dog) 包含有_many (Fleas)。嵌入在狗的 show.html.haml 中的是对 render @dog.fleas 的调用,它会自动(?)查找 &使用“fleas/_flea.html.haml”中的模板列出与所述狗相关的每个跳蚤。

这显示正确。哇!现在,在每个跳蚤旁边,我放置了一个“Kill Flea”链接,该链接转到以下网址://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...并了解发生了什么

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

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

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:

= 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.html page. it's as if it's using GET on /dogs/1/fleas/7 instead of DELETE?!?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

烂柯人 2024-10-12 14:03:09

这里我没有使用 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();"

现在工作正常。

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

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.

橘和柠 2024-10-12 14:03:09

检查 application.html.erb 是否

<%= javascript_include_tag :application %>

不是:

<%= javascript_include_tag :default %>

check if application.html.erb

<%= javascript_include_tag :application %>

not as:

<%= javascript_include_tag :default %>
清风疏影 2024-10-12 14:03:09

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

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

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

  <%= link_to 'Kill Flea', [flea.dog, flea], :confirm => 'Sure?', :method => :delete %>
醉酒的小男人 2024-10-12 14:03:08

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

<%= javascript_include_tag :all %>

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

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:

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

檐上三寸雪 2024-10-12 14:03:08

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

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

Hi you can also try this:

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" %>
黑白记忆 2024-10-12 14:03:08

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

也许您缺少“application”js

它必须至少看起来像

<%= javascript_include_tag "application" %>

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

Perhaps you are missing the "application" js

It must look at least like

<%= javascript_include_tag "application" %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文