【问题标题】:Ignore modules from Model忽略模型中的模块
【发布时间】:2012-10-22 19:44:41
【问题描述】:

我的应用程序中有一些我想用作 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

【问题讨论】:

    标签: mysql ruby-on-rails-3 api activerecord


    【解决方案1】:

    发现我的问题。

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

    【讨论】:

      猜你喜欢
      • 2013-03-21
      • 1970-01-01
      • 2023-03-15
      • 2013-04-30
      • 2020-09-22
      • 2016-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多