2012-02-28 7 views
0

ネストしたリソースで奇妙なエラーが発生するような気がします。コメントのネストされたリソースと破棄するRoutingError

resources :users do 
    resources :comments, :only => [:create, :destroy] 
end 

私のエンドポイントは、次のようにだけので、そのコントローラーが定義されているJSONです:

私は、ネストされたリソースは以下のように定義されています。私はcancanとactsAsApiの宝石を使用しています。期待されるように、私はその後、いくつかのリクエストパラメータとコメントで「/users/1/comments.json」にPOSTリクエストを送信することができます

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 

が作成されます。私はこれにroutes.rbをを変更した場合、追加の情報として

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' 

:残念ながら、私はそれを破壊するアクションを検索しようと、エラーを取得しています

resources :comments, :only => [:destroy] 
resources :users do 
    resources :comments, :only => [:create] 
end 

私はすべてのエラーが表示されません。

答えて

0

で詳細を参照してください:次のように

を参照してください。基本的に、リソースをネストするときは、respond_withを次のように使用する必要があります。

respond_with(@comment.note, @comment, :api_template => :default) 
0

ネストされたリソースを使用しているため、作業するコメントに対するアクションのためにユーザーとコメントの両方を読み込むようにcancanに指示する必要があります。

class CommentsController < ApplicationController 
    load_and_authorize_resource :user 
    load_and_authorize_resource :comment, :through => :user 
end 

が、私はこれを理解することができましたcancan nested resource page

関連する問題