2017-05-25 68 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