Ich habe ein Artwork
Modell, das nur von API-Endpunkten jetzt manipuliert wird. (Sie werden sehen, warum das in Kürze wichtig ist). Diese API-Endpunkte sind wie so in meiner routes.rb
Datei deklariert:Rails 3: respond_with Fehler auf undefinierten URL Helfer
namespace :api do
namespace :v1, :defaults => { :format => :json } do
resources :artworks, :only => [:create, :destroy, :index, :show, :update]
Daraus ergeben sich die folgenden Routen:
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}
Relevante Code:
class Api::V1::ArtworksController < Api::V1::ApiController
def create
artwork = Artwork.create(artwork_params)
respond_with artwork
end
Das Problem
Wenn #create
gelingt, respond_with
Drosseln:
`undefined method `artwork_url' for #<Api::V1::ArtworksController:0x007fea1b4c67f8>`
Sie den Helfer erwarten für die HTTP-Location artwork_url
zu sein. Wie kann ich es stattdessen api_v1_artwork_url
verwenden? Kann ich den URL-Helper aliasieren?
gerade dieses Problem auftritt, und ich bin ein wenig verwirrt darüber, warum wir den Namespace hinzufügen müssen .. kann es nicht auf der offiziellen Dokumentation (oder vielleicht übersehen ich es) – giosakti
Sie wollen lesen Sie über polymorphe Routen: http://ryanbigg.com/2012/03/polymorphic-routes/, http://api.rubyonrails.org/classes/ActionDispatch/Routing/PolymorphicRoutes.html – rossta
Funktioniert nicht. bekommt nur "undefinierte Methode' api_RESOURCE_url 'für # " – ajbraus