Avinesh
Avinesh

Reputation: 624

Deletion in RAILS using method: :delete

I am new to rails and following the tutorial https://mackenziechild.me/12-in-12/12/. I have problems in deleting posts in the app [The rails app allows to create, edit, update, destroy posts(which have a title, a link and a description)]. When destroy button is clicked, instead of redirecting to home page, the DELETION Confirmation message is not shown and I think it keeps reloading the page. Also I noticed that the id of the post changes every time when I hit the destroy button. When I checked the home page (that lists all posts), the count remains the same and nothing is deleted.
FYI, I have attached my code snippet of show.html.haml

%h1 Inspirations (SHOW)

%h1= @post.title
%p= @post.link
%p= @post.description

%p= @post.__id__

= link_to "Edit", edit_post_path(@post)
= link_to "Destroy", post_path(@post), method: :delete, data: { confirm: "Are you sure?"}
= link_to "Home", root_path

And my ../app/views/layouts/application.html.erb file is as follows

<!DOCTYPE html>
<html>
<head>
  <title>Muse</title>
  <%= stylesheet_link_tag    'default', media: 'all', 'data-turbolinks-track' => true %>
  <%= javascript_include_tag 'default', 'data-turbolinks-track' => true %>
  <%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>

Also my ../app/assets/javascripts/application.js is as follows

//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .

Delete Method code in Controller

class PostsController < ApplicationController

  before_action :find_post, only: [:show, :edit, :update, :destroy]

  ....
                                             
  def destroy
    @post.destroy
    redirect_to root_path
  end

  private

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

  def post_params
    params.require(:post).permit(:title, :link, :description)
  end
                                             

Some of my observations


My Home Page before deletion enter image description here


Deleting Dummy Post
enter image description here


Dummy Post's ID changed upon hitting Destroy button
enter image description here


But when checking the Home page (After deletion), still all posts are listed

Please help me where I went wrong, thanks in advance :)

Upvotes: 1

Views: 1462

Answers (1)

Frederick Cheung
Frederick Cheung

Reputation: 84152

You're attempting to load default.js, which doesn't exist. Your call to javascript_link_tag should in fact be

 javascript_link_tag "application", "data-turbolinks-track" => true 

Your call to stylesheet_link_tag is similarly wrong although this is not related to your problem.

Upvotes: 0

Related Questions