encode & decode URL params in rails api pagination

696 views Asked by At

In my rails-api app, a user can request for products index by pagination.

So I want to provide next and previous url via paging hash as

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

The above data i.e. paging hash is sent as www.url.com?page=2\u0026per_page=5

Hence, when this request comes, page and per_page params aren't recognised.

How do I resolve this??

1

There are 1 answers

0
rAzOr On

The problem here was render json: result.

json method was converting the & symbol. So I had to add following line in application.rb file

config.active_support.escape_html_entities_in_json = false

Found the solution here.