Rails入门教程删除功能不起作用

时间:2017-04-26 14:54:28

标签: ruby-on-rails

我已经关注the tutorial up to 5.13,除博客的两个方面外,一切正常:

  1. 删除功能导航到我要删除的页面,而不是删除它。
  2. 在我的CMD中,我得到了一堆与布局文件夹中的routing_error有关的渲染错误,但我不确定这是否是一个问题,将在本教程后面解决。
  3. 我的问题涉及删除链接的无功能

    index.html.erb

        <h1>My Notes</h1>
     <%= link_to 'New Note', new_article_path %>
    <table>
      <tr>
        <th>Note Entry Title</th>
        <th>Preview</th>
        <th colspan="3"></th>
      </tr>
    
      <% @articles.each do |article| %>
        <tr>
          <td><%= article.title %></td>
          <td><%= article.text %></td>
          <td><%= link_to 'Show', article_path(article) %></td>
          <td><%= link_to 'Edit', edit_article_path(article) %></td>
          <td><%= link_to 'Destroy', article_path(article),
                  method: :delete,
                  data: { confirm: 'Are you sure?' } %></td>  #<<< This is the delete code
    
        </tr>
      <% end %>
    </table>
    

    articles_controller.rb

    class ArticlesController < ApplicationController
        def index
        @articles = Article.all
        end
    
        def show
        @article = Article.find(params[:id])
        end
    
        def new
        @article = Article.new
        end
    
        def edit
        @article = Article.find(params[:id])
        end
    
        def create
        #render plain: params[:article].inspect
        #@article = Article.new(params.require(:article).permit(:title, :text)) 
            @article = Article.new(article_params)
    
            if @article.save
                redirect_to @article
            else
                render 'new'
            end
        end
    
        def update
        @article = Article.find(params[:id])
            if @article.update(article_params)
                redirect_to @article
            else
                render 'edit'
            end
        end
        def destroy #<<< This is the destroy method
            @article = Article.find(params[:id])
            @article.destroy
    
            redirect_to articles_path
        end
        private
            def article_params
                params.require(:article).permit(:title, :text)
        end
    end
    

    我想我根本不确定如何解决这个问题,因为我已经逐字逐句地阅读了教程,并花了半个星期的时间试图解决这个问题,只是被其他帖子引导到了兔子洞。要么不属于这个问题,要么没有充分的修复。也许我错过了一个错字,但我已经多次梳理过代码了(注意我是一个Ruby初学者,所以这个东西对我来说很新)。

    下面是我每次点击页面上的任何链接时出现的错误消息的图像(显示和编辑/保存按钮与创建新页面一起工作,但删除只是导航页面。

    https://i.stack.imgur.com/EeeCB.png

    更新4/26/2017 - 下午1:00

    routes.rb文件代码:

    Rails.application.routes.draw do
      #get 'welcome/index' <- commented this out from earlier step in tutorial
    
      resources :articles 
      root 'welcome#index'
      # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
    end
    

    CMD错误代码 - 这是上面提供的屏幕截图的补充

    C:\Users\Nick\blog>rails server
    => Booting Puma
    => Rails 5.0.2 application starting in development on http://localhost:3000
    => Run `rails server -h` for more startup options
    *** SIGUSR2 not implemented, signal based restart unavailable!
    *** SIGUSR1 not implemented, signal based restart unavailable!
    *** SIGHUP not implemented, signal based logs reopening unavailable!
    Puma starting in single mode...
    * Version 3.8.2 (ruby 2.3.3-p222), codename: Sassy Salamander
    * Min threads: 5, max threads: 5
    * Environment: development
    * Listening on tcp://localhost:3000
    Use Ctrl-C to stop
    Started GET "/articles" for ::1 at 2017-04-26 10:47:37 -0400
      ActiveRecord::SchemaMigration Load (0.0ms)  SELECT "schema_migrations".* FROM "schema_migrations"
    Processing by ArticlesController#index as HTML
      Rendering articles/index.html.erb within layouts/application
      Article Load (0.0ms)  SELECT "articles".* FROM "articles"
      Rendered articles/index.html.erb within layouts/application (16.0ms)
    Completed 200 OK in 275ms (Views: 241.1ms | ActiveRecord: 1.0ms)
    
    
    Started GET "/stylesheets/default.css" for ::1 at 2017-04-26 10:47:50 -0400
    
    ActionController::RoutingError (No route matches [GET] "/stylesheets/default.css"):
    Started GET "/javascripts/default.js" for ::1 at 2017-04-26 10:47:50 -0400
    
    actionpack (5.0.2) lib/action_dispatch/middleware/debug_exceptions.rb:53:in `call'
    web-console (3.5.0) lib/web_console/middleware.rb:135:in `call_app'
    web-console (3.5.0) lib/web_console/middleware.rb:28:in `block in call'
    web-console (3.5.0) lib/web_console/middleware.rb:18:in `catch'
    web-console (3.5.0) lib/web_console/middleware.rb:18:in `call'
    actionpack (5.0.2) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
    railties (5.0.2) lib/rails/rack/logger.rb:36:in `call_app'
    railties (5.0.2) lib/rails/rack/logger.rb:24:in `block in call'
    activesupport (5.0.2) lib/active_support/tagged_logging.rb:69:in `block in tagged'
    activesupport (5.0.2) lib/active_support/tagged_logging.rb:26:in `tagged'
    activesupport (5.0.2) lib/active_support/tagged_logging.rb:69:in `tagged'
    railties (5.0.2) 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.2) lib/action_dispatch/middleware/request_id.rb:24:in `call'
    rack (2.0.1) lib/rack/method_override.rb:22:in `call'
    rack (2.0.1) lib/rack/runtime.rb:22:in `call'
    activesupport (5.0.2) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
    actionpack (5.0.2) lib/action_dispatch/middleware/executor.rb:12:in `call'
    actionpack (5.0.2) lib/action_dispatch/middleware/static.rb:136:in `call'
    rack (2.0.1) lib/rack/sendfile.rb:111:in `call'
    railties (5.0.2) 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'
    
    ActionController::RoutingError (No route matches [GET] "/javascripts/default.js"):
    
    actionpack (5.0.2) lib/action_dispatch/middleware/debug_exceptions.rb:53:in `call'
    web-console (3.5.0) lib/web_console/middleware.rb:135:in `call_app'
    web-console (3.5.0) lib/web_console/middleware.rb:28:in `block in call'
    web-console (3.5.0) lib/web_console/middleware.rb:18:in `catch'
    web-console (3.5.0) lib/web_console/middleware.rb:18:in `call'
    actionpack (5.0.2) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
    railties (5.0.2) lib/rails/rack/logger.rb:36:in `call_app'
    railties (5.0.2) lib/rails/rack/logger.rb:24:in `block in call'
    activesupport (5.0.2) lib/active_support/tagged_logging.rb:69:in `block in tagged'
    activesupport (5.0.2) lib/active_support/tagged_logging.rb:26:in `tagged'
    activesupport (5.0.2) lib/active_support/tagged_logging.rb:69:in `tagged'
    railties (5.0.2) 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.2) lib/action_dispatch/middleware/request_id.rb:24:in `call'
    rack (2.0.1) lib/rack/method_override.rb:22:in `call'
    rack (2.0.1) lib/rack/runtime.rb:22:in `call'
    activesupport (5.0.2) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
    actionpack (5.0.2) lib/action_dispatch/middleware/executor.rb:12:in `call'
    actionpack (5.0.2) lib/action_dispatch/middleware/static.rb:136:in `call'
    rack (2.0.1) lib/rack/sendfile.rb:111:in `call'
    railties (5.0.2) 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 C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb within rescues/layout
      Rendering C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
      Rendered C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.0ms)
      Rendered collection of C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/routes/_route.html.erb [9 times] (3.0ms)
      Rendered C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/routes/_table.html.erb (14.5ms)
      Rendering C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
      Rendered C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.5ms)
      Rendered C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb within rescues/layout (1355.1ms)
      Rendering C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb within rescues/layout
      Rendering C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
      Rendered C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.5ms)
      Rendered collection of C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/routes/_route.html.erb [9 times] (3.0ms)
      Rendered C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/routes/_table.html.erb (1.5ms)
      Rendering C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
      Rendered C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.5ms)
      Rendered C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb within rescues/layout (2732.5ms)
    

    点击&#39;销毁&#39;后的CMD错误日志在索引页面

    Processing by ArticlesController#show as HTML
      Parameters: {"id"=>"4"}
      Article Load (0.0ms)  SELECT  "articles".* FROM "articles" WHERE "articles"."id" = ? LIMIT ?  [["id", 4], ["LIMIT", 1]]
      Rendering articles/show.html.erb within layouts/application
      Rendered articles/show.html.erb within layouts/application (1.0ms)
    Completed 200 OK in 81ms (Views: 57.1ms | ActiveRecord: 0.0ms)
    
    
    Started GET "/stylesheets/default.css" for ::1 at 2017-04-26 13:12:34 -0400
    
    ActionController::RoutingError (No route matches [GET] "/stylesheets/default.css"):
    Started GET "/javascripts/default.js" for ::1 at 2017-04-26 13:12:34 -0400
    
    actionpack (5.0.2) lib/action_dispatch/middleware/debug_exceptions.rb:53:in `call'
    web-console (3.5.0) lib/web_console/middleware.rb:135:in `call_app'
    web-console (3.5.0) lib/web_console/middleware.rb:28:in `block in call'
    web-console (3.5.0) lib/web_console/middleware.rb:18:in `catch'
    web-console (3.5.0) lib/web_console/middleware.rb:18:in `call'
    actionpack (5.0.2) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
    railties (5.0.2) lib/rails/rack/logger.rb:36:in `call_app'
    railties (5.0.2) lib/rails/rack/logger.rb:24:in `block in call'
    activesupport (5.0.2) lib/active_support/tagged_logging.rb:69:in `block in tagged'
    activesupport (5.0.2) lib/active_support/tagged_logging.rb:26:in `tagged'
    activesupport (5.0.2) lib/active_support/tagged_logging.rb:69:in `tagged'
    railties (5.0.2) 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.2) lib/action_dispatch/middleware/request_id.rb:24:in `call'
    rack (2.0.1) lib/rack/method_override.rb:22:in `call'
    rack (2.0.1) lib/rack/runtime.rb:22:in `call'
    activesupport (5.0.2) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
    actionpack (5.0.2) lib/action_dispatch/middleware/executor.rb:12:in `call'
    actionpack (5.0.2) lib/action_dispatch/middleware/static.rb:136:in `call'
    rack (2.0.1) lib/rack/sendfile.rb:111:in `call'
    railties (5.0.2) 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'
    
    ActionController::RoutingError (No route matches [GET] "/javascripts/default.js"):
    
    actionpack (5.0.2) lib/action_dispatch/middleware/debug_exceptions.rb:53:in `call'
    web-console (3.5.0) lib/web_console/middleware.rb:135:in `call_app'
    web-console (3.5.0) lib/web_console/middleware.rb:28:in `block in call'
    web-console (3.5.0) lib/web_console/middleware.rb:18:in `catch'
    web-console (3.5.0) lib/web_console/middleware.rb:18:in `call'
    actionpack (5.0.2) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
    railties (5.0.2) lib/rails/rack/logger.rb:36:in `call_app'
    railties (5.0.2) lib/rails/rack/logger.rb:24:in `block in call'
    activesupport (5.0.2) lib/active_support/tagged_logging.rb:69:in `block in tagged'
    activesupport (5.0.2) lib/active_support/tagged_logging.rb:26:in `tagged'
    activesupport (5.0.2) lib/active_support/tagged_logging.rb:69:in `tagged'
    railties (5.0.2) 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.2) lib/action_dispatch/middleware/request_id.rb:24:in `call'
    rack (2.0.1) lib/rack/method_override.rb:22:in `call'
    rack (2.0.1) lib/rack/runtime.rb:22:in `call'
    activesupport (5.0.2) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
    actionpack (5.0.2) lib/action_dispatch/middleware/executor.rb:12:in `call'
    actionpack (5.0.2) lib/action_dispatch/middleware/static.rb:136:in `call'
    rack (2.0.1) lib/rack/sendfile.rb:111:in `call'
    railties (5.0.2) 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 C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb within rescues/layout
      Rendering C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb within rescues/layout
      Rendering C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
      Rendering C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
      Rendered C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.5ms)
      Rendered C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.0ms)
             Rendered collection of C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/routes/_route.html.erb [9 times] (3.5ms)
      Rendered collection of C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/routes/_route.html.erb [9 times] (3.0ms)
      Rendered C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/routes/_table.html.erb (1.5ms)
      Rendered C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/routes/_table.html.erb (1.5ms)
      Rendering C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
      Rendering C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
      Rendered C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.5ms)
      Rendered C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.0ms)
      Rendered C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb within rescues/layout (2775.8ms)
      Rendered C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/routing_error.html.erb within rescues/layout (3170.5ms)
    

    rails routes命令结果:

    C:\Users\Nick\blog>rails routes
          Prefix Verb   URI Pattern                  Controller#Action
        articles GET    /articles(.:format)          articles#index
                 POST   /articles(.:format)          articles#create
     new_article GET    /articles/new(.:format)      articles#new
    edit_article GET    /articles/:id/edit(.:format) articles#edit
         article GET    /articles/:id(.:format)      articles#show
                 PATCH  /articles/:id(.:format)      articles#update
                 PUT    /articles/:id(.:format)      articles#update
                 DELETE /articles/:id(.:format)      articles#destroy
            root GET    /                            welcome#index
    

    application.js代码

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

    layouts / application.html.erb文件代码:

    <!DOCTYPE html>
    <html>
      <head>
        <title>Scribble</title>
        <%= csrf_meta_tags %>
    
        <%= stylesheet_link_tag    'default', media: 'all', 'data-turbolinks-track': 'reload' %>
        <%= javascript_include_tag 'default', 'data-turbolinks-track': 'reload' %>
      </head>
    
      <body>
        <%= yield %>
      </body>
    </html>
    

    更新时间1:56 PM 更改application.js文件中的一些代码似乎已经缓解了这个问题(显然这是我所知道的JS加载错误)

    <%= stylesheet_link_tag    'default', media: 'all', 'data-turbolinks-track': 'reload' %>
            <%= javascript_include_tag 'default', 'data-turbolinks-track': 'reload' %>
    

    FROM ^

    <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track': 'reload' %>
            <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
    

    TO ^

    • 如果我遇到与此主题相关的其他问题,我将继续本教程并更新帖子。感谢大家的帮助/支持到目前为止!!!

0 个答案:

没有答案