Rails 3 Responder: undefined method `list_url'
mrrooijen opened this issue · 1 comments
When nesting routes like so:
namespace :api do
namespace :v1 do
resources :items
resources :lists
end
end
and having a controller like so:
class Api::V1::ListsController < ApiController
self.responder = ActsAsApi::Responder
respond_to :json
def create
respond_with List.create(params[:list]), :api_template => :public
end
end
On successful creation it'll error out with a 500 internal server error because of the following exception:
NoMethodError (undefined method `list_url' for #<Api::V1::ListsController:0x007fa3518e3f68>):
app/controllers/api/v1/lists_controller.rb:20:in `create'
When I add the following to my routes, it fixed it, but is not desired.
resources :lists
This of course generates the list_url() method, but what does a JSON responder need it for, and why doesn't it pick the nested version if it really needs the list_url
(e.g. api_v1_list_url
)?
Any idea how to fix this without generating a root-level route for the List resource?
Thanks!
As far as I can see this is not caused by acts_as_api but by ActionController::Responder
.
The error occurs, because the responder tries to set the Location HTTP Header on create, but this fails on nested routes.
Another example of this problem without acts_as_api can be found here: rails/rails#2798
So the solution is:
Add the location options, when rendering the response of a #create action
@list = List.create(params[:list])
respond_with @list, :api_template => :public, :location => api_v1_list_url(@list)
Hope this helps!