[英]Rails 3: respond_with errors on undefined URL helper
我有一個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
#create
:
`undefined method `artwork_url' for #<Api::V1::ArtworksController:0x007fea1b4c67f8>`
它期望HTTP位置的幫助器是artwork_url
。 我怎么告訴它使用api_v1_artwork_url
呢? 我可以為URL幫助器設置別名嗎?
在這種情況下,您需要為響應者指定名稱空間。 嘗試:
respond_with :api, :v1, artwork
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.