Rails 3 response_with,路线约束和资源 [英] Rails 3 respond_with, route constraints and resources

查看:152
本文介绍了Rails 3 response_with,路线约束和资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建版本控制的API,因此我具有以下嵌套控制器:

I'm building a versioned API, so I have the following nested controllers:

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

可通过子域访问API.我有以下路线:

The API is accessed via a subdomain. I have the following routes:

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

这会产生我想要的URL类型(/v1/事件).

This produces the type of URL I want (/v1/events).

我面临的问题是在Api::V1::EventsController中使用responds_with时.仅执行以下简单操作会失败,并显示错误too few arguments:

The problem I'm facing is when using responds_with in Api::V1::EventsController. Just doing something as simple as the below fails with the error too few arguments:

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

我知道respond_with是与资源一起使用的,但是我不确定如何从受约束的,作用域和命名空间的路由访问事件资源.我可以输出其他内容(例如current_user),而不是事件数组.帮助吗?

I know respond_with is meant to be used with resources, but I'm not sure how the events resource should be accessed from the constrained, scoped, and namespaced route. I can output other things (such as current_user), just not an array of events. Help?

更新:

这是有效的方法:

# 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

因此,也许它与事件模型有关,特别是集合与数组.我会继续调查.

So maybe it has something to do with the Event model, specifically collections vs. arrays. I'll keep investigating.

推荐答案

尝试使用类似的东西:

respond_with [:v1, @events]

这篇关于Rails 3 response_with,路线约束和资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆