2017-05-25 71 views
0

我使用Rails 5創建一個API,但不能使用Rail 5 API only模式,因爲我需要Devise for user auth ...下面是如何我有我的第一個API控制器設置:Rails 5,如何設置不帶API版本的Rails的api控制器

的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 

當我去,像這樣來測試這個瀏覽器: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: 

如何將此設爲任何建議工作?由於

+0

你確定你有沒有定義'別處SkillsController'?似乎Ruby期待它從另一個類繼承。這也可能與重裝或彈簧有關;一定要強迫他們停下來重新開始。 – coreyward

+0

謝謝你提到。我只查了一下,SkillsController不在應用程序的其他地方。 – AnApprentice

回答

1

設置你的控制器是這樣的:

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

不錯,謝謝!!! – AnApprentice