LoadError (Unable to autoload constant Api::V1::UserTokenController) Knock Gem - ruby-on-rails

As this is a brand new App, I'm using Rails 6 beta.
I'm trying to use the Knock Gem, but when trying to get an API response from posting a user I get this error:
LoadError (Unable to autoload constant Api::V1::UserTokenController,
expected
/Users/Simon/Sites/TGD/ginbackend/app/controllers/api/v1/user_token_controller.rb
to define it):
However, I do have user_token_controller.rb file in that location. I'm using a namespace for my API endpoint:
Rails.application.routes.draw do
namespace :api do
namespace :v1 do
post 'user_token' => 'user_token#create' # <= manually placed this under the namespace
resources :distilleries
resources :botanicals
resources :gins
resources :botanicals_gins
end
end
end
As per the Knock documentation, I have also updated my application_controller.rb, do note this is located at app/controllers/ and not app/controllers/api/v1 is that an issue?
class ApplicationController < ActionController::API
include Knock::Authenticable
private
def authenticate_v1_user
authenticate_for V1::User
end
end

Solved.
It's all in the details (as always).
As the knock installer auto generated user_token_controller it didn't know about my routes namespace.
So I Changed:
class UserTokenController < Knock::AuthTokenController
to:
class Api::V1::UserTokenController < Knock::AuthTokenController

Related

Ruby on Rails add nested controller/resource

I have an application that I want to have a route that is /admin/active_vulnerabilities but when I generate the controller as rails generate controller ActiveVulnerabilities and put the following in my routes.rb
namespace :admin do
resources :users
resources :active_vulnerabilities
# Admin root
root to: 'application#index'
end
But I get the error uninitialized constant Admin::ActiveVulnerabilitiesController so I changed my controller to class Admin::ActiveVulnerabilitiesController < ApplicationController
I then get the error Unable to autoload constant ActiveVulnerabilitiesController, expected /home/luke/projects/vuln_frontend/app/controllers/active_vulnerabilities_controller.rb to define it but the file mentioned is my controller named exactly as that.
Your controller should be put in app/controllers/admin/ because the namespace. Otherwise, you can forget this directory and the namespace and use just scope
scope :admin do
resources :active_vulnerabilities
end
class ActiveVulnerabilitiesController < ApplicationController

Namespacing JSONAPI resource and controller

I'm trying to add a namespace to my 'Category' controller and resource.
So the first thing I did was to move the categories_controller.rb to app/controllers/api/v1/categories_controller and the category_resource.rb to app/resources/api/v1/
And then I redeclared these artifacts as following:
Controller
module Api
module V1
class CategoriesController < ApplicationController
#before_action :doorkeeper_authorize!
end
end
end
Resource
module Api
module V1
class CategoryResource < JSONAPI::Resource
attribute :name
end
end
end
And in routes.rb I moved the categories route to
namespace :api do
namespace :v1 do
jsonapi_resources :categories
end
end
I already got different erros trying to solve this issue. To the current configuration, this is the error I get:
JSONAPI: Could not find resource 'categories'. (Class CategoryResource not found) (NameError)
What am I doing wrong?
Based on the documentation here (https://github.com/cerebris/jsonapi-resources) you should not move the resource.
And it should not be in the modules.
Your code looks fine - I have something similar with jsonapi-resources 0.7.0:
class Api::V1::UsersController
...
class Api::V1::UserResource < BaseResource
...
namespace :api do
namespace :v1 do
jsonapi_resources :users do
jsonapi_relationships
end
is it possible your rails load path is trying to load the api/v1 directory directly rather than treating it as a module subfolder?

uninitialized constant when calling class method in controller

I have a class called Device. It has a model device.rb
I have set the routing up so that the same controller is called from two different paths. i.e. the paths:
/driver_api/v1/devices
and
/sender_api/v1/devices
both call the following controller:
/user_api/v1/devices
In my routes.rb I have:
namespace :driver_api do
namespace :v1 do
resources :devices, :only => [:create], controller: '/user_api/v1/devices'
end
end
namespace :sender_api do
namespace :v1 do
resources :devices, :only => [:create], controller: '/user_api/v1/devices'
end
end
Now, in my devices controller, I'm trying to call a Device class method. i.e. in my controller:
class UserApi::V1::DevicesController < ApplicationController
Devise.method_name(input)
end
But i get an error:
uninitialized constant UserApi::V1::DevicesController::Device
Why am I getting this error?
Because you wrote Devise and not Device that could be a good reason.
If that is just a typpo from the question, here's another alternative.
Sometimes there's some kind of naming problem when the classes are defined in the way you did (I ignore the reason why that happens)
Try to decompose the class scoping by defining the modules:
module UserApi
module V1
class DevicesController < ApplicationController
# rest
end
end
end

Rails resource route under namespace leading to "not a supported controller name."

I'm building an API in Rails and I'd like for it to be versioned. My routes.rb file consists of:
Rails.application.routes.draw do
namespace :V1 do
resources :users
end
end
And I have my controller under /app/controllers/V1/users_controller.rb, which has this content:
module V1
class UsersController < ApplicationController
def index
render json: {message: "This is a test!"}
end
end
end
When I try to run the rails server on the command line I get the following error:
`default_controller_and_action': 'V1/users' is not a supported controller name. This can lead to potential routing problems. See http://guides.rubyonrails.org/routing.html#specifying-a-controller-to-use
I've looked at the link the error message gave me, however it seems to be how to specify a controller to use with a resource. Rather than doing that, can Rails not automatically determine it from my directory structure?
try this one:
Rails.application.routes.draw do
namespace :v1 do
resources :users
end
end

Rails and Devise - Namespace is Uninitialised Constant

I am trying to use namespaces to declare an api.
My routes.rb contains:
devise_scope :user do
namespace :api do
namespace :v1 do
match 'log_in', :to => 'token_authentications#log_in', :via => "post"
end
end
end
And my *token_authentications_controller.rb* looks like this:
class Api::V1::TokenAuthenticationsController < ApplicationController
...
def log_in
...
end
...
end
When I hit: api/v1/log_in I get:
Routing Error
uninitialized constant Api
So do I need to declare the namespace somewhere?
Rails expects namespaces to follow directory structure, unless I'm mistaken.
Given your class name for your controller, Api::V1::TokenAuthenticationsController, rails expects it to live in app/controllers/api/v1/token_authentications_controller.rb.
If you just move your controller to the correct folder, I think you should be fine.
You might also want to make sure to actually declare the namespace modules somewhere, like for instance refactoring your controller as such:
module Api
module V1
class TokenAuthenticationsController
...
end
end
end

Resources