I want to achieve the following in a rails web app:
In my image, 1 = @article.first, 2 = @article.offset(1).first
So for example:
ArticlesController
def index
@articles = Article.all.paginate(page: params[:page], per_page: 16)
end
Now how am I supposed to achieve this layout in the view?
The use (or not) of will_paginate
(or any pagination, for that matter) is irrelevant to how you lay your visual elements out - that's the entire point of the MVC architecture.
The call to paginate
makes sure you get one page of 16 results at a time, with the page number controlled by the ?page=
parameter in your case.
Elements are often laid out one after the other, which results in a formation using .each
:
@articles.each do |article|
render 'articles/article', :article => article
end
In your case, you may need to access the articles in the collection one by one, or even by index. ActiveRecord::Relation
collections support array-like access, so to get the first of the sixteen elements you can use articles[0]
, the second at articles[1]
, all the way to the sixteenth at articles[15]
.