Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9287659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T19:43:50+00:00 2026-06-18T19:43:50+00:00

In every examples I see, people only implement one giant api.rb file. Ex: intridea/grape

  • 0

In every examples I see, people only implement one giant api.rb file. Ex:

  • intridea/grape
  • bloudraak/grape-sample-blog-api
  • djones/grape-goliath-example

While this approach works fine as is, it can quickly become crowded and difficult to maintain so I would like to split things up on my app.

For instance, I would like to split my entities from my resources, and then split up my resources between different files. For examples:

app
 - api
   api.rb
   - entities
     - weblog.rb
     - post.rb
     - comment.rb
   - resources
     - weblog.rb
     - post.rb
     - comment.rb

Now, api.rb would be something like:

require 'grape'
module Blog
  class API < Grape::API
    prefix "api"
  end
end

app/api/entities/post.rb would be something like:

module Blog
  module Entities
    class Post < Grape::Entity
      root 'posts', 'posts'
      expose :id
      expose :content
    end
  end
end

app/api/resources/post.rb would be something like:

module Blog
  class API < Grape::API
    resource :posts do
      get do
        present Post.all, with: Blog::Entities::Post
      end

      desc "returns the payment method corresponding to a certain id"
      params do
        requires :id, :type => Integer, :desc => "Post id."
      end
      get ':id' do
        present Post.find(params[:id]), with: Blog::Entities::Post
      end
    end
  end
end

When we do this, we encounter the following message:

Expected /blog-app/api/resources/post.rb to define Post


SOLUTION (thanks to dB. and my co-workers)

You have to change the structure to something like:

app
 - api
   api.rb
   - resources
     - post_api.rb

Then, in the post_api.rb

module Blog
  class Resources::PostAPI < Grape::API
    resource :posts do
      get do
        present Post.all
      end
    end
  end
end

Finally, the api.rb becomes:

require 'grape'
module Blog
  class API < Grape::API
    prefix 'api'
    version 'v1', :using => :path
    format :json

    mount Blog::Resources::PostAPI => '/'
  end
end

Now /api/v1/posts should work 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T19:43:51+00:00Added an answer on June 18, 2026 at 7:43 pm

    The class in post.rb should be Post, not API. Then you can mount the Post API inside class API.

    class API < Grape::API
      mount Blog::Post => '/'
    end
    

    To avoid confusion I would put Post in a Resources namespace, too or rename it to PostAPI.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I see a lot of Reachability examples where people only display a message when
Every example I see seems to be for recursively getting files in subdirectories uses
Are there any good examples of mvc routing wherein every 404 page not found
I've ran through several examples over the web, and found that every single time
I think I understand strong typing , but every time I look for examples
Every time I see an example of office Automation using.NET, the language used is
I'm making a little open source mp3 player for people to see the code,
What kind of server do you people see in real projects? 1) Web Services
Output or Input filtering? I constantly see people writing filter you inputs, sanitize your
One can often see class names carrying a reference to the namespace to which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.