【发布时间】:2014-11-13 19:45:18
【问题描述】:
我一直在尝试将自定义属性添加到 jbuilder,就像我在显示页面中所做的那样到我的索引页面进行分页,将分页并且它不显示自定义属性。
例如,我的控制器操作中的内容是
def index
#respond_with
@publishers = Publisher.paginate(:page => params[:page], :per_page => 30)
respond_to do |format|
format.json
end
end
而我的 index.json.jbuilder 是
json.array!(@publishers) do |publisher|
json.extract! publisher, :id, :name, :url
json.categories do
publisher.categories.each do |category|
json.name category.name
json.id category.id
json.url url_for(category)
end
end
end
我想要的是
json.current_page @publishers.current_page
json.total_pages @publishers.totla_entries
json.array!(@publishers) do |publisher|
json.extract! publisher, :id, :name, :url
json.categories do
publisher.categories.each do |category|
json.name category.name
json.id category.id
json.url url_for(category)
end
end
end
这样我就可以在索引页的 json 输出中显示 current_page 和总页数。
目前我拥有的是
[{"id":1,"name":"facebook","url":"http://www.facebook.com","categories":{"name":"Art and Crafts","id":1,"url":"/categories/1-art-and-crafts"}}]
我怎样才能做到这一点。我也在使用 willpaginate
【问题讨论】:
标签: ruby-on-rails json ruby-on-rails-4 jbuilder