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

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

PS:我正在使用rails 3.2.8

4

1 回答 1

0

发现了我的问题。

我的模型是复数形式,在我的控制器上我用单数形式调用它

于 2012-10-25T13:36:44.737 回答