2012-09-28 9 views
24

Ho un modello Artwork che è manipolato solo dagli endpoint API al momento. (Vedrai perché questo è importante a breve). Tali endpoint API sono dichiarate in questo modo nel mio file routes.rb:Rails 3: respond_with errori sull'helper URL non definito

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

Ciò si traduce nei seguenti percorsi:

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} 

codice rilevante:

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

    respond_with artwork 
    end 

Il problema

Quando #create riesce, respond_with soffoca:

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

Si aspetta il supporto per il HTTP posizione di essere artwork_url. Come posso dire di usare invece api_v1_artwork_url? Posso alias l'helper dell'URL?

risposta

35

In questo caso, è necessario specificare lo spazio dei nomi per il risponditore. Prova:

respond_with :api, :v1, artwork 
+1

solo verificare questo problema e io sono un po 'confuso sul perché dobbiamo aggiungere lo spazio dei nomi .. non riesco a trovare su documentazione ufficiale (o forse ho trascurato di esso) – giosakti

+0

Vorrete leggi i percorsi polimorfi: http://ryanbigg.com/2012/03/polymorphic-routes/, http://api.rubyonrails.org/classes/ActionDispatch/Routing/PolymorphicRoutes.html – rossta

+0

Non funziona. si ottiene "metodo non definito' api_RESOURCE_url 'per # " – ajbraus

Problemi correlati