Why does trying to use Grape with Rails fail with “uninitialized constant API”?

前端 未结 3 450
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-16 04:21

I would like someone to explain why this is happening in Rails (4.1.8) with Grape (0.10.1)

so this is my API:

app/api/root.rb:

m         


        
3条回答
  •  难免孤独
    2021-01-16 04:51

    It's because app/api is the top-level folder for your API classes, not app.

    From Grape's documentation:

    Place API files into app/api. Rails expects a subdirectory that matches the name of the Ruby module and a file name that matches the name of the class. In our example, the file name location and directory for Twitter::API should be app/api/twitter/api.rb.

    Therefore the correct location for an API::Root class would actually be app/api/api/root.rb, not /app/api/root.rb—though that is the correct location for a class in the top-level namespace, which is why the second example you give (with classes removed from the API module) works.

    I recommend you keep your API classes together in their own module, though, and move them to a matching subfolder beneath app/api.

提交回复
热议问题