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 回答