【发布时间】:2012-09-29 05:16:43
【问题描述】:
我有一个 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 位置的帮助程序是 artwork_url。我如何告诉它改用api_v1_artwork_url?我可以为 URL 助手设置别名吗?
【问题讨论】:
标签: ruby-on-rails