アプリケーションには、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:レール3.2.8を使用しています