5

I'm building a feature in Ruby on Rails that allows one to take notes, edit them, and deletes them. I can create no problem, edit no problem, but when i go to delete i get redirected to that notes page and it doesn't get deleted.

index.html.erb

<p id="notice"><%= notice %></p>

<h1>Session Notes</h1>

<table>
  <thead>
    <tr>
      <th>Note</th>
      <th>Session</th>
      <th>Created at</th>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% @session_notes.each do |session_note| %>
      <tr>
        <td><%= session_note.note %></td>
        <td><%= session_note.session_id %></td>
        <td><%= session_note.created_at %></td>
        <td><%= link_to 'Show', session_note %></td>
        <td><%= link_to 'Edit', edit_session_note_path(session_note) %></td>
        <td><%= link_to 'Destroy', session_note, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>
  </tbody>
</table>

<br>

session_notes_controller.rb

require 'pry'
class SessionNotesController < ApplicationController
  before_action :logged_in_teacher, only: [:create, :destroy]  
  before_action :set_session_note,  only: [:show, :edit, :update, :destroy]


  # GET /session_notes
  # GET /session_notes.json
  def index
    @session_notes = SessionNote.all
  end

  # GET /session_notes/1
  # GET /session_notes/1.json
  def show
  end

  def testfunc
  end

  # GET /session_notes/new
  def new
    @session_note = SessionNote.new
  end

  # GET /session_notes/1/edit
  def edit
  end

  # POST /session_notes
  # POST /session_notes.json
  def create
    @session_note = SessionNote.new(session_note_params)

    respond_to do |format|
      if @session_note.save
        @session_note.new_record?
        format.html { redirect_to @session_note, notice: 'Session note was 
successfully created.' }
        format.json { render :show, status: :created, location: @session_note }
      else
        format.html { render :new }
        format.json { render json: @session_note.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /session_notes/1
  # PATCH/PUT /session_notes/1.json
  def update
    respond_to do |format|
      if @session_note.update(session_note_params)
        format.html { redirect_to @session_note, notice: 'Session note was successfully updated.' }
        format.json { render :show, status: :ok, location: @session_note }
      else
        format.html { render :edit }
        format.json { render json: @session_note.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /session_notes/1
  # DELETE /session_notes/1.json
  def destroy
    @session_note.destroy
    binding.pry
    respond_to do |format|
      format.html { redirect_to session_notes_url, notice: 'Session note was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_session_note
      @session_note = SessionNote.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def session_note_params
       params.require(:session_note).permit(:note, :session_id, :created_at)
    end
end

routes.rb

Rails.application.routes.draw do
  root 'login_session#new'

  get 'teachers/:id/pword' => 'teachers#pword'
  get "teachers/:id/home",  to: 'teachers#home'

  resources :roster_students
  resources :roster_squares
  resources :session_notes
  resources :session_events
  resources :sessions
  resources :squares
  resources :students
  resources :teachers
  resources :schools
  get    '/report1',  to: 'reports#report1'
  # For details on the DSL available within this file, see 
http://guides.rubyonrails.org/routing.html

  get    'login'   => 'login_session#new'
  post   'login'   => 'login_session#create'

  get    'logout'  => 'login_session#logout'

  get    'about1'  => 'static_pages#about1'
  get    'about2'  => 'static_pages#about2'

  get    'home1'   => 'static_pages#home1'
  post   'home1'   => 'static_pages#home1'

  get    '/super_report',    to: 'teachers#super_report'
  get    '/admin',    to: 'teachers#admin'
  get    '/super',    to: 'schools#super'

  get    '/allSchools', to: 'schools#index'
end

Console after clicking link

Started GET "/session_notes/7" for 192.160.165.63 at 2017-05-15 19:55:42 +0000
Cannot render console from 192.160.165.63! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
  ActiveRecord::SchemaMigration Load (0.3ms)  SELECT "schema_migrations".* FROM "schema_migrations"
Processing by SessionNotesController#show as HTML
  Parameters: {"id"=>"7"}
  SessionNote Load (0.2ms)  SELECT  "session_notes".* FROM "session_notes" WHERE "session_notes"."id" = ? ORDER BY "session_notes"."created_at" DESC LIMIT ?  [["id", 7], ["LIMIT", 1]]
  Rendering session_notes/show.html.erb within layouts/application
  Rendered session_notes/show.html.erb within layouts/application (5.7ms)
  Teacher Load (0.2ms)  SELECT  "teachers".* FROM "teachers" WHERE "teachers"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
Completed 200 OK in 367ms (Views: 325.0ms | ActiveRecord: 2.6ms)


Started GET "/fonts/fontawesome-webfont.woff2?v=4.7.0" for 192.160.165.63 at 2017-05-15 19:55:43 +0000
Cannot render console from 192.160.165.63! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255

ActionController::RoutingError (No route matches [GET] "/fonts/fontawesome-webfont.woff2"):

actionpack (5.0.3) lib/action_dispatch/middleware/debug_exceptions.rb:53:in `call'
web-console (3.5.1) lib/web_console/middleware.rb:135:in `call_app'
web-console (3.5.1) lib/web_console/middleware.rb:20:in `block in call'
web-console (3.5.1) lib/web_console/middleware.rb:18:in `catch'
web-console (3.5.1) lib/web_console/middleware.rb:18:in `call'
actionpack (5.0.3) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
railties (5.0.3) lib/rails/rack/logger.rb:36:in `call_app'
railties (5.0.3) lib/rails/rack/logger.rb:24:in `block in call'
activesupport (5.0.3) lib/active_support/tagged_logging.rb:69:in `block in tagged'
activesupport (5.0.3) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (5.0.3) lib/active_support/tagged_logging.rb:69:in `tagged'
railties (5.0.3) lib/rails/rack/logger.rb:24:in `call'
sprockets-rails (3.2.0) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (5.0.3) lib/action_dispatch/middleware/request_id.rb:24:in `call'
rack (2.0.2) lib/rack/method_override.rb:22:in `call'
rack (2.0.2) lib/rack/runtime.rb:22:in `call'
activesupport (5.0.3) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
actionpack (5.0.3) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.0.3) lib/action_dispatch/middleware/static.rb:136:in `call'
rack (2.0.2) lib/rack/sendfile.rb:111:in `call'
railties (5.0.3) lib/rails/engine.rb:522:in `call'
puma (3.8.2) lib/puma/configuration.rb:224:in `call'
puma (3.8.2) lib/puma/server.rb:600:in `handle_request'
puma (3.8.2) lib/puma/server.rb:435:in `process_client'
puma (3.8.2) lib/puma/server.rb:299:in `block in run'
puma (3.8.2) lib/puma/thread_pool.rb:120:in `block in spawn_thread'
  Rendering /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.3/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb within rescues/layout
  Rendering /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.3/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
  Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.3/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.1ms)
  Rendered collection of /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.3/lib/action_dispatch/middleware/templates/routes/_route.html.erb [87 times] (23.6ms)
  Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.3/lib/action_dispatch/middleware/templates/routes/_table.html.erb (9.4ms)
  Rendering /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
  Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.7ms)
  Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.3/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb within rescues/layout (65.3ms)
Started GET "/fonts/fontawesome-webfont.woff?v=4.7.0" for 192.160.165.63 at 2017-05-15 19:55:43 +0000
Cannot render console from 192.160.165.63! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255

ActionController::RoutingError (No route matches [GET] "/fonts/fontawesome-webfont.woff"):

actionpack (5.0.3) lib/action_dispatch/middleware/debug_exceptions.rb:53:in `call'
web-console (3.5.1) lib/web_console/middleware.rb:135:in `call_app'
web-console (3.5.1) lib/web_console/middleware.rb:20:in `block in call'
web-console (3.5.1) lib/web_console/middleware.rb:18:in `catch'
web-console (3.5.1) lib/web_console/middleware.rb:18:in `call'
actionpack (5.0.3) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
railties (5.0.3) lib/rails/rack/logger.rb:36:in `call_app'
railties (5.0.3) lib/rails/rack/logger.rb:24:in `block in call'
activesupport (5.0.3) lib/active_support/tagged_logging.rb:69:in `block in tagged'
activesupport (5.0.3) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (5.0.3) lib/active_support/tagged_logging.rb:69:in `tagged'
railties (5.0.3) lib/rails/rack/logger.rb:24:in `call'
sprockets-rails (3.2.0) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (5.0.3) lib/action_dispatch/middleware/request_id.rb:24:in `call'
rack (2.0.2) lib/rack/method_override.rb:22:in `call'
rack (2.0.2) lib/rack/runtime.rb:22:in `call'
activesupport (5.0.3) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
actionpack (5.0.3) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.0.3) lib/action_dispatch/middleware/static.rb:136:in `call'
rack (2.0.2) lib/rack/sendfile.rb:111:in `call'
railties (5.0.3) lib/rails/engine.rb:522:in `call'
puma (3.8.2) lib/puma/configuration.rb:224:in `call'
puma (3.8.2) lib/puma/server.rb:600:in `handle_request'
puma (3.8.2) lib/puma/server.rb:435:in `process_client'
puma (3.8.2) lib/puma/server.rb:299:in `block in run'
puma (3.8.2) lib/puma/thread_pool.rb:120:in `block in spawn_thread'
  Rendering /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.3/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb within rescues/layout
  Rendering /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.3/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
  Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.3/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.2ms)
  Rendered collection of /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.3/lib/action_dispatch/middleware/templates/routes/_route.html.erb [87 times] (53.7ms)
  Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.3/lib/action_dispatch/middleware/templates/routes/_table.html.erb (1.4ms)
  Rendering /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
  Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.7ms)
  Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.3/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb within rescues/layout (87.9ms)
Started GET "/fonts/fontawesome-webfont.ttf?v=4.7.0" for 192.160.165.63 at 2017-05-15 19:55:43 +0000
Cannot render console from 192.160.165.63! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255

ActionController::RoutingError (No route matches [GET] "/fonts/fontawesome-webfont.ttf"):

actionpack (5.0.3) lib/action_dispatch/middleware/debug_exceptions.rb:53:in `call'
web-console (3.5.1) lib/web_console/middleware.rb:135:in `call_app'
web-console (3.5.1) lib/web_console/middleware.rb:20:in `block in call'
web-console (3.5.1) lib/web_console/middleware.rb:18:in `catch'
web-console (3.5.1) lib/web_console/middleware.rb:18:in `call'
actionpack (5.0.3) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
railties (5.0.3) lib/rails/rack/logger.rb:36:in `call_app'
railties (5.0.3) lib/rails/rack/logger.rb:24:in `block in call'
activesupport (5.0.3) lib/active_support/tagged_logging.rb:69:in `block in tagged'
activesupport (5.0.3) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (5.0.3) lib/active_support/tagged_logging.rb:69:in `tagged'
railties (5.0.3) lib/rails/rack/logger.rb:24:in `call'
sprockets-rails (3.2.0) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (5.0.3) lib/action_dispatch/middleware/request_id.rb:24:in `call'
rack (2.0.2) lib/rack/method_override.rb:22:in `call'
rack (2.0.2) lib/rack/runtime.rb:22:in `call'
activesupport (5.0.3) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
actionpack (5.0.3) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.0.3) lib/action_dispatch/middleware/static.rb:136:in `call'
rack (2.0.2) lib/rack/sendfile.rb:111:in `call'
railties (5.0.3) lib/rails/engine.rb:522:in `call'
puma (3.8.2) lib/puma/configuration.rb:224:in `call'
puma (3.8.2) lib/puma/server.rb:600:in `handle_request'
puma (3.8.2) lib/puma/server.rb:435:in `process_client'
puma (3.8.2) lib/puma/server.rb:299:in `block in run'
puma (3.8.2) lib/puma/thread_pool.rb:120:in `block in spawn_thread'
  Rendering /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.3/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb within rescues/layout
  Rendering /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.3/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
  Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.3/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.5ms)
  Rendered collection of /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.3/lib/action_dispatch/middleware/templates/routes/_route.html.erb [87 times] (28.1ms)
  Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.3/lib/action_dispatch/middleware/templates/routes/_table.html.erb (1.4ms)
  Rendering /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
  Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.4ms)
  Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.3/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb within rescues/layout (63.0ms)

Application.js

// This is a manifest file that'll be compiled into application.js, which will 

    include all the files
    // listed below.
    //
    // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
    // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
    //
    // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
    // compiled file. JavaScript code in this file should be added after the last require_* statement.
    //
    // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
    // about supported directives.
    //
    //= require jquery
    //= require jquery_ujs
    //= require bootstrap
    //= require turbolinks
    //= require_tree .
  • Possible duplicate of [ruby on rails link\_to delete method not working](http://stackoverflow.com/questions/12997167/ruby-on-rails-link-to-delete-method-not-working) – Eyeslandic May 15 '17 at 19:28
  • Are you logged in as a teacher? Your before_action requires you to login before you can delete. – hashrocket May 15 '17 at 19:35
  • @Brian just posted –  May 15 '17 at 19:36
  • @hashrocket I am logged in as a teacher. We have an initial teacher that we can log in as. However I'm not in charge of logging in nor teachers so I'm not sure how correctly it was done. Is there anything I can do/check on my end? –  May 15 '17 at 19:41
  • Add your console after you try to delete a session note and any errors you get after trying to delete. – hashrocket May 15 '17 at 19:53
  • @hashrocket just did –  May 15 '17 at 20:01
  • Are you sure you have `jquery-ujs` in your `application.js` file? – Eyeslandic May 15 '17 at 20:06
  • @Iceman yes. I posted to question to be sure its correct –  May 15 '17 at 20:10
  • Your console is showing what happens after you click "Show", not "Delete". But the error appears to be a failure when trying to load font-awesome. How and where are you trying to use font-awesome? Thst easiest way in my opinion is to use the font-awesome-rails gem at https://github.com/bokmann/font-awesome-rails – Brian May 15 '17 at 21:31
  • Hey @Brian, I'm clicking the delete function but it shows up as show. I'm never click the show link. –  May 15 '17 at 21:40
  • Can you post the relevant section of the generated HTML? (Do a 'View Source' in your browser and copy everything within the `...` tags. Also, how are you using font-awesome? – Brian May 15 '17 at 21:48
  • Routing errors for font-awesome don't matter. This has every symptom of being a missing javascript stuff, have you taken a look at the javascript console in your browser, if there are any errors? – Eyeslandic May 15 '17 at 22:09
  • I think your issue is that in your link_to you are referencing the object and rails interprets this as a link to the `show` action. Could you try changing it to use the session_note_path i.e. `<%= link_to 'Destroy', session_note_path(session_note), method: :delete, data: { confirm: 'Are you sure?' } %>` – mahi-man May 16 '17 at 01:37
  • 1
    @mahi-man That is not the issue, the `link_to` is correct – Eyeslandic May 16 '17 at 06:58
  • Add your gemfile. – hashrocket May 17 '17 at 13:05
  • Does the layout include `javascript_include_tag 'application'`? Otherwise, `jquery_ujs` might be part of `application.js` but if `application.js` isn't loaded, then neither is the jQuery UJS functionality. – Clemens Kofler Aug 12 '20 at 08:37
  • I am facing the same issue and I _think_ it has to do with whether or not scaffold is used, if that helps anyone – thumbtackthief Aug 12 '20 at 13:22
  • @thumbtackthief did you try this solution - https://github.com/FortAwesome/font-awesome-sass/issues/48#issuecomment-55530874 ? Maybe there is something wrong with font-awesome imports in applications.css/scss – nuaky Aug 13 '20 at 07:52
  • Is `binding.pry` working when you click the delete button? If so can you post the result of `@session_note.errors` – Mehmet Adil İstikbal Aug 14 '20 at 13:41

2 Answers2

6

From your logs the request is not being passed as a DELETE but rather as a GET which indicates that either there is a Javascript issue before importing jquery-ujs, or your application.js file is simply not imported at the given page.

Make sure the layout file that renders that action have a <%= javascript_include_tag application.js %> declaration and that there is no errors in the Javascript execution of the page.

ErvalhouS
  • 4,178
  • 1
  • 22
  • 38
-4

index.html.erb

<td><%= link_to 'Destroy', session_note_path(session_note), method: :delete, data: { confirm: 'Are you sure?' } %></td>
Oleg Shevtsov
  • 73
  • 2
  • 12
  • you have it <%= link_to 'Destroy', session_note, method: :delete, data: { confirm: 'Are you sure?' } %> – Oleg Shevtsov Aug 11 '20 at 20:41
  • 1
    Oleg, using `session_note` instead of `session_note_path(session_note)` is totally fine here (because an ActiveModel instance gets passed to `polymorphic_path` which resolves exactly to `session_note_path(session_note)` in this case). – Clemens Kofler Aug 12 '20 at 08:34
  • @OlegShevtsov Welcome to StackExchange! Looking through your profile, I see that you have a lot of answers that are just code snippets. Just some advice: Answers without explanations usually get downvoted, if that's something you care about. I appreciate your effort though! – thumbtackthief Aug 12 '20 at 13:23