Search code examples
ruby-on-railsapiruby-on-rails-5

How to setup api controller without the API version of Rails


I'm using Rails 5 to create an API but can not use the Rail 5 API only mode as I need Devise for user auth. Here's is how I have my first API controller setup:

routes.rb

Rails.application.routes.draw do
  namespace :api do
    namespace :v1 do
        resources :skills
      end
  end
  get '/*path' => 'home#index'
end

/controllers/api_controller.rb

class ApiController < ActionController::API

end

/controllers/api/v1/skills_controller.rb

class SkillsController < ApiController

  def index
    @skills = Skill.all
    json_response(@todos)
  end


  def json_response(object, status = :ok)
    render json: object, status: status
  end

end

When I go to test this in the browser like so: http://localhost:4300/api/v1/skills.json

I'm getting the following errors in the rails log:

Started GET "/api/v1/skills.json" for 127.0.0.1 at 2017-05-25 08:44:12 -0700
TypeError (superclass mismatch for class SkillsController):
app/controllers/api/v1/skills_controller.rb:1:in `<top (required)>'
Error during failsafe response: superclass mismatch for class SkillsController

ActionView::MissingTemplate (Missing template public/index.html with {:locale=>[:en], :formats=>[:html, :text, :js, :css, :ics, :csv, :vcf, :png, :jpeg, :gif, :bmp, :tiff, :svg, :mpeg, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip, :gzip], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby]}. Searched in:

How can I set this up to work?


Solution

  • Set up your controller like this:

    # /controllers/api/v1/skills_controller.rb
    class Api::V1::SkillsController < ApplicationController
    # fill in the rest
    end