ruby on rails link_to delete method not working

£可爱£侵袭症+ 提交于 2019-12-03 12:41:35

You must be add

//= require jquery
//= require jquery_ujs

in javascripts/application.js file

second, check in layout file,

javascript_include_tag "application"

is include or not?

Hope this help.

Solution:

I had commented out this from application.js when i created the project

// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//
//= require jquery
//= require jquery_ujs
//= require_tree .

so re-adding it in solved the problem

Have you included this line in your layout file?

<%= javascript_include_tag "application" %>

This is the Rails way:

<%= link_to 'Destroy', post, confirm: 'Are you sure?', method: :delete %>

If the post isn't deleted then the problem lies in your controller. Are you sure you've correctly implemented the #destroy action? What's the output you're getting in your server logs?

UPDATE: Change your action to:

def destroy
  @post = Post.find(params[:id])

  respond_to do |format|
    if @post.destroy
      format.html { redirect_to posts_url }
      format.json { head :no_content }
    else
      format.html # do something here
      format.json { head :no_content }
    end
  end
end
user2711795

Make sure that there is no space after the param

def destroy
        @post = Post.find(params[:id])
        @post.destroy
        redirect_to posts_path, :notice => "Your post has been deleted successfully."
end

Check the whitespaces.

I checked all the comments here. All the includes were set correctly. But I noticed that deleting articles did not work, while deleting comments would work. After rewriting some code and checking here and there, I found two files that looked identically in the editor, but one version would work and one would not work!

curry-blog/app/views/articles/index.html.erb:

<h1>Listing Articles</h1>
<%= link_to 'New article', new_article_path %>
<table>
  <tr>
    <th>Title</th>
    <th>Text</th>
    <th colspan="3"></th>
  </tr>
 
  <% @articles.each do |article| %>
    <tr>
      <td><%= article.title %></td>
      <td><%= article.text %></td>
      <td><%= link_to 'Show', article_path(article) %></td>
      <td><%= link_to 'Edit', edit_article_path(article) %></td>
      <td><%= link_to 'Delete', article_path(article),
              method: :delete,
              data: { confirm: 'Are you sure?' } %></td>        
    </tr>
  <% end %>
</table>

However, looking at the files with xxdiff I found that in one version only tabs where used, while the other also used blanks. Probably, from copy pasting code from the tutorial. Replacing the blanks with tabs did therefore fix the problem.

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