1

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

Avinesh
  • 584
  • 3
  • 13
  • 31
  • 2
    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 May 25 '15 at 15:11
  • Please add the code of the `delete` method of the controller, and logs. – Baldrick May 25 '15 at 15:19
  • I have included the `delete` method of the controller, please check.. – Avinesh May 25 '15 at 15:27
  • @D-side what can be done to fix the `jquery-ujs` related problems ?? – Avinesh May 25 '15 at 15:30
  • 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 May 25 '15 at 15:46

1 Answers1

0

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.

Frederick Cheung
  • 83,189
  • 8
  • 152
  • 174
  • 1
    But when I changed from 'default' to 'application' in ../app/views/layouts/application.html.erb, it throws the `ExecJS::ProgramError in Posts#index` error :( Initially I had the ExecJS problem and in this link http://stackoverflow.com/questions/28421547/rails-execjsprogramerror-in-pageshome# I saw the answer to fix the execJs error .. – Avinesh May 25 '15 at 15:46
  • Then there's some other problem with your app that previously you weren't seeing because it wasn't using your application.js at all – Frederick Cheung May 25 '15 at 15:47
  • Any other ways of solving the `ExecJS::ProgramError` @Frederick – Avinesh May 25 '15 at 15:51
  • @SteveJobs sudo apt-get install nodejs – Pardeep Dhingra May 25 '15 at 16:02
  • @SteveJobs not from the information given. feels like a separate question to me (but have a search first- lots of questions on that here) – Frederick Cheung May 25 '15 at 16:05
  • I am using Windows @Pardeep Dhingra and thatz why so much problems :/ – Avinesh May 25 '15 at 16:23
  • 1
    @SteveJobs have you tried this http://blog.teamtreehouse.com/install-node-js-npm-windows – Pardeep Dhingra May 25 '15 at 16:25
  • @Pradeep Thx a lot. All works fine all of a sudden after a couple of restarts :) May be I think, I installed node.js but I didn't restart my system :) – Avinesh May 26 '15 at 07:04