Ruby on Rails:Link_to方法:: delete不起作用

时间:2017-05-15 19:13:55

标签: ruby-on-rails link-to

我正在Ruby on Rails中构建一个功能,允许用户记录,编辑和删除它们。我可以创建没有问题,编辑没有问题,但当我去删除我被重定向到该笔记页面,它不会被删除。

index.html.erb

SET NODE_ENV=production

session_notes_controller.rb

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

的routes.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

点击链接后的控制台

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

的application.js

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)

2 个答案:

答案 0 :(得分:4)

不是从DELETE而是从GET传递请求,这表明在导入jquery-ujs之前存在Javascript问题,或者您的{{1 }}文件根本不会在给定页面上导入。

确保用于呈现该操作的布局文件具有application.js声明,并且页面的Javascript执行中没有错误。

答案 1 :(得分:-3)

index.html.erb

<td><%= link_to 'Destroy', session_note_path(session_note), method: :delete, data: { confirm: 'Are you sure?' } %></td>