Javascript Rails 3-嵌套模型的JSON未按预期返回

Javascript Rails 3-嵌套模型的JSON未按预期返回,javascript,ruby-on-rails,ruby-on-rails-3,json,Javascript,Ruby On Rails,Ruby On Rails 3,Json,我正在尝试在中设置一个javascript前端,该前端使用来自Rails后端的JSON 唯一的问题是,当我在Rails中有嵌套模型时,我不确定返回的JSON格式是否正确 我有一个帖子模型和一个评论模型。一篇帖子有很多评论。每个模型都有一个“标题”和“内容”字段 这就是我目前的问题所在 1)当我访问url时:http://localhost:3000/posts.json我得到了预期的输出: > [{"content":"My first > post","created_at":"2

我正在尝试在中设置一个javascript前端,该前端使用来自Rails后端的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的方式不同的事情,以便在客户端使用它?


编辑

我假设您可能需要查看控制器注释的控制器代码。这是:

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
class CommentsController“comment已成功创建”。)}
format.xml{render:xml=>@comment,:status=>:created,:location=>@comment}
format.json{render:json=>@comment,:status=>:created,:location=>@comment}
其他的
format.html{render:action=>“new”}
format.xml{render:xml=>@comment.errors,:status=>:unprocessable_entity}
format.json{render:json=>@comment.errors,:status=>:unprocessable_entity}
结束
结束
结束
def更新
@comment=@post.comments.find(参数[:id])
回应待办事项|格式|
if@comment.update_属性(参数[:comment])
format.html{redirect_to(@comment,:notice=>“comment已成功更新”)}
format.xml{head:ok}
format.json{head:ok}
其他的
format.html{render:action=>“edit”}
format.xml{render:xml=>@comment.errors,:status=>:unprocessable_entity}
format.json{render:json=>@comment.errors,:status=>:unprocessable_entity}
结束
结束
结束
def销毁
@comment=@post.comments.find(参数[:id])
@毁灭
回应待办事项|格式|
format.html{redirect_to(post_comments_url)}
format.xml{head:ok}
format.json{head:ok}
结束
结束
受保护的
def get_post
@post=post.find_by_id(参数[:post_id])
将_重定向到根路径,除非@post
结束
结束

你没有分配给@comment。

应该是
回复(@comments)
或者是打字错误?一个字母可以产生惊人的差异。谢谢
def index
    @comments = @post.comments.all    
    respond_with (@comment)    
end