【问题标题】:encode & decode URL params in rails api pagination在rails api分页中编码和解码URL参数
【发布时间】:2016-12-21 18:11:36
【问题描述】:

在我的 rails-api 应用程序中,用户可以通过分页请求产品索引。

所以我想通过分页哈希提供下一个和上一个 url 作为

result = @products.paginate(:page => params[:page] || 1, :per_page => params[:per_page] || 5)

paging {
    :next => www.url.com?page=next&per_page=5,
    :prev => www.url.com?page=prev&per_page=5,
}

result << paging
render json: result

上述数据即分页哈希发送为www.url.com?page=2\u0026per_page=5

因此,当此请求到来时,无法识别 page 和 per_page 参数。

我该如何解决这个问题??

【问题讨论】:

    标签: ruby-on-rails url encoding decoding url-parameters


    【解决方案1】:

    这里的问题是render json: result

    json method 正在转换 &amp; 符号。所以我不得不在application.rb 文件中添加以下行

    config.active_support.escape_html_entities_in_json = false
    

    找到解决方案here

    【讨论】:

      猜你喜欢
      • 2017-07-26
      • 2019-06-30
      • 2018-10-18
      • 1970-01-01
      • 2011-05-30
      • 1970-01-01
      • 2021-11-30
      • 2016-01-13
      • 1970-01-01
      相关资源
      最近更新 更多