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

标签 javascript ruby-on-rails ruby-on-rails-3 json

我正在尝试设置一个 JavaScript 前端,从 Rails 后端使用 JSON。

唯一的问题是,当我在 Rails 中嵌套模型时,我不确定返回的 JSON 格式是否正确。

我有一个帖子模型和一个评论模型。一个帖子有很多评论。每个模型都有一个“标题”和“内容”字段。

这就是到目前为止我的问题所在......

1) 当我访问网址: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) 如果我输入此网址: 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) 但是...如果我输入这样的网址:http://localhost:3000/posts/1/comments.json

它返回null

我无权访问与该帖子关联的评论列表的 JSON 版本。

使用标准 Rails View ,我可以通过嵌套执行完整的 CRUD,没有问题,但是我是否需要做一些与 Rails 通常传回 JSON 的方式不同的操作,以便在客户端使用它?

<小时/>

编辑

我假设您可能需要查看评论 Controller 的 Controller 代码。这是:

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。

关于javascript - Rails 3 - 嵌套模型的 JSON 未按预期返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9238242/

相关文章:

javascript - AJAX:获取加载的 html 文档的元数据,例如元描述

ruby-on-rails - 使用作为方法的值在 ruby​​ 中搜索哈希

ruby-on-rails - 找出在 Ruby/Rails 中定义方法的位置(相对于 Java)

ruby-on-rails - 在不实例化类的情况下调用ruby方法

mysql - 轨道 3 : Search in a query result

ruby-on-rails - 安装 rails 时找不到文件 'lib'

javascript - 使用 PHP 中的 cURL 抓取源代码时出现的问题

javascript - Mootools:向导插件

ruby-on-rails - 需要在特定时间运行作业

Javascript:如何查找在长段落中选择了特定文本的哪个实例(例如,在段落中多次出现的单词)?