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
Upgrading the grape version, I got this error:
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
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
Related
I'm trying to create the below route but I am running into a "routing error" of "uninitialized constant Foo::Bar::Biz"
/foo/bar/biz/<biz_id>/custom
My routes file is as follows:
namespace(:foo) do
namespace(:bar) do
resources(:biz, only: []) do
get('/custom' => 'foo/bar/biz/custom#index') # I have also tried just custom#index
end
end
end
When I run rake routes I see the route and the controller.
/foo/bar/biz/:biz_id/custom(.:format) foo/bar/biz/custom#index
My controllers file structure is this:
controllers/foo/bar/biz/custom_controller.rb
I don't currently have a controller for biz, but I have tested with one present.
My custom_controller is as follows:
module Foo
module Bar
module Biz
class CustomController < FooController
def index
// do something
end
end
end
end
end
I suspect my routes are setup correctly and my error is in my controller or module setup. Is there something I am missing?
Change your path
'foo/bar/biz/custom#index'
to path starting with root (/)
namespace(:foo) do
namespace(:bar) do
resources(:biz, only: []) do
get('/custom' => '/foo/bar/biz/custom#index')
end
end
end
I'm getting the following error message when trying to access the admin/admin_users route provided by the Administrate gem: LoadError in Admin::AdminUsersController#index. The other admin routes (i.e. Users, Topics, Posts) work fine. I haven't changed any of the other default configuration for Administrate.
I encountered the bug while working on a code walkthrough available here: https://rails.devcamp.com/professional-rails-development-course/advanced-user-features/customizing-forms-administrate-dashboard. The URL contains a link to the repo; my local version is identical aside from using rails 5.
Portions of relevant files are included below. Any idea what might be causing this error?
localhost:3000/admin/admin_users
Unable to autoload constant Admin::AdminUsersController, expected .../app/controllers/admin/admin_users_controller.rb to define it
...
else
require_or_load(expanded, qualified_name)
raise LoadError, "Unable to autoload constant #{qualified_name}, expected #{file_path} to define it" unless from_mod.const_defined? (const_name, false)
return from_mod.const_get(const_name)
end
elsif mod = autoload_module!(from_mod, const_name, qualified_name, path_suffix)
../app/controllers/admin_user_controller.rb
module Admin
class ApplicationController < Administrate::ApplicationController
end
end
../config/routes.rb
Rails.application.routes.draw do
namespace :admin do
resources :users
resources :topics
resources :admin_users
resources :posts
root to: "users#index"
end
...
../vendor/gemfile.rb
...
gem "administrate", "~> 0.3.0"
gem 'bourbon'
Im trying to create simple Ruby on Rails REST API.
app/controllers/api/vi/product_controller.rb
module Api
module V1
class ProductController < ApplicationController::API
def index
render json: {message: 'Welcome!'}
end
end
end
end
config/routes.rb
Rails.application.routes.draw do
namespace :api do
namespace :v1 do
get '/product', to: 'product_controller#index', as: 'product'
end
end
end
When I run project on localhost, I get uninitialized constant Api::V1::ApplicationController routing error. Can anyone help to such Ruby on Rails newbie as I am?
you just need to create a folder inside controllers called api and a v1 folder inside api.
You should provide all the controllers inside v1 folder.
In your app/controllers/api/v1/product_controller.rb
class Api::V1::ProductController < ApplicationController
def index
render json: {message: 'Welcome!'}
end
end
In your routes:
Rails.application.routes.draw do
namespace :api do
namespace :v1 do
get '/product', to: 'product_controller#index', as: 'product'
end
end
end
you nested the route, so it should be '/api/v1/product`
if you run rake routes from your console, you will get a list of all available routes.
for more information about routing and nested routes, have a look at rails guides
change this and try:
module Api
module V1
class ProductController < ApplicationController
def index
render json: {message: 'Welcome!'}
end
end
end
end
I have referred this link
to create my own api but facing an routing error since this is the first time am using namespace.
This is the controller I have
class API::IndexController < ApplicationController
def index
#clients = Client.all
respond_to do |format|
format.json
end
end
end
my route has
namespace :api do
resources :index
end
Here is my inflection.rb
ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.acronym 'API'
end
Once i finish it I tried localhost:3000/api/index.json but throwing error as
Routing Error
uninitialized constant API
Can anyone help.
Even if this question is pretty old...
I had the same problem and my solution was to rename the api-folders (in app/controllers and app/views) from "API" (full caps) to "api" (all lower case)
Hope this helps
You must have the same name in your class same as your namespace. Try this:
class Api::IndexController < ApplicationController
def index
#clients = Client.all
respond_to do |format|
format.json
end
end
end
routes:
namespace :api do
resources :index
end
If you are defining your namespace for IndexController as API then in your routes.rb it should be written as
namespace :a_p_i do
resources :index
end
Otherwise, if you want to use what you have written in your routes file then just change the namespace for IndexController form API to Api . For Ex : Api::IndexController
To solve this add the following file
#./spec/support/api/helper.rb
module ApiHelper
include Rack::Test::Methods
def app
Rails.application
end
end
RSpec.configure do |c|
c.include ApiHelper, :type => :api
end
Then include it in spec_helper.rb
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
Read https://gist.github.com/alex-zige/5795358 for more info.
This is a somewhat old question, but my experience could still be useful.
I found my problem was because of a typo in the file name, I had a class named ApiFooController and the file was named api_foo_contoller.rb (notice the missing 'n' in the file name).
So it seems this error comes down to one general problem, and it's failig to correctly follow the naming conventions.
My guess is the controller was probably placed under controller/ path instead of controller/api and also the namespace should've been Api, not API (Not sure if adding API as an Acronym does the trick, I've never tried it.
It seams like the environment is not loaded when I try to execute a shell command inside rails project.
I fix it like this:
rcmd = 'rake'
rcmd = '/opt/ruby-enterprise-1.8.7-2010.02/bin/rake' if Rails.env.to_s == 'production'
rcmd = '/usr/local/bin/rake' if Rails.env.to_s == 'staging'
`cd #{Rails.root}; #{rcmd} RAILS_ENV=#{Rails.env} ts:in:delta`
Is there a better way?
Why are you trying to shell out and invoke Rake from within the Rails project? Just make a class that does all the work.
# lib/ts_in_delta.rb
class TsInDelta
def run
# code that does all the work here
end
end
You can use this from Rake quite easily:
# lib/tasks/ts_in_delta.rake
namespace :ts do
namespace :in do
task :delta => [:environment] do
TsInDelta.new.run
end
end
end
# shell
$ rake ts:in:delta
You can also use this from anywhere else in your Rails project quite easily, such as from a controller.
# app/controllers/posts_controller.rb (snippet)
class PostsController < ApplicationController
def ts_in_delta
TsInDelta.new.run
render :json => true
end
end
# config/routes.rb (snippet)
MyApp::Application.routes.draw do
resources :posts do
collection do
post 'ts_in_delta'
end
end
end