2014-02-27 66 views
0

我在Mac OS Mavericks上使用Ruby 2.0.0p353。Rails API问题

虽然继 “#350 REST API Versioning” railscast在我的网站上创建一个RESTful API,我遇到了这个错误:

RuntimeError in Api::V1::SurveysController#index 
Circular dependency detected while autoloading constant Api::V1::SurveysController 

Rails.root: /Users/thomashammond89/SurveyMe 
Application Trace | Framework Trace | Full Trace 

Request 

Parameters: 

{"format"=>"json"} 

这里是我的surveys_controller,在控制器/ API/V1发现:

module Api 
    module V1 
    class ProductsController < ApplicationController 
     before_filter :restrict_access 
     respond_to :json 

     def index 
     respond_with Survey.all 
     end 

     def show 
     respond_with Survey.find(params[:id]) 
     end 

     def create 
     respond_with Survey.create(params[:survey]) 
     end 

     def update 
     respond_with Survey.update(params[:id], params[:survey]) 
     end 

     def destroy 
     respond_with Survey.destroy(params[:id]) 
     end 

     def restrict_access 
     api_key = ApiKey.find_by_access_token(params[:access_token]) 
     head :unauthorized unless api_key 
     end 
    end 
    end 
end 

api_key型号:

class ApiKey < ActiveRecord::Base 
    before_create :generate_access_token 

    private 

    def generate_access_token 
    self.access_token = SecureRandom.hex 
    end while self.class.exists?(access_token: access_token) 

end 

还有我的路线:

namespace :api, defaults: {format: 'json'} do 
    namespace :v1 do 
     resources :surveys 
    end 
    end 

任何想法我做错了什么或错误来自哪里?我试着在“Circular dependency detected while autoloading constant”之后将我的Rails版本从4.0.1更改为4.0.0。

Circular dependency detected while autoloading constant User”暗示问题是他有两个相同的控制器,一个在api文件夹下。我也有两个surveys_controllers.rb。这是否会导致问题?如果是这样,有人知道我应该如何在api文件夹下设置控制器,以便我的调查显示? railscast让我创建了第二个控制器。

+0

你怎么会有两个'surveys_controllers.rb'? OSX文件系统不允许在单个目录中存在重复的文件名。 –

+0

那么一个是在我的控制器文件夹。另一个位于控制器文件夹中的api/v1文件夹中。在railscast –

+2

之后您是否曾尝试调用您的调查控制器的专有名称?目前它被定义为'ProductsController'。 –

回答

2

generate_access_token功能应该是

def generate_access_token 
    begin 
    self.access_token = SecureRandom.hex 
    end while self.class.exists?(access_token: access_token) 
end