我正在嘗試使用Grape創建一個API。而爲了使這個API的工作是指這個網站,One Grape API。我甚至在stackoverflow上看到這些posts,但它不工作。 代碼如下: /app/api/api.rb預期../app/api/api.rb在rails中定義Api
class API < Grape::API
prefix 'api'
version 'v1', using: :path
format :json
mount Code::Coding
end
/app/api/code/coding.rb:
module Code
class Coding < Grape::API
resource :exam_questions do
desc "getting all the questions"
get do
ExamQuestion.first
end
end
end
end
/應用/配置/應用程序。 RB:
config.paths.add 'app/api', glob: '**/*.rb'
config.autoload_paths += Dir["#{Rails.root}/app"]
/app/config/routes.rb:
mount Coders::API => '/'
但我不斷收到此錯誤
預計../app/api/api.rb定義阿比
好了,所以現在我把一個文件夾中的API文件命名爲編碼器和沒有錯誤。 所以文件的結構是這樣的:
應用程序/ API /編碼器/ api.rb
module Coders
class API < Grape:: API
#code goes here
end
end
和APP/API /編碼器/編碼/ coding.rb
module Coders
module Code
class Coding < Grape::API
resource :exam_questions do
desc "getting all the questions"
get do
ExamQuestion.first
end
end
end
end
end
現在我已經嘗試了幾乎所有的排列組合和訪問方式,但我無法獲得可以訪問問題的路徑,但它始終顯示404錯誤。
運行rake途徑得到: coders_api_api /打碼機:: API
有沒有辦法找出訪問我的方法的鏈接。
我已經得到了問題的答案,嘗試了不同的鏈接後,最終它與這個鏈接:** http:// locahost:3000/api/v1/exam_questions **沒有_.json_,那是我犯的錯誤。 – sahil 2015-03-20 11:44:44