0

I was trying to create an API using Grape. And for making this API work was referring to this website- One Grape API. I even saw these posts on stackoverflow but its not working. The code is as follows: /app/api/api.rb

  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

/app/config/application.rb:

config.paths.add 'app/api', glob: '**/*.rb'
config.autoload_paths += Dir["#{Rails.root}/app"]

/app/config/routes.rb:

mount Coders::API => '/'

But I keep getting this error

Expected ../app/api/api.rb to define Api

Okay so now I moved the api files inside a folder named as Coders and there's no error. So the file structure is like this:

app/api/coders/api.rb

module Coders
 class API < Grape:: API
  #code goes here
 end
end

And app/api/coders/code/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

Now I have tried almost all permutations and combinations to access it but I am unable to get the path where I can access the questions, it keeps showing 404 error.

Running rake routes gives: coders_api_api/ Coders::API

Is there any way to find out the links to access my methods.

Community
  • 1
  • 1
Sahil
  • 3,338
  • 1
  • 21
  • 43
  • I have got the answer to the question, after trying out with different links, finally it worked with this link : **http://locahost:3000/api/v1/exam_questions** without _.json_ and that was the mistake which I was committing. – Sahil Mar 20 '15 at 11:44

1 Answers1

0

I have got the answer to the question, after trying out with different links, finally it worked with this link : locahost:3000/api/v1/exam_questions without .json and that was the mistake which I was committing.

Sahil
  • 3,338
  • 1
  • 21
  • 43