ruby on rails link_to delete method not working

后端 未结 7 1515
挽巷
挽巷 2021-02-14 06:04

I am trying to delete a post using the code below:

<%= link_to \'Destroy\', post, :method => :delete, :onclick => \"return confirm(\'Are you sure you wa         


        
相关标签:
7条回答
  • 2021-02-14 06:31

    Have you included this line in your layout file?

    <%= javascript_include_tag "application" %>
    
    0 讨论(0)
  • 2021-02-14 06:31

    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.

    0 讨论(0)
  • 2021-02-14 06:33

    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
    
    0 讨论(0)
  • 2021-02-14 06:34

    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
    
    0 讨论(0)
  • 2021-02-14 06:35

    If using link_to with :delete method you must be sure to have javascript active:

    Note that if the user has JavaScript disabled, the request will fall back to using GET.

    As seen in docs

    If you don't want to be dependent on javascript you can use button_to instead of link_to.

    0 讨论(0)
  • 2021-02-14 06:50

    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

    0 讨论(0)
提交回复
热议问题