从RABL中提取分页节点

时间:2014-07-30 00:43:25

标签: ruby-on-rails pagination rabl

我使用的是Rails 3.2.18和RABL 0.10.1。

现在,我的应用程序中有许多索引操作,包括一些分页信息以及集合。

这是渲染的RABL响应的示例:

{
  "current_page" => 1,
  "total_pages"  => 1,
  "total_count"  => 2,
  "per_page"     => 1000,
  "links"        => [ {"id" => ...}, {"id" => ...}]
}

我的config/initializers/rabl.rb看起来像是:

# config/initializers/rabl.rb
Rabl.configure do |config|
  config.include_child_root = false
end

以上示例响应的索引视图如下所示:

# app/views/links/index.rabl
object false

node(:current_page) { @links.current_page }
node(:total_pages)  { @links.num_pages }
node(:total_count)  { @links.total_count }
node(:per_page)     { @links.limit_value }

child(@links => :links) do
  extends 'links/show'
end

我为许多索引操作构建了current_pagetotal_pagetotal_countper_page个节点。我想减少重复。

我希望我可以使用偏爱来制作这样的东西:

# app/views/shared/pagination.rabl
node(:current_page) {|o| o.current_page }
node(:total_pages)  {|o| o.num_pages }
node(:total_count)  {|o| o.total_count }
node(:per_page)     {|o| o.limit_value }

# app/views/links/index.rabl
object false

partial 'shared/pagination', object: @links

child(@links => :links) do
  extends 'links/show'
end

但是这最终结束了错误:

ActionView::Template::Error:
       undefined method `current_page' for #<Link:0x007f92387dbc58>

我已经阅读了RABL文档,并根据我在那里阅读的内容尝试了其他一些方法,但我仍然无法弄清楚如何减少重复并仍然保留原始输出。

有没有人知道如何做到这一点?

感谢您的帮助,我很乐意提供更多有用的信息。

0 个答案:

没有答案