Ruby Rails在HTTP GET请求中找不到404

时间:2017-12-26 11:26:35

标签: ruby-on-rails ruby http get restful-authentication

我正在尝试使用Ruby Rails。我试图以JSON格式呈现我的数据库。出于某种原因,每当我从邮递员发送GET请求时,我都会收到路由错误。当我输入“rails routes”时,我得到了正确的路线。其图像定义如下。 下面给出了我的route.db的代码和错误的屏幕截图。如果有人能指导我,我将不胜感激。

routes.db的代码

Rails.application.routes.draw do

 namespace 'api' do
  namespace 'v1' do
   resources :questions
  end
 end
end

控制器代码

module Api
 module V1
  class QuestionsController < ApplicationController
   def index
    questions = Question.order('created_at DESC');
    render json: {status: 'SUCCESS', message:' Loaded Questions', data: questions}, status: :ok
   end
  end
 end
end

Image of Error I am getting

1 个答案:

答案 0 :(得分:0)

我使用了你的代码,没有错误发生。我的代码是

配置/ routes.rb中

Rails.application.routes.draw do
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
  namespace 'api' do
    namespace 'v1' do
      resources :questions
    end
  end
end

应用程序/控制器/ API / V1 / questions_controller.rb

module Api
  module V1
    class QuestionsController < ApplicationController
      def index
        #questions = Question.order('created_at DESC');
        render json: {status: 'SUCCESS', message:' Loaded Questions', data: 'data'}, status: :ok
      end
    end
  end
end

我想你可能会错路。

相关问题