2012-02-11 11 views
0

RailsのバックエンドからJSONを消費するjavascriptフロントを設定しようとしています。Rails 3 - ネストされたモデルのJSONが期待通りに返されない

唯一の問題は、Railsにネストされたモデルがある場合、返されるJSONが正しい形式であるかどうかわかりません。

私は投稿モデルとコメントモデルを持っています。投稿には多くのコメントがあります。各モデルには、「タイトル」と「コンテンツ」フィールドがあります。私の問題は、これまでここで

はここ

1)

は私がURLにアクセスすると...です: http://localhost:3000/posts.json私が予想される出力を得る:私は置けば

> [{"content":"My first 
> post","created_at":"2012-02-07T18:56:16Z","id":1,"title":"Hello","updated_at":"2012-02-07T18:56:16Z"},{"content":"More 
> crap","created_at":"2012-02-07T21:30:51Z","id":2,"title":"My 2nd 
> Post","updated_at":"2012-02-07T21:30:51Z"}] 

2)をこのURL:http://localhost:3000/posts/1/comments/4.json私も予想されます:

> {"content":"that's 
> nice","created_at":"2012-02-07T20:57:16Z","id":4,"post_id":1,"title":"cool","updated_at":"2012-02-07T20:57:16Z"} 

3)しかし...私は、このようなURLに置く場合:http://localhost:3000/posts/1/comments.json

それはnullを返します。

投稿に関連付けられたコメントリストのJSONバージョンにアクセスすることはできません。

標準的なRailsビューを使用すると、ネストは問題なく完全にCRUDできますが、クライアント側でそれを消費するためにRailsが通常JSONを戻す方法とは異なる何かをする必要がありますか?


EDIT

私はあなたのコメントをコントローラ用のコントローラコードを参照する必要があるかもしれませんと仮定しています。ここでは、次のとおりです。

class CommentsController < ApplicationController 

    before_filter :get_post 

    respond_to :html, :xml, :json 

    def index 
    @comments = @post.comments.all  
    respond_with (@comment)  
    end 

    def show 
    @comment = @post.comments.find(params[:id]) 

    respond_with(@comment) 

    end 

    def new 
    @comment = @post.comments.build 

    respond_with(@comment) 
    end 

    def edit 
    @comment = @post.comments.find(params[:id]) 
    end 

    def create 
    @comment = @post.comments.build(params[:comment]) 

    respond_to do |format| 
     if @comment.save 
     format.html { redirect_to([@post, @comment], :notice => 'Comment was successfully created.') } 
     format.xml { render :xml => @comment, :status => :created, :location => @comment } 
     format.json { render :json => @comment, :status => :created, :location => @comment } 
     else 
     format.html { render :action => "new" } 
     format.xml { render :xml => @comment.errors, :status => :unprocessable_entity } 
     format.json { render :json => @comment.errors, :status => :unprocessable_entity } 
     end 
    end 
    end 

def update 
    @comment = @post.comments.find(params[:id]) 

    respond_to do |format| 
     if @comment.update_attributes(params[:comment]) 
     format.html { redirect_to(@comment, :notice => 'Comment was successfully updated.') } 
     format.xml { head :ok } 
     format.json { head :ok } 
     else 
     format.html { render :action => "edit" } 
     format.xml { render :xml => @comment.errors, :status => :unprocessable_entity } 
     format.json { render :json => @comment.errors, :status => :unprocessable_entity } 
     end 
    end 
    end 

    def destroy 
    @comment = @post.comments.find(params[:id]) 
    @comment.destroy 

    respond_to do |format| 
     format.html { redirect_to(post_comments_url) } 
     format.xml { head :ok } 
     format.json { head :ok } 
    end 
    end 

    protected 

    def get_post 
    @post = Post.find_by_id(params[:post_id]) 
    redirect_to root_path unless @post 
    end 

end 
+3

「respond_with(@comments)」か、それは打ち間違いですか? – lucapette

+0

うん - それだけです。 – PhillipKregg

+0

私は答えたはずです:P – lucapette

答えて

2
def index 
    @comments = @post.comments.all  
    respond_with (@comment)  
end 

あなたが@commentに割り当てませんでした。

+0

一文字の違いを驚かせてください。ありがとう。 – PhillipKregg

関連する問題