Reputation: 2082
I have an Artwork
model that is manipulated only by API endpoints right now. (You'll see why this is important shortly). Those API endpoints are declared like so in my routes.rb
file:
namespace :api do
namespace :v1, :defaults => { :format => :json } do
resources :artworks, :only => [:create, :destroy, :index, :show, :update]
This results in the following routes:
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}
Relevant code:
class Api::V1::ArtworksController < Api::V1::ApiController
def create
artwork = Artwork.create(artwork_params)
respond_with artwork
end
The Problem
When #create
succeeds, respond_with
chokes:
`undefined method `artwork_url' for #<Api::V1::ArtworksController:0x007fea1b4c67f8>`
It's expecting the helper for the HTTP Location to be artwork_url
. How do I tell it to use api_v1_artwork_url
instead? Can I alias the URL helper?
Upvotes: 24
Views: 3580
Reputation: 11494
In this case, you'd need to specify the namespace for the responder. Try:
respond_with :api, :v1, artwork
Upvotes: 35