如何从Hash中分页键和值

时间:2017-05-21 08:26:45

标签: ruby-on-rails ruby hash pagination will-paginate

思想: 我想用每页十个记录(键 - >值)对散列结果进行分页。 当我尝试对对象进行分页时,它会显示错误: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

仅分页键而不显示值。 如何对键和值进行分页并在视图中返回?

1 个答案:

答案 0 :(得分:2)

- @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