I'm new to Ruby, and my task is to add pagination for the get method, but I try to test if the params (page) and(per_page) exist, if they dont exist it should return all the data in the database, because by defaulkt kaminari return only the 25 page.
this is my function
def index
if page.present? && per_page.present?
@domains = Domain.accessible_by(access_token).page(page).per(per_page)
else
@domain = Domain.accessible_by(access_token).all
end
render json: @domain
end
at it return a 200 OK
response but null
Controller method receives request parameters via params
object. Variables page
and per_page
are undefined which will throw an exception undefined local variable, etc.
.
A few more things:
@domain
variable is a misprint and @domains
should be used instead.if
fork return a value assigning to @domains
variable, it can be simplified.To sum up, here is improved controller method:
def index
@domains = if params[:page].present? && params[:per_page].present?
Domain.accessible_by(access_token)
.page(params[:page])
.per(params[:per_page])
else
Domain.accessible_by(access_token).all
end
render json: @domains
end