6

正常に動作する API にポスト リクエストを送信しています。新しいレコードが作成されます。ただし、レコードが作成された後、サーバーログにこのエラーが表示されます

NoMethodError (undefined method `device_url' for #<Api::V1::DevicesController:0x007fa2f5dde5d8>):app/controllers/api/v1/devices_controller.rb:14:in `create'

これは私の作成アクションです

def create
    respond_with Device.create(params[:device])
end

すべてのリソースは Api#V1 の下に名前空間が設定されています。これがルート ファイルです。

# Api ('/api')
namespace :api do
    # Version 1 ('/api/v1')
    namespace :v1 do
        # Locations ('/api/v1/locations')
        resources :locations
        # Videos ('/api/v1/videos')
        resources :videos
        # Devices ('/api/v1/devices')
        resources :devices
    end
end
4

1 に答える 1

8

HTML POST リクエストの場合、オブジェクトの作成に成功した後、respond_with はオブジェクトのパスにリダイレクトします。つまり、ここでは次のようなものと同等です。

redirect_to Device.create(params[:device])

device_url にリダイレクトします。しかし、ルートで :devices に名前空間を設定したため、respond_with 呼び出しにそれを指定する必要があります。これは次のようになります。

respond_with :api, :v1, Device.create(params[:device])
于 2012-03-17T07:59:11.010 に答える