Skip to content

Problem in namespacing controllers #445

@danjvarela

Description

@danjvarela

What I hope to achieve

Have routes mapped to namespaced controllers (Api::V1::NotesController) much like the one below:

image

Attempted solution:

  1. Create new rails app: rails new sample-graphiti-api --api -m https://www.graphiti.dev/template
  2. Press <ENTER> if asked for the namespace (default is /api/v1)
  3. rails generate model Note content:string
  4. rails generate graphiti:resource note content:string created_at:datetime (rails generate graphiti:resource api/v1/note content:string created_at:datetime doesn't work so we do it manually)
  5. Move app/resources/note_resource.rb to app/resources/api/v1/note_resource.rb and change NoteResource to Api::V1::NoteResource
class Api::V1::NoteResource < ApplicationResource
  attribute :content, :string
  attribute :created_at, :datetime, writable: false
end
  1. Move app/controllers/notes_controller.rb to app/controllers/api/v1/note_resource.rb and change NotesController to Api::V1::NotesController. Also, change all occurrences of NoteResource to Api::V1::NoteResource. File should look like this:
class Api::V1::NotesController < ApplicationController
  def index
    notes = Api::V1::NoteResource.all(params)
    respond_with(notes)
  end

  def show
    note = Api::V1::NoteResource.find(params)
    respond_with(note)
  end

  def create
    note = Api::V1::NoteResource.build(params)

    if note.save
      render jsonapi: note, status: 201
    else
      render jsonapi_errors: note
    end
  end

  def update
    note = Api::V1::NoteResource.find(params)

    if note.update_attributes
      render jsonapi: note
    else
      render jsonapi_errors: note
    end
  end

  def destroy
    note = Api::V1::NoteResource.find(params)

    if note.destroy
      render jsonapi: { meta: {} }, status: 200
    else
      render jsonapi_errors: note
    end
  end
end
  1. Change the routes to:
Rails.application.routes.draw do
  namespace :api do
    namespace :v1 do
      resources :notes, defaults: { format: :jsonapi }
    end
  end
end
  1. Run the migrations and start the server
  2. Do a GET /api/v1/notes. An error occurs:

Graphiti::Errors::InvalidEndpoint (          Api::V1::NoteResource cannot be called directly from endpoint /api/v1/notes#index!

          Either set a primary endpoint for this resource:

          primary_endpoint '/my/url', [:index, :show, :create]

          Or whitelist a secondary endpoint:

          secondary_endpoint '/my_url', [:index, :update]

          The current endpoints allowed for this resource are: [{:path=>:"/api/v1/notes", :full_path=>:"/api/v1/api/v1/notes", :url=>:"http://localhost:3000/api/v1/api/v1/notes", :actions=>[:index, :show, :create, :update, :destroy]}]
):


Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions