24

我现在有一个Artwork仅由 API 端点操作的模型。(您很快就会明白为什么这很重要)。这些 API 端点在我的routes.rb文件中声明如下:

namespace :api do
  namespace :v1, :defaults => { :format => :json } do
    resources :artworks, :only => [:create, :destroy, :index, :show, :update]

这导致以下路线:

api_v1_artworks GET        /api/v1/artworks(.:format)                                             api/v1/artworks#index {:format=>:json}
                POST       /api/v1/artworks(.:format)                                             api/v1/artworks#create {:format=>:json}
api_v1_artwork  GET        /api/v1/artworks/:id(.:format)                                         api/v1/artworks#show {:format=>:json}
                PUT        /api/v1/artworks/:id(.:format)                                         api/v1/artworks#update {:format=>:json}
                DELETE     /api/v1/artworks/:id(.:format)                                         api/v1/artworks#destroy {:format=>:json}

相关代码:

class Api::V1::ArtworksController < Api::V1::ApiController
  def create
    artwork = Artwork.create(artwork_params)

    respond_with artwork
  end

问题

#create成功时,respond_with窒息:

`undefined method `artwork_url' for #<Api::V1::ArtworksController:0x007fea1b4c67f8>`

它期望 HTTP Location 的助手是artwork_url. 我如何告诉它使用api_v1_artwork_url呢?我可以给 URL 助手起别名吗?

4

1 回答 1

35

在这种情况下,您需要为响应者指定命名空间。尝试:

respond_with :api, :v1, artwork
于 2012-09-28T21:16:43.460 回答