Ruby-on-rails link_to 删除 url 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4423314/
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
link_to delete url is not working
提问by felix
I have the following link_to delete url in my app
我的应用程序中有以下 link_to delete url
<%=link_to "Delete",blog_path(@blog.id), :method => :delete, :class => "delete", :confirm => "Are you sure ?"%>
It does not seem to be working.When I click this url, it just takes me to the show path.Can someone please tell me how to fix this. Thanks.
它似乎不起作用。当我单击此 url 时,它只会将我带到显示路径。有人可以告诉我如何解决这个问题。谢谢。
回答by Sean Ahrens
Are you using jQuery? If so, I think the problem could be that you are using jQuery without the updated rails.js file.
你在使用 jQuery 吗?如果是这样,我认为问题可能是您在使用 jQuery 时没有更新 rails.js 文件。
Download rails.js here: https://github.com/rails/jquery-ujs/raw/master/src/rails.jsDrop it in your javascripts directory, overwriting the rails.js that comes default with rails.
在此处下载 rails.js:https: //github.com/rails/jquery-ujs/raw/master/src/rails.js 将其放入您的 javascripts 目录中,覆盖 Rails 默认附带的 rails.js。
Add a javascript include line to include it.
添加一个 javascript 包含行以包含它。
<%= javascript_include_tag "rails" %>
Put this after your Jquery include tag. You probably also want to disinclude the javascript defaults if you don't plan on using prototype.
把它放在你的 Jquery 包含标签之后。如果您不打算使用原型,您可能还想取消 javascript 默认值。
I included jQuery UI in my application, I found that delete is now working as show, but after doing above Resolved Issue.
我在我的应用程序中包含了 jQuery UI,我发现删除现在可以正常工作,但是在完成上述已解决的问题之后。
回答by Matoeil
Make sure these lines appear in application.js:
确保这些行出现在application.js:
//= require jquery
//= require jquery_ujs
回答by klew
Ensure that you have java script turned on. Otherwise :method => :deleteacts just as show in Rails.
确保您已打开 Java 脚本。否则:method => :delete就像在 Rails 中显示的一样。
回答by clemensp
If you're using restful routing for blogs, then the following should work:
如果您正在为博客使用静态路由,那么以下应该有效:
<%= link_to "Delete", @blog, :method => :delete, :confirm => "Are you sure ?"%>
回答by rorra
You can try with 'data-method' instead of :method.
您可以尝试使用 'data-method' 而不是 :method。
<%=link_to "Delete",blog_path(@blog.id), 'data-method' => :delete, :class => "delete", :confirm => "Are you sure ?"%>
You can check on jquery_ujs.js the following piece of code:
您可以在 jquery_ujs.js 上查看以下代码:
// Handles "data-method" on links such as:
// <a href="/users/5" data-method="delete" rel="nofollow" data-confirm="Are you sure?">Delete</a>
回答by Mihai-Andrei Dinculescu
In order for link_toto work with the deletemethod, Rails needs the unobtrusive scripting adapter for jQuery.
为了link_to使用该delete方法,Rails 需要不显眼的 jQuery 脚本适配器。
Make sure that your Gemfilehas
gem 'jquery-rails'Make sure that app/assets/javascripts/application.jshas
//= require jquery//= require jquery_ujsMake sure that your app/views/layouts/application.html.erbhas
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %><%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>inside the
headtag. Remove the'data-turbolinks-track' => truesection if you don't plan to use Turbolinks.
确保你的Gemfile有
gem 'jquery-rails'确保 app/assets/javascripts/ application.js有
//= require jquery//= require jquery_ujs确保你的 app/views/layouts/ application.html.erb有
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %><%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>内部
head标签。'data-turbolinks-track' => true如果您不打算使用 Turbolinks,请删除该部分。
回答by thiagoh
you should use
你应该使用
<%=button_to "Delete",blog_path(@blog.id), :method => :delete, :class => "delete", :confirm => "Are you sure ?"%>

