1
votes

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 :)

1
I don't know where you found __id__, but it contains the id of a Ruby object, not a database row. Database row is in simple id and should not change. As for the problem -- jquery-ujs seems to be missing in your app's JavaScript. - D-side
Please add the code of the delete method of the controller, and logs. - Baldrick
I have included the delete method of the controller, please check.. - Avinesh
@D-side what can be done to fix the jquery-ujs related problems ?? - Avinesh
I think your default.js file doesn't have jquery_ujs <%= javascript_include_tag 'default', 'data-turbolinks-track' => true %> Include //= require jquery //= require jquery_ujs in default.js - Pardeep Dhingra

1 Answers

0
votes

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.