2012-03-16 3 views
6

잘 작동하는 게시 요청을 보내고 새 레코드를 만듭니다. 레코드가 생성 된 후에 그러나, 나는 서버 로그'create'작업을 성공적으로 실행 한 후 NoMethodError

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에서 네임 스페이스있다가, 여기에 행동 만들 내 경로가

파일 내되어이 오류가 html로 POST 요청에 대한
# 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 

답변

8

는, 성공적으로 객체를 생성 한 후, respond_with는

0처럼 뭔가에 해당하는 것 즉, 여기 객체의 경로로 리디렉션
redirect_to Device.create(params[:device]) 

이는 device_url로 리디렉션됩니다. 그러나 경로의 장치가 네패스에 있기 때문에 respond_with 호출에 해당 장치를 지정해야합니다.

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

감사합니다. –

+0

그것은 매력처럼 작동합니다! 고마워. –

+0

작동합니다. 해결책에 대해 감사드립니다. 하지만 respond_with에서 리디렉션을 사용 중지하는 방법이 있습니까? API 요청 이후로 리디렉션 동작이 필요하지 않습니다. – lionel

관련 문제