【问题标题】:Rails 3 respond_with, route constraints and resourcesRails 3 respond_with,路由约束和资源
【发布时间】:2011-03-11 04:53:36
【问题描述】:

我正在构建一个版本化的 API,所以我有以下嵌套控制器:

  • 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

所以也许它与事件模型有关,特别是集合与数组。我会继续调查的。

【问题讨论】:

  • 对我来说也一样,我也在使用 mongoid。

标签: ruby-on-rails ruby-on-rails-3 resources routing mongoid


【解决方案1】:

尝试使用类似的东西:

respond_with [:v1, @events]

【讨论】:

  • 您可以在某处添加或链接到您的完整日志吗?
  • 您是否尝试添加第三个参数:respond_with [:api, :v1, @events] ?
  • 是的,我尝试添加第三个参数,以及我能想到的所有变体。我忘了提到我使用的是 Mongoid 而不是 ActiveRecord,如果这有影响的话。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-23
  • 1970-01-01
  • 2012-09-07
  • 1970-01-01
  • 2013-09-25
相关资源
最近更新 更多