Ruby on rails 3 成功运行后的NoMethodError';创建';行动
我正在向我的API发送一个post请求,该请求运行良好,它创建了一个新记录。但是,在创建记录之后,我在服务器日志中得到了这个错误Ruby on rails 3 成功运行后的NoMethodError';创建';行动,ruby-on-rails-3,nomethoderror,Ruby On Rails 3,Nomethoderror,我正在向我的API发送一个post请求,该请求运行良好,它创建了一个新记录。但是,在创建记录之后,我在服务器日志中得到了这个错误 NoMethodError (undefined method `device_url' for #<Api::V1::DevicesController:0x007fa2f5dde5d8>):app/controllers/api/v1/devices_controller.rb:14:in `create' 我所有的资源都在Api#V1下命名,这是我
NoMethodError (undefined method `device_url' for #<Api::V1::DevicesController:0x007fa2f5dde5d8>):app/controllers/api/v1/devices_controller.rb:14:in `create'
我所有的资源都在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
对于HTML POST请求,在成功创建对象后,通过重定向到对象路径来响应_,即,在这里,它相当于
redirect_to Device.create(params[:device])
它重定向到设备的url。但是,由于您在路由中有名称空间:设备,您需要将其指定给respond_with call,这将是
respond_with :api, :v1, Device.create(params[:device])
这很有效,谢谢你的解决方案。但是,在respond_中是否仍然可以禁用重定向?因为这是一个api post请求,所以我们实际上不需要重定向行为。
respond_with :api, :v1, Device.create(params[:device])