我想在rails web app中实现以下功能:
在我的图片中,1 = @ article.first,2 = @ article.offset(1).first
例如:
from xmlsoccer import XmlSoccer
from straightred.models import StraightredFixtureLive
@csrf_exempt
def updatefixtureslive(request, soccerseason, league):
if request.user.is_authenticated():
xmlsoccer = XmlSoccer(api_key='XYZ123ABC789', use_demo=False)
fixtureslive = xmlsoccer.call_api(method='GetLiveScoreByLeague',
league='English Premier League')
count = 0
for fixturelive in fixtureslive:
if 'Id' in fixturelive.keys():
count = count + 1
fixtureLiveUpdate = StraightredFixtureLive(fixtureid_id=fixturelive['Id'],
away_team_id = fixturelive['AwayTeam_Id'],
home_team_id = fixturelive['HomeTeam_Id'],
fixturedate = fixturelive['Date'],
fixturestatus = fixturelive['Time'],
fixturematchday_id = fixturelive['Round'],
spectators = fixturelive['Spectators'],
hometeamscore = fixturelive['HomeGoals'],
awayteamscore = fixturelive['AwayGoals'],
homegoaldetails = fixturelive['HomeGoalDetails'],
awaygoaldetails = fixturelive['AwayGoalDetails'],
hometeamyellowcarddetails = fixturelive['HomeTeamYellowCardDetails'],
awayteamyellowcarddetails = fixturelive['AwayTeamYellowCardDetails'],
hometeamredcarddetails = fixturelive['HomeTeamRedCardDetails'],
awayteamredcarddetails = fixturelive['AwayTeamRedCardDetails']
)
fixtureLiveUpdate.save()
return HttpResponse("Live games have been updated." + str(count))
else:
return HttpResponse("You must be logged in to update teams.")
现在我应该如何在视图中实现此布局?
答案 0 :(得分:1)
will_paginate
(或任何分页)的使用(或不使用)与您如何放置视觉元素无关 - 这是MVC架构的全部要点。
对paginate
的调用可确保您一次获得一页16个结果,页面编号由您的?page=
参数控制。
元素通常是一个接一个地布局,这导致使用.each
:
@articles.each do |article|
render 'articles/article', :article => article
end
在您的情况下,您可能需要逐个访问集合中的文章,甚至索引。 ActiveRecord::Relation
个集合支持类似数组的访问,因此要获取16个元素中的第一个,您可以使用articles[0]
,第二个位于articles[1]
,一直到{16} articles[15]
}。