link_to 删除 url 不起作用
我的应用程序中有以下 link_to 删除网址,
<%=link_to "Delete",blog_path(@blog.id), :method => :delete, :class => "delete", :confirm => "Are you sure ?"%>
它似乎不起作用。当我单击此网址时,它只会将我带到显示路径。有人可以告诉我如何解决此问题吗?谢谢。
I have the following link_to delete url in my app
<%=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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
你在使用 jQuery 吗?如果是这样,我认为问题可能是您使用的 jQuery 没有更新的rails.js 文件。
在这里下载rails.js:
https://github.com/rails/jquery-ujs/raw /master/src/rails.js
将其放入您的 javascripts 目录中,覆盖rails 默认附带的rails.js。
添加 javascript include 行以包含它。
将其放在 Jquery include 标记之后。如果您不打算使用原型,您可能还想排除 javascript 默认值。
我在我的应用程序中包含了 jQuery UI,我发现删除现在可以正常工作,但在完成上述已解决的问题之后。
Are you using jQuery? If so, I think the problem could be that you are using jQuery without the updated rails.js file.
Download rails.js here:
https://github.com/rails/jquery-ujs/raw/master/src/rails.js
Drop it in your javascripts directory, overwriting the rails.js that comes default with rails.
Add a javascript include line to include it.
Put this after your Jquery include tag. You probably also want to disinclude the javascript defaults if you don't plan on using prototype.
I included jQuery UI in my application, I found that delete is now working as show, but after doing above Resolved Issue.
确保这些行出现在
application.js
中:Make sure these lines appear in
application.js
:确保您已打开 Java 脚本。否则
:method => :delete
的作用与 Rails 中所示相同。Ensure that you have java script turned on. Otherwise
:method => :delete
acts just as show in Rails.如果您对博客使用静态路由,则以下内容应该有效:
If you're using restful routing for blogs, then the following should work:
您可以尝试使用“data-method”而不是:method。
您可以在 jquery_ujs.js 上检查以下代码:
You can try with 'data-method' instead of :method.
You can check on jquery_ujs.js the following piece of code:
为了使
link_to
能够使用delete
方法,Rails 需要 用于 jQuery 的不显眼的脚本适配器。确保您的Gemfile具有
gem 'jquery-rails'
Make确保 app/assets/javascripts/application.js 有
//=需要jquery
//= require jquery_ujs
确保您的 app/views/layouts/application.html.erb 有
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' =>;正确%>
<%= javascript_include_tag 'application', 'data-turbolinks-track' =>;正确%>
在
head
标记内。删除'data-turbolinks-track' => true
部分(如果您不打算使用 Turbolinks)。In order for
link_to
to work with thedelete
method, Rails needs the unobtrusive scripting adapter for jQuery.Make sure that your Gemfile has
gem 'jquery-rails'
Make sure that app/assets/javascripts/application.js has
//= require jquery
//= require jquery_ujs
Make sure that your app/views/layouts/application.html.erb has
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
inside the
head
tag. Remove the'data-turbolinks-track' => true
section if you don't plan to use Turbolinks.你应该使用
you should use
没有 jQuery 也可以有一个有效的 link_to
我已经找到了在没有 jQuery 的情况下为 Ruby on Rails 创建有效删除链接的最佳流程!在这里我已经做出了一个答案: https://stackoverflow.com/a/67710994/14387700
但是为了让这变得简单,我又在这里写下了。
我们需要处理 3 件事:
让我们开始...
添加 destroy 方法在articles_controller.rb中
首先我们会在
articles_controller.rb
中添加def destroy ... end
,让我们打开:
在
这是发出破坏命令的主要因素。并使 link_to 正常工作。
设置routes.rb
但是等等
我们需要 2 个销毁页面的路由,它们是:
在路由中只需添加:
这里
最后 2 行声明 DELETE 方法,
link_to 标记中的“articles_delete_path”或“articles_delete_url”。
然后
最后最甜蜜的删除输出
所需的 link_to 标记
我们可以使用它来获取正确的删除 link_to 标记,该标记将起作用。
除了 jQuery 之外,一切都已完成
感谢您正确阅读此答案。!
快乐编程
It's possible to have a working link_to without jQuery
I've found the best process to make a working delete link for ruby on rails without jQuery! Here I already make an answer: https://stackoverflow.com/a/67710994/14387700
But for making this easy, I'm writing this here again.
We need to work with 3 things:
Let's Start...
Adding destroy method in articles_controller.rb
At first, we will add
def destroy ... end
inarticles_controller.rb
,lets open:
Here
This is the main factor which will give the destroying command. and make the link_to working.
Setup routes.rb
BUT WAIT
We need 2 routes for the destroy page which are:
In routes just add:
Here
The 2nd last line is declaring the DELETE method,
'articles_delete_path' or 'articles_delete_url' in link_to tag.
Then
Last sweetest delete output
The desired link_to tag
we can use this to get proper delete link_to tag which will work.
AND that's done except jQuery
Thanks for reading this answer properly.!
HAPPY PROGRAMMINGS