Rails 3 link_to (:method => :delete) 不起作用
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这里我没有使用 javascript_include_tag :all, :application 等。
我正在使用自己的 js 文件,并使用另一个自定义名称: javascript_include_tag :my_custom_file 我也遇到了这个问题。我没有在文件中使用“= require jquery”,但 javascript/jquery 正在工作。我认为 Rails 默认包含 jquery。我所做的是将 更改
为
现在工作正常。
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
to
Now its working fine.
检查 application.html.erb 是否
不是:
check if application.html.erb
not as:
看起来你的 link_to 方法不太正确。尝试使用以下代码:
Looks like your link_to method isn't quite right. Try using this code instead:
Rails 3 现在使用不显眼的 javascript。在 Rails 2.3 中,erb 只是在 onClick 事件中将所有混乱的 javascript 直接推入链接本身。现在 JavaScript 已从链接中移出,并移至外部 js 文件中。确保你的布局中有这个:
如果你有这个,可能会有更深层次的问题阻止你的 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:
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.
您好,您也可以尝试这个:
application.html.erb:
Hi you can also try this:
application.html.erb:
检查
application.html.erb
中的<%= javascript_include_tag %>
也许您缺少“application”js
它必须至少看起来像
Check your
<%= javascript_include_tag %>
inapplication.html.erb
Perhaps you are missing the "application" js
It must look at least like