0

我想发送计数值,以计算我在 ROR 中使用竖起大拇指gem 的帖子。

现在我想在 json 中发送计数,投票作为 def show 中的发布操作

def index
@posts = Post.all
respond_with(@posts) do |format|
  format.json { render json: @post_names = {:post => @posts.as_json(:only=> [:content, :title]) } }

end
end 

我想使用 json 发送计数值,因为我想在客户端显示该计数值。

4

2 回答 2

3

您可以通过这种方式发送投票计数。我认为这样的事情可以工作..

def index
  @posts = Post.all
  respond_with(@posts) do |format|
    format.json { render :json => @post_names = {:post => @posts.map {|t| {:title => t.title, :content => t.content, :count => t.votes_for }} } }
  end
end
于 2013-06-17T09:11:10.093 回答
1

由于您正在使用控制器,您可能会发现查看renderRails 文档部分会有所帮助。

于 2013-06-15T05:39:47.190 回答