link_to delete url is not working

狂风中的少年 提交于 2019-11-27 04:33:08

问题


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.


回答1:


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.

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

I included jQuery UI in my application, I found that delete is now working as show, but after doing above Resolved Issue.




回答2:


Make sure these lines appear in application.js :

 //= require jquery
 //= require jquery_ujs



回答3:


Ensure that you have java script turned on. Otherwise :method => :delete acts just as show in Rails.




回答4:


If you're using restful routing for blogs, then the following should work:

<%= link_to "Delete", @blog, :method => :delete, :confirm => "Are you sure ?"%>



回答5:


You can try with 'data-method' instead of :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:

// Handles "data-method" on links such as:
// <a href="/users/5" data-method="delete" rel="nofollow" data-confirm="Are you sure?">Delete</a>



回答6:


In order for link_to to work with the delete 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.




回答7:


you should use

<%=button_to "Delete",blog_path(@blog.id), :method => :delete, :class => "delete", :confirm => "Are you sure ?"%>  


来源:https://stackoverflow.com/questions/4423314/link-to-delete-url-is-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!