Rails:向本地服务器发出获取请求时出错

时间:2019-06-28 02:30:24

标签: ruby-on-rails ruby ruby-on-rails-5

ruby​​ / rails的新手,无法找到有关此主题的大量资源和/或最新的堆栈溢出线程,因此发布

当我尝试使用rails route时,在命令行中可以看到api / articles路由,但是,当尝试发出get请求时,我收到了错误消息

ActionController :: RoutingError:没有路由与[GET] \“ / api / articles \” 匹配

以下是我的相关文件。

routes.rb

Rails.application.routes.draw do
  namespace :api do
    resources :articles, only: [:index, :show]
  end
end

articles_controller.rb

module Api
    class ArticlesController  < ApplicationController
      def index
        articles  = Article.order('created_at DESC');
        render json: {
          status: 'success', message: 'loaded articles', data: articles
        },
        status: :ok
    end
end

文章模型

class Article < ApplicationRecord
  validates :title, presence :true
  validates :body, presence :true
end

完整的堆栈跟踪 ActionController :: RoutingError(没有路由与[GET]“ / api / articles”匹配):

actionpack(5.2.3)lib / action_dispatch / middleware / debug_exceptions.rb:65:在call' actionpack (5.2.3) lib/action_dispatch/middleware/show_exceptions.rb:33:in通话中 栏(5.2.3)lib / rails / rack / logger.rb:38:in call_app' railties (5.2.3) lib/rails/rack/logger.rb:26:in中的通话' activesupport(5.2.3)lib / active_support / tagged_logging.rb:71:in block in tagged' activesupport (5.2.3) lib/active_support/tagged_logging.rb:28:in tagged' activesupport(5.2.3)lib / active_support / tagged_logging.rb:71:在tagged' railties (5.2.3) lib/rails/rack/logger.rb:26:in通话中 actionpack(5.2.3)lib / action_dispatch / middleware / remote_ip.rb:81:在call' actionpack (5.2.3) lib/action_dispatch/middleware/request_id.rb:27:in中调用 机架(2.0.7)lib / rack / runtime.rb:22:in call' activesupport (5.2.3) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in call' actionpack(5.2.3)lib / action_dispatch / middleware / executor.rb:14:in call' actionpack (5.2.3) lib/action_dispatch/middleware/static.rb:127:in call' 机架(2.0.7)lib / rack / sendfile.rb:111:in call' railties (5.2.3) lib/rails/engine.rb:524:in call' puma(3.12.1)lib / puma / configuration.rb:227:in call' puma (3.12.1) lib/puma/server.rb:660:in handle_request' 美洲豹(3.12.1)lib / puma / server.rb:474:in process_client' puma (3.12.1) lib/puma/server.rb:334:in block in run' 彪马(3.12.1)lib / puma / thread_pool.rb:135:in`spawn_thread中的块'

0 个答案:

没有答案