【发布时间】:2020-05-06 16:34:10
【问题描述】:
我正在使用 Sinatra 和 a Hackernews API 我一直在为分页而苦苦挣扎,这个 API 不是用查询参数来做的
因此,对于站点的信息(第 1 页),端点将是这个 https://api.hnpwa.com/v0/news/1.json 和第 2 页是这个https://api.hnpwa.com/v0/news/2.json
我想从 .erb 文件中发送参数来修改它,但我无法
app.rb
helpers do
def get_page(site, page)
"https://api.hnpwa.com/v0/#{site}/#{page}.json"
end
end
get '/' do
@page = 1
endpoint = get_page("news", @page)
@stories = JSON.parse(HTTP.get(endpoint).to_s)
erb :news
end
news.erb
<table>
<tbody>
<% @stories.each_with_index do |story, index| %>
<tr>
<td class="post">
<div>
<a class="index" href="#"><%= index + 1 %>.</a>
<a href="<%= story['url'] %>" class="post-title"><%= story['title'] %></a>
<span><a class="url" href="<%= story['url'] %>">(<span><%= story['url'].nil? ? '' : story['url'].split('/')[2] %></span>)</a></span>
</div>
<div class="post-details">
<p><%= story['points'] %> points by <%= story['user'] %></p>
<p><%= time_since_in_words(story['time']) %> ago</p>
<p>| hide |</p>
<p><%= story['comments_count'] %> comments</p>
</div>
</td>
</tr>
<%end%>
</tbody>
</table>
<a class="more" href=<% @page+=1%>>More</p>
我对 Sinatra 和 Ruby 真的很陌生,所以我不知道还能做什么。感谢您的帮助,谢谢!
【问题讨论】: