"Unable to autoload constant API, ~(LoadError)" error when using grape in rails

Asked 1 years ago, Updated 1 years ago, 97 views

I'm a beginner at ruby. Excuse me for the rudimentary question.

Using this article (http://blog.dakatsuka.jp/2011/05/27/grape.html), I am trying to create an API by adding grape to rails.

/Users/masah/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:481:in`load_missing_constant':Unable to autoload constant/appositive/appositive/appositive/appositive/appositive/appositive

I got the error

What should I do?

lib/api.rb

module Cospic
  class API <Grape::API
      # Add prefix to API access
      # ex) http://localhost:3000/api
      prefix "api"

      # Add version information to API access
      # ex) http://localhost:3000/api/v1
      version 'v1',:using=>:path

      resource "users" do
        # ex) http://localhost:3000/api/v1/users
        desc "returns all users"
        get do
          User.all
        end
        # ex)OK: http://localhost:3000/api/v1/users/1
        # ex)NG: http://localhost:3000/api/v1/users/a
        desc "return a user"
        params do
          requirements:id, type: Integer
        end
        get' —id'do
          User.find (params[:id])
        end
      end
    end
    end

config/routes.rb

Rails.application.routes.drawdo
  resources —Layers
  resources:posts
  resources —users
  resources —posts do
   collection do
    get'tag'
  end
end

 root'posts#index'

 get'/auth/:provider/callback'=>'sessions#create' 
 get'/logout'=>'sessions#destroy',:as=>:logout


  resource:user,only::destroy do
    get 'retire'
    end
   resources:posts, except::index do   
  # resources:posts, except: —index do
    resources:like, only:[:new,:create,:destroy]

 # collection do
  #  get 'tag /:tag_name'
  #end
  end

  # match '*path' = > 'application #error404', via: :all
  match':controller(/:action(/:id))', via: [:get,:post,:patch]

end
mountCospic::API=>"/"

config/application

requireFile.expand_path('../boot',__FILE__)
require 'rails/all'
module Cospic
class Application <Rails::Application

  config.generators do | g |
    g.orm —active_record
    end
    config.i18n.default_locale=:ja
    config.autoload_paths+=%W (#{config.root}/lib)
  end
end

Thank you for your cooperation.

ruby-on-rails grape

2022-09-30 20:30

1 Answers

Moving lib/api.rb to lib/cospic/api.rb will load well.

How Constants Read Automatically

The original article says:

#config/initializers/api.rb
require "# {Rails.root}/lib/api"

This explicitly reads lib/api.rb, so the Cospic::API written in lib/api.rb is loaded properly.

If you do not write explicitly, Rails will attempt to load the file based on the constant name Cospic::Api. If you write Cospic::Api, the Cospic namespace is specified, so you go to the cospic directory below the autoload_paths directory.For example, a file with the following location:

  • app/models/cospic/api.rb
  • app/controllers/cospic/api.rb
  • lib/cospic/api

Therefore, moving lib/api.rb to lib/cospic/api.rb will load well.

Read the Rails Guide below for more information on how to determine which file to load from the constant name. This case is around 6.2.

Constant Autoloading and Reloading—Ruby on Rails Guides

A side story

If the code is application-specific, it might be better to place it below the app. If you feel uncomfortable with other controllers and where to put your files, create a app/api directory and add it to autoload_paths.

Also, mountCospic::Api seems to be written in a different location. mountWrite in Rails.application.routes.draw.

Rails.application.routes.drawdo
  mountCospic::Api
end


2022-09-30 20:30

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.