2012-10-22 104 views
0

我在我的应用程序中有一些我想用作api的控制器。在这个API中,我需要使用版本控制。忽略模型

在我routes.rb我真的使用这个:

require 'api_constraints' 

(...)

scope '/:target/:version', :module => :api, :constraints => { :version => /[0-z\.]+/ } , :defaults => { :format => 'json' } do 
    scope :module => :v1, :constraints => ApiConstraints.new(:version => 1, :default => true) do 
     match '/list' => 'sample#list' 
    end 
    end 

api_constraints.rb

class ApiConstraints 

    def initialize(options) 
    @version = options[:version] 
    @default = options[:default] 
    end 

    def matches?(req) 
    @default || req.headers['Accept'].include?("application/waytaxi.api.v#{@version}") 
    end 

    def self.version 
    @version 
    end 

end 

在我SampleController.rb

module Api 
    module V1 
    class SampleController < ApiBaseController 

     def list 
     render json: Model.find_by_id(params[:id]) 
     end 

    end  
    end 
end 

ApiBaseController

module Api 
    class ApiBaseController < ApplicationController 
    before_filter :authenticate 
    skip_before_filter :verify_authenticity_token 

    private 

    def authenticate 
     # if params[:target] == "ios" 
     # render :json => {status: 404} 
     # return false   
     # end 
    end 
    end 
end 

的问题是:

每当我试图打电话给Model我得到这个错误:如果我使用

uninitialized constant Api::V1::SampleController::Model 

::Model我得到这个错误:

uninitialized constant Model 

是的,我的数据库上有这个模型。如果我在SampleController之外使用Model.all,我会得到对象。

P.S .:我正在使用rails 3.2.8

回答

0

发现我的问题。

我的模型是复数,我控制器上我是在单数

叫它