3

I believe that I have followed the tutorial completely, but when I click the "Trash It" link, the page reloads to the same @doc page without any change.

Have a look at the code and let me know what needs to change.

DELETE is a listed verb under the rake routes for docs#destroy.

Thanks so much for the help!

show.html.haml:

%h1= @doc.title
%p= @doc.content

= link_to "All Docs", docs_path
= link_to "Fix Docs", edit_doc_path(@doc)
= link_to "Trash It", doc_path(@doc), method: :delete, data: { confirm: "Are you Sure?" }

docs_controller.rb:

class DocsController < ApplicationController
    before_action :find_doc, only: [:show, :edit, :update, :destroy]

    def index
        @docs = Doc.all.order("created_at DESC")
    end

    def show
    end

    def new
        @doc = Doc.new
    end

    def create
        @doc = Doc.new(doc_params)

        if @doc.save
            redirect_to @doc
        else
            render 'new'
        end
    end

    def edit
    end

    def update
        if @doc.update(doc_params)
            redirect_to @doc
        else 
            render 'edit'
        end
    end

    def destroy
        @doc.destroy
        redirect_to docs_path
    end

    private 

        def find_doc
            @doc = Doc.find(params[:id])
        end

        def doc_params
            params.require(:doc).permit(:title, :content)
        end
end



Rails.application.routes.draw do
  get 'welcome/index'

  root 'welcome#index'

  resources :docs
end
Luka Kerr
  • 4,161
  • 7
  • 39
  • 50
David Sawatske
  • 111
  • 1
  • 7

3 Answers3

0

Do you have jquery_ujs (found at https://github.com/rails/jquery-rails) loaded on the page? Javascript is required to change the <a> to a form post with method=delete.

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Joe Van Dyk
  • 6,828
  • 8
  • 57
  • 73
  • I do. I updated to gem 'jquery-rails', '~> 4.2', '>= 4.2.1' in the gem file to make sure. – David Sawatske Oct 25 '16 at 20:19
  • right.. but is it loaded on the page? any javascript errors? You can look at the network request in chrome debugger to figure out if it's doing a GET or a POST when clicking the link. – Joe Van Dyk Oct 25 '16 at 20:20
  • 3:5 GET http://localhost:3000/stylesheets/default.css 3:6 GET http://localhost:3000/javascripts/default.js – David Sawatske Oct 25 '16 at 20:24
  • These are showing in the console tab of the debugger. Is this the information you needed? Thanks for your patience when dealing with someone who is trying to learn – David Sawatske Oct 25 '16 at 20:26
  • You want the network tab, not the console tab. Watch to see what new requests are issued when you click the delete link. – philomory Oct 25 '16 at 21:20
  • default.css 404 stylesheet 4:5 0 B 192 ms default.js 404 script 4:6 0 B 362 ms These show in red – David Sawatske Oct 25 '16 at 21:50
0

Make sure gem 'jquery-rails' is in gemfile and jquery_ujs is included in app/assets/javascripts/application.js file

//= require jquery
//= require jquery_ujs

I believe your problem is similar this question.

Community
  • 1
  • 1
Kelsey Hannan
  • 2,857
  • 2
  • 30
  • 46
0

Thank you everyone for the help! I was able to figure out the source of the problem.

I had changed 'application' to 'default' in the application.html.erb on lines 5 and 6 to fix a Rails ExecJS::ProgramError in Pages#home? error that has is common on Windows machines.

For reference, the fix was to install a prev version of the gem 'coffee-script-source', '1.8.0'. After changing back the application.html.erb file, I am now in business!

Thanks again for all who helped!

David Sawatske
  • 111
  • 1
  • 7