NoMethodError after running successful 'create' action
Asked Answered
K

1

6

I am sending a post request to my API which works fine, it creates a new record. However, after the record is created 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 create action

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

All my resources are namespaced under Api#V1, here is my routes 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
Kutzenco answered 16/3, 2012 at 22:37 Comment(0)
A
8

For a HTML POST request, after successfully creating the object, respond_with redirects to the object's path, i.e. here it would be equivalent to something like

redirect_to Device.create(params[:device])

which redirects to device_url. But since you have namespaced :devices in your routes, you need to specify that to your respond_with call, which would be

respond_with :api, :v1, Device.create(params[:device])
Akee answered 17/3, 2012 at 7:59 Comment(1)
it works, thanks for the solution. But is there anyway to disable to redirect in the respond_with? since it's a api post request, we don't really need the redirect behavior.Static

© 2022 - 2024 — McMap. All rights reserved.