NoMethodError after successful create action

I am sending a mail request to my API, which works fine, it creates a new entry. However, after creating the record, I get this error in the server log

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

This is my action to create

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

All my resources are named under Api # V1, here is my route file

 # 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 
+6
source share
1 answer

For an HTML POST request, after successfully creating the object, response_with redirects the path to the object, i.e. here it will be equivalent to something like

 redirect_to Device.create(params[:device]) 

which is redirected to device_url. But since you have namespaced: devices on your routes, you need to point this to your response_with call, which will

 respond_with :api, :v1, Device.create(params[:device]) 
+8
source

Source: https://habr.com/ru/post/910956/


All Articles