ruby on rails link_to delete method not working

后端 未结 7 1514
挽巷
挽巷 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: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
    

提交回复
热议问题