I am using Knock for JWT authentication for Rails 5 API.
I have this Routes file:
Rails.application.routes.draw do
namespace :api, constraints: { subdomain: 'api' }, path: '/' do
namespace :v1 do
post 'user_token' => 'user_token#create'
end
end
end
With this, I expect to be able to make a POST request to create new token like this:
POST http://api.domain.com/v1/user_token
but this request gives me the following error:
NameError: uninitialized constant API::V1::User
What I can understand is that Knock is trying to access the model User on the same namespace where the controller (user_token_controller) is. But my model are not namespaced:
class User < ApplicationRecord
has_secure_password
#...
end
My user_token_controller.rb
module API
module V1
class UserTokenController < Knock::AuthTokenController
end
end
end
What I dong wrong?
It's a bug already fixed but not yet published.
The temporary solution was using the gem from github code like:
gem 'knock', github: 'psantos10/knock', branch: 'master'
https://github.com/nsarno/knock/issues/120
Related
I'm trying to create an api controller for a model defined in a module
Model class:
module Reports
class Report < ActiveRecord::Base
end
end
Api controller:
class API::V2::ReportsController < API::V2::BaseController
end
Route:
namespace :api, defaults: { format: :json } do
namespace :v2 do
resources :reports
end
end
The error I get when try to call api/v2/reports is:
LoadError (Unable to autoload constant Report, expected /.../app/models/reports/report.rb to define it):
Is there a way to solve this, making the api controller look for Reports::Report instead of Report?
Your controller needs to include Reports somehow:
module Api
module V2
module Reports
class Report < API::V2::BaseController
your controller actions
end
end
end
end
Apart from that, I think it will be confusing if you use Reports::Report?
I'm using Rails 5 and I'm getting this error when calling a method in my Api. The thing is, the error only happens sometimes.
LoadError (Unable to autoload constant Api::V1::UsersController, expected /home/user/projects/project-name/app/controllers/api/v1/users_controller.rb to define it):
Relevant part of routes.rb:
Rails.application.routes.draw do
namespace :api do
namespace :v1 do
resources :users
end
end
end
Relevant part of the controller:
class Api::V1::UsersController < ApplicationController
I'm clueless of whats wrong, google a lot but couldn't find a solution.
Have you tried adding to the top of your controller
module Api
module V1
class className < ApplicationController
....
end
end
end
This should follow your directory structure.
I'm trying to setup a RoR API but I'm hitting a few roadbump along the way. After tiresome coding to get the database setup right I'm hitting an error that doesn't quite make sense to me (I'm new to RoR)
Unable to autoload constant Api::V1::SubmissionsController, expected ./app/controllers/api/v1/submissions_controller.rb to define it
I'm not quite sure what this error means and my interwebs searches are coming up empty on a clear answer. I'm hoping any of you can help me here.
Routes
Rails.application.routes.draw do
namespace :api, :defaults => {:format => :json} do
namespace :v1 do
get "/submissions", to: "submissions#index"
end
end
end
submission.rb
class Submission < ActiveRecord::Base
end
submissions_controller.rb
class API::V1::SubmissionsController < ApplicationController
def index
#submissions = Submission.all
render json: #submissions, status: :ok
end
end
Your file contains:
class API::V1::SubmissionsController < ApplicationController
Rails expects:
class Api::V1::SubmissionsController < ApplicationController
What does your folder structure look like for app/controllers & app/views? Depending on how these files were generated, by "rails generate" or manually created, if you namespace, the folder structure has to match in controllers and views directories.
app/controllers/api/v1
app/views/api/v1
This might be a really stupid question. But I am starting to create a rails API and created a skeleton api with one controller index action which returns a static json.
Here is the sequence of steps I followed:
rails-api new my-app
My gemfile has:
gem 'rails', '4.1.8'
My routes are as follows:
MyApp::Application.routes.draw do
namespace :api, defaults: {format: 'json'} do
namespace :v1 do
resources :projects
end
end
end
here is my controller:
module Api
module V1
class ProjectsController < ApplicationController
def index
render json: {message: 'Resource not found'}, status: 404
end
end
end
end
Here is my model which has nothing:
class Project < ActiveRecord::Base
end
So when I try run the app rails s and go to:
localhost:3000/certificates/ or localhost:3000/api/v1/certificates
I get the following error:
ActiveRecord::ConnectionNotEstablished
Eventually I am going to add a database and retrieve results from a real table, but I was wondering shouldn't my above code return the json "Resource not found" instead of trying to connect to active record.
Does it always require an activerecord connection? I am trying to understand how rails-api works.
When you run the server ActiveRecord gets loaded automatically, and it will whine since it wants a db to connect to. Ìf you really need to, you can "deactivate" ActiveRecord on startup by following this answer.
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