Rails 3-嵌套模型的JSON没有按预期返回
Rails 3 - JSON of nested models not returning as expected
我正试图在中设置一个javascript前端,该前端使用Rails后端的JSON。
唯一的问题是,当我在Rails中嵌套模型时,我不确定返回的JSON格式是否正确。
我有一个Post模型和一个Comment模式。一篇帖子有很多评论。每个模型都有一个"title"answers"content"字段。
到目前为止,我的问题就在这里。。。
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,这没有问题,但为了在客户端使用JSON,我是否需要做一些与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
def index
@comments = @post.comments.all
respond_with (@comment)
end
你没有分配给@comment。
相关文章:
- Jquery:对返回JSON数据的php脚本的Ajax调用
- 返回JSON中的下标
- jQuery Map 从多个源返回 JSON 对象
- 如何在 Node.js 中返回 JSON 后停止代码流
- AngularJS:表单提交没有返回JSON中的值
- 使用ajax发送数据,返回json
- 从我的解析函数返回json
- 如何从脚本页面返回json数据
- 有些人有任何关于如何使用Modified Java Script Value处理返回Json(url)的示例
- 如何在html页面中从ajax返回json数据
- Ajax调用在本地返回json,但在服务器上运行时返回xml
- ajax调用返回JSON ParseError即使认为JSON看起来是正确的
- 调用.ajax方法得到未定义的json结果,返回json格式列表<字符串>
- 如何通过C#返回JSON并用JavaScript进行解析
- 使用 AJAX 返回 json 数组
- 从 AJAX 命令返回 JSON 格式的响应
- 如何通过node中的请求返回JSON.js(从javascript)
- Node.js API 在终端中返回 JSON,但不在浏览器中返回 JSON
- 如何在支柱 2 中返回 JSON 数组
- Rhino:从 Java 中返回 JSON