1
votes

Following this tutorial, I created the API of my existing blog web application on Rails. I am getting the error:

uninitialized constant API

This is my code:

lib/api/v1/articles.rb:

module API
 module V1
   class Articles < Grape::API
  version 'v1'
  format :json

  resource :articles do
    desc "Return list of recent posts"
    get do
     Article.recent.all
    end
  end
 end
end
end

lib/api/v1/root/rb

module API
 module V1
 class Root < Grape::API
  mount API::V1::Articles
     end
 end
end

lib/api/root.rb

module API
class Root < Grape::API
    prefix 'api'
    mount API::V1::Root
    end
end

lib/tasks/routes.rake

namespace :api do
  desc "API Routes"
  task :routes => :environment do
   API::Root.routes.each do |api|
  method = api.route_method.ljust(10)
  path = api.route_path.gsub(":version", api.route_version)
  puts "     #{method} #{path}"
 end
end
end

config/routes.rb

 Rails.application.routes.draw do
  mount API::Root => '/'
 get 'welcome/index'
 root 'welcome#index'
  resources :articles
  end

This is the existing web application code:

app/controllers/articles_controller.rb

class ArticlesController < ApplicationController
def new
end
def create
   # render plain: params[:article].inspect
     @article = Article.new(article_params)

 @article.save
 redirect_to @article
    end
def show
 @article = Article.find(params[:id])
end
private

def article_params
    params.require(:article).permit(:title,:text)
    end

end

Now I'm in the Accessing API routes part of this article. When I run rake routes it gives the error, uninitialized constant API.. What I'm doing wrong.

Edit: As per the comment, giving the detailed error

rake aborted!
NameError: Uninitialized constant API
F:/blog/config/routes.rb:2:in 'block in <top (required)>'
F:/blog/config/routes.rb:1:in '<top (required)>'
C:in 'execute_if_updated'
F:/blog/config/environment.rb:5:in '<top (required)>'
Tasks: TOP =>routes =>environment

Contents of environment.rb

# Load the Rails application.
require File.expand_path('../application', __FILE__)

# Initialize the Rails application.
Rails.application.initialize!

When I insert config.autoload_paths += Dir["#{config.root}/lib/**/"] in your config/application.rb, and run rake routes, it gives the error can't convert Symbol into String

enter image description here

Upgrading the grape version, I got this error:

enter image description here

2
Can you please post the error trace including the line(s) of code where the error is happening?Surya
@Surya: I have updated the error above. Please check.inquisitive
Add this line: config.autoload_paths += Dir["#{config.root}/lib/**/"] in your config/application.rb file and then try again?Surya
Where do i place this code in the application.rb. I placed it inside class. It gives some other error now.inquisitive
Place it inside: class Application < Rails::Application . did you try? Does it work? What is the output?Surya

2 Answers

2
votes

As per the discussion, you need to update your grape gem's version to 0.9.0 and then you need to add this line in your Gemfile:

gem 'grape', '0.9.0'

and then:

$ bundle install
1
votes

As of version 0.11.0, the documentation says

Rails

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.

Simple solution would be creating another api folder and moving entire files & dirs into app/api/api folder