How to obtain per_page number of a Model in views?
Asked Answered
A

5

5

The number of items for a page in Kaminari is defined by:

  1. defining per_page member in model, or
  2. defining default_per_page in config.

My question is, how to obtain that number of items in view?

Update

Pardon me for being not clear. What I want is to show is the order of item in each page. I have a table header:

<th>#</th>

And several columns below:

<td><%= (@sales.current_page - 1) * some_ruby_code + index + 1 %></td>

My question is what some_ruby_code should be?

1) If I substitute some_ruby_code with Sale.per_page, it will throw error when I decided to delete per_page.

2) If I substitute some_ruby_code with Kaminari.config.default_per_page, it won't show the proper value when I add per_page in model.

I want to know whether there are a method in Kaminari which detects the existence of per_page, return its value if exists, or Kaminari.config.default_per_page otherwise.

Thank you!

Argyle answered 30/6, 2013 at 14:27 Comment(0)
E
4
M
3

You can use ActiveRecord's limit_value method for this, since Kaminari internally uses limit and offset.

User.all.limit(10).limit_value
=> 10

With kaminari is the same:

User.page(3).per(10).limit_value
=> 10

User.page(3).per(10).offset_value
=> 20
Meingolda answered 22/6, 2015 at 21:8 Comment(0)
D
1

I guess, you want to display the number of items visible out of the total,

if so you can use

<%= page_entries_info @items %>

Read this documentation on github:

https://github.com/amatsuda/kaminari

Dogy answered 30/6, 2013 at 17:21 Comment(1)
Sorry, I'm afraid this is not the answer I need. I have updated my question.Argyle
O
0

Assuming you actually have the items for that page being rendered in the view, this should work:

@items.length

Occurrence answered 30/6, 2013 at 15:54 Comment(1)
Sorry, I'm afraid this is not the answer I need. I have updated my question.Argyle
G
-1

current_per_page returns of given list of paged records.

Gigolo answered 24/3, 2019 at 12:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.