2011-03-11 85 views
1

我建立一个版本的API,所以我有以下嵌套控制器:的Rails 3 respond_with,路由约束和资源

  • ApiController < ApplicationController
  • Api::V1Controller < ApiController
  • Api::V1::EventsController < Api::V1Controller

的API通过子域访问。我有以下路线:

constraints(:subdomain => "api") do 
    scope :module => 'api' do 
     namespace :v1 do 
     resources :events 
     end 
    end 
    end 

这产生了我想要的URL类型(/ v1/events)。

我面临的问题是在Api::V1::EventsController中使用responds_with时。只是这样做的以下简单的东西失败,错误too few arguments

def index 
    @events = Event.all 
    respond_with(@events) 
end 

我知道respond_with是为了与资源使用,但我不知道怎么的事件资源应该从约束,范围的访问和名称空间路由。我可以输出其他东西(如current_user),而不是一系列事件。帮帮我?

更新:

这里是什么工作:

# a single resource 
def index 
    @event = Event.all.first 
    respond_with @event 
end 

# an array of a completely different resource 
def index 
    @user = User.all 
    respond_with @user 
end 

因此,也许它是与事件模型,专门收集与阵列。我会继续调查。

+0

同样的事情对我来说,和我使用mongoid了。 – 2011-07-04 01:54:06

回答

0

尝试使用类似:

respond_with [:v1, @events] 
+0

这没有奏效,我得到了相同的“太少参数”错误。 – Intelekshual 2011-03-11 12:21:47

+0

你能添加或链接到你的完整日志吗? – Yannis 2011-03-11 14:37:20

+0

您是否尝试添加第三个参数:respond_with [:api,:v1,@events]? – Yannis 2011-03-11 14:45:24