Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails _paginate JSON支持吗?_Ruby On Rails_Ruby_Json_Gem_Will Paginate - Fatal编程技术网

Ruby on rails _paginate JSON支持吗?

Ruby on rails _paginate JSON支持吗?,ruby-on-rails,ruby,json,gem,will-paginate,Ruby On Rails,Ruby,Json,Gem,Will Paginate,我想知道是否有人可以告诉我will_paginate是否可以支持JSON开箱即用,或者是否必须对其进行黑客攻击 我想在will_paginate管理分页时,将页面数据添加到JSON响应中 will_paginate只是以切片方式将记录取回。所以它只是从数据库中获取一个数组 当您呈现为json时,rails将遍历对象数组并对其调用为_json 因此,是的,您可以将渲染为json。我建议您观看以下Railscast:内容: @posts = Post.paginate :page => par

我想知道是否有人可以告诉我will_paginate是否可以支持JSON开箱即用,或者是否必须对其进行黑客攻击


我想在will_paginate管理分页时,将页面数据添加到JSON响应中

will_paginate只是以切片方式将记录取回。所以它只是从数据库中获取一个数组

当您呈现为json时,rails将遍历对象数组并对其调用为_json


因此,是的,您可以将渲染为json。

我建议您观看以下Railscast:

内容:

@posts = Post.paginate :page => params[:page]

respond_to do |format|
  format.json {
    render :json => {
      :current_page => @posts.current_page,
      :per_page => @posts.per_page,
      :total_entries => @posts.total_entries,
      :entries => @posts
    }
  }
end

一旦确定了所需的格式,就可以在WillPaginate::Collection上定义一个
to_json
方法。

将分页添加到API

我找到了一个简单的解决方案,可以使用will_分页来实现API Json响应分页

首先在
ApplicationController
中创建一个类方法,该类方法将创建一个
after_过滤器,该过滤器将在响应头中设置分页元数据:

应用程序\u controller.rb


最简单的解决方案

  def index
    @posts =  Post.all.paginate(:page => params[:page], :per_page => 4)
    render :json => @posts.to_json(:methods => [:image_url])
  end
您只需添加
gem'will\u paginate'

它适合我。

这一款帮助我:

将此方法添加到基本API控制器

def pagination_dict(collection)
  {
    current_page: collection.current_page,
    next_page: collection.next_page,
    prev_page: collection.prev_page, # use collection.previous_page when using will_paginate
    total_pages: collection.total_pages,
    total_count: collection.total_count # use collection.total_entries when using will_paginate
  }
end
然后,在渲染方法上使用它

render json: posts, meta: pagination_dict(posts)

Gem:will_paginate,版本3.1.8

  def pagination_meta(collection)
    {
      current_page: collection.current_page,
      next_page: collection.next_page,
      prev_page: collection.previous_page,
      total_page: collection.total_pages,
      total_count: collection.total_entries,
    } if collection.present?
  end

我没有使用RJS,所以数据必须在JSON响应中,但我想我必须对其进行破解,以便它包括JSON响应中的总页数以及分页数据。这都是Model.paginate的响应中的内容,所以您只需要在JSON响应中查找它。我还将对此进行研究@mislav提供了我想要的示例,但是,我可以看到,仅仅将标准输出呈现为JSON也应该包含我需要的信息。是的,这就是我想要做的,现在我知道它是如何工作的,它可能通过直接呈现为JSON返回我需要的信息,但是,能够像这样更改格式是很有用的。查看GitHub API文档[1],了解在响应中编码分页的更好方法(提示:将其放在标题中)[1]:您是如何找到这些方法的?它们没有列在will_paginate wiki中。这是API的伟大分页。不要修改返回的数据,将分页信息放在标题中。超级干净的解决方案,与Angular等希望返回索引/列表/查询结果数组的人一起玩得很好。这是一个死链接。谢谢你-我删除了url-但我的剪贴效果很好。
def pagination_dict(collection)
  {
    current_page: collection.current_page,
    next_page: collection.next_page,
    prev_page: collection.prev_page, # use collection.previous_page when using will_paginate
    total_pages: collection.total_pages,
    total_count: collection.total_count # use collection.total_entries when using will_paginate
  }
end
render json: posts, meta: pagination_dict(posts)
  def pagination_meta(collection)
    {
      current_page: collection.current_page,
      next_page: collection.next_page,
      prev_page: collection.previous_page,
      total_page: collection.total_pages,
      total_count: collection.total_entries,
    } if collection.present?
  end