Namespacing JSONAPI resource and controller - ruby-on-rails

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?

Related

LoadError (Unable to autoload constant Api::V1::UserTokenController) Knock Gem

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

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

Adding namespace in Rails and routing error

I tried to add namespace in my RoR project.
It works as expected:
controller:
class Dashboard::CategoriesController < ApplicationController
...some code
end
routes.rb
namespace "dashboard" do
resources :categories
end
but it doens'y work:
class Dashboard::UsersController < ApplicationController
...some code
end
class Dashboard::CardsController < ApplicationController
...some code
end
routes.rb:
namespace "dashboard" do
resources :users do
resources :cards do
member do
post 'review'
end
end
end
end
it throws an routing error: uninitialized constant CardsController
what's wrong?
Rails auto loads class if its name match file name. Error indicates that CardsController class is not loaded, so most probably you named your controller file wrongly. It should be app/controllers/dashboard/cards_controller.rb.
First of all, you'll be better making your routes more compacted:
#config/routes.rb
namespace :dashboard do
resources :users do
resources :cards do
post :review #-> domain.com/dashboard/users/:user_id/cards/1
end
end
end
The error it self will likely be caused by the way in which you're trying to call the controller. Typically, you'll receive errors with the namespace prepended to the class name (Dashboard::CardsController). In this instance, it just says CardsController.
You'll need to look at how you're calling the route.
Specifically, I presume you're using a form_for - which will build the route for you. If this is the case, you'll need to use the namespace within the form_for declaration itself:
<%= form_for [:dashboard, #user, #card], url: dashboard_user_cards_review(#user, #card) do |f| %>

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