如何从 Hash 中分页键和值
How to paginate keys and values from Hash
想法:
我想按每页十条记录(键->值)对散列结果进行分页。
当我尝试对对象进行分页时,它显示错误:Undefined method paginates for Hash。
我可以通过这个对键进行分页:
def index
@url = @base_url+'latest.json'+@api
@response = HTTParty.get(@url)
@latest_currencies = @response['rates'].sort.to_h
@lc_keys = @latest_currencies.keys.paginate(page: params[:page], per_page: 10)
@lc_values = @latest_currencies.values.paginate(page: params[:page], per_page: 10)
end
但在视野中:
table.table.table-condensed
thead
tr
th Waluta
th Kurs
tbody
- @lc_keys.each do |k, v|
tr
td
= k
td
= v
= will_paginate @lc_keys
仅对键分页,不显示值。
如何在视图中对键和值进行分页和 return?
- @lc_keys.each do |k|
tr
td
= k
td
= @latest_currencies[k]
或
@lc = @latest_currencies.to_a.paginate(page: params[:page], per_page: 10)
- @lc.each do |k, v|
tr
td
= k
td
= v
= will_paginate @lc
想法: 我想按每页十条记录(键->值)对散列结果进行分页。 当我尝试对对象进行分页时,它显示错误:Undefined method paginates for Hash。 我可以通过这个对键进行分页:
def index
@url = @base_url+'latest.json'+@api
@response = HTTParty.get(@url)
@latest_currencies = @response['rates'].sort.to_h
@lc_keys = @latest_currencies.keys.paginate(page: params[:page], per_page: 10)
@lc_values = @latest_currencies.values.paginate(page: params[:page], per_page: 10)
end
但在视野中:
table.table.table-condensed
thead
tr
th Waluta
th Kurs
tbody
- @lc_keys.each do |k, v|
tr
td
= k
td
= v
= will_paginate @lc_keys
仅对键分页,不显示值。 如何在视图中对键和值进行分页和 return?
- @lc_keys.each do |k|
tr
td
= k
td
= @latest_currencies[k]
或
@lc = @latest_currencies.to_a.paginate(page: params[:page], per_page: 10)
- @lc.each do |k, v|
tr
td
= k
td
= v
= will_paginate @lc