I'm consuming an external API, and I would like to create some sort of pagination for the index action. The API returns 15 elements (which is the limit I specified in the HttParty query hash) and I can add the "page" option too. For starters, I tried putting an incremental in the view and passing it as a param, and it works but only for one page (ex. from page 1 to 2) and then it just reloads the second page. How could I achieve my goal? I know there are some gems out there but I would like to learn it myself. Here's the code:
controller
class PropertiesController < ApplicationController
BASE_URL='https://api.stagingeb.com/v1/properties'
HEADERS = {
"X-Authorization" => ENV['API_KEY']
}
def index
page = params[:page]
query = {
"limit" => "15",
"page" => page || 1
}
request = HTTParty.get(BASE_URL, :query => query, :headers => HEADERS)
@response = JSON.parse(request.body)
end
def show
request = HTTParty.get(BASE_URL + "/#{params[:id]}", :headers => HEADERS)
@response = JSON.parse(request.body)
end
end
view
<h1 class="index-heading">Listing all properties</h1>
<div class="main-wrapper">
<%@response["content"].each do |property|%>
<div class="property-card">
<div class="image">
<%if property["title_image_thumb"]%>
<%=image_tag property["title_image_thumb"]%>
<%else%>
<p class="noimage"><i class="fas fa-exclamation-triangle"></i> No image</p>
<%end%>
</div>
<div class="card-text">
<p class="public-id"><%=property["public_id"]%></p>
<p class="title"><%=property["title"]%></p>
<p class="type"><%=property["property_type"]%></p>
<p class="type"><i class="fas fa-map-marker-alt"></i> <%=property["location"]%></p>
</div>
<div class="property-link">
<%=link_to 'Go to property', "properties/#{property["public_id"]}"%>
</div>
</div>
<%end%>
</div>
<div class="pagination">
<%i = 1%>
<%=link_to 'Next page', root_path(:page => i+=1)>
</div>
PD: My goal is to add two buttons, one for the previous page and one for the next page