JSONAPI Resource routes not showing up

1.2k Views Asked by At

I am trying to use JSONAPI Resource gem to create an API for my rails application. I want to be able to leverage normal rails routing with my controllers and have a namespaced API as well.

So far I have something like

# sitting in resources/api/goal_resource.rb
module Api
  class GoalResource < JSONAPI::Resource
    attributes :name, :description, :progress
  end
end

Rails.application.routes.draw do
  devise_for :users,
    path: '',
  controllers: {
    registrations: 'users/registrations',
    invitations: 'users/invitations',
    sessions: 'users/sessions'
  },
  path_names: {
    edit:  'settings/profile'
  }

  devise_scope :user do
    authenticated :user do
      root 'dashboard#index', as: :authenticated_root
    end

    unauthenticated do
      root 'users/sessions#new', as: :unauthenticated_root
    end
  end

  post '/invitation/:id/resend', to: 'users/invitations#resend', as: :resend_invitation

  resources :goals do
    resources :goal_comments
    resources :goal_followers, only: [:index]
    member do
      post :on_target, as: :on_target
    end
  end

  resources :users, path: '/settings/users', only: [:index, :update, :edit, :destroy]
  resources :teams, path: '/settings/teams', only: [:index, :new, :create, :update, :edit, :destroy]
  resources :notifications, only: [:index]

  get "my_goals", to: "my_goals#index", as: :my_goals
  get "user_goals/:user_id", to: "user_goals#index", as: :user_goals
  get "team_goals/:team_id", to: "team_goals#index", as: :team_goals

  namespace :api, defaults: { format: 'json' } do
    jsonapi_resources :goals
  end
end


# Gemfile
source 'https://rubygems.org'
ruby '2.1.2'
gem 'jsonapi-resources'
# other gems here

# models/goal.rb
class Goal < ActiveRecord::Base
  # some more code here
end

Is it possible to use this gem in conjunction with normal routing? What am I doing wrong? rake routes returns all the routes for my application, but no api routes.

2

There are 2 best solutions below

2
On

The most likely reason the jsonapi_resources is not working because you have to derive your Application Controller from JSONAPI::ResourceController as well:

class ApplicationController < JSONAPI::ResourceController
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :null_session
end

Another thing is (This is not causing your routes to disappear), use the plural of the resource in the route. Use goals like below NOT goal:

Rails.application.routes.draw do
  # other routes here

  namespace :api, defaults: { format: 'json' } do
    jsonapi_resources :goals
  end
end

Here is a demo application that uses jsonapi_resources.

0
On

I managed to get it working

# app/controllers/api/api_controller.rb
module Api
  class ApiController < JSONAPI::ResourceController
  end
end

# app/controllers/api/goals_controller.rb
module Api
  class GoalsController < ApiController
  end
end

I just created a separate ApiController and made that inherit from the JSONAPI::ResourceController then I created an additional controller for my goals.

My normal controllers/goals_controller.rb still works perfectly!