Я нахожусь в какой-то странной ситуации, когда получаю странную ошибку с вложенными ресурсами.
У меня есть вложенный ресурс, определенный ниже:
resources :users do
resources :comments, :only => [:create, :destroy]
end
Моя конечная точка для комментариев - только json, поэтому ее контроллер определяется следующим образом. Обратите внимание, что я использую камни cancan и actsAsApi.
class CommentsController < ApplicationController
load_and_authorize_resource
self.responder = ActsAsApi::Responder
respond_to :json
# POST /comments.json
def create
flash[:notice] = 'Comment was successfully created.' if @comment.save
respond_with(@comment, :api_template => :default)
end
# DELETE /comments/1.json
def destroy
@comment.destroy
respond_with(@comment, :api_template => :default)
end
Затем я могу отправить почтовый запрос на /users/1/comments.json с некоторыми параметрами запроса, и комментарий будет создан, как и ожидалось. К сожалению, я получаю сообщение об ошибке при попытке найти действие уничтожения:
Completed 404 Not Found in 169ms
ActionController::RoutingError (No route matches {:action=>"destroy", :controller=>"comments", :id=>#<Comment id: 34, user_id: 1, text: "test test test", created_at: "2012-02-28 06:45:49", updated_at: "2012-02-28 06:45:49">}):
app/controllers/comments_controller.rb:12:in `create'
В качестве дополнительной информации, если я изменю route.rb на это:
resources :comments, :only => [:destroy]
resources :users do
resources :comments, :only => [:create]
end
Я не вижу ошибок.