How to get per_page model number in views?

The number of elements for a page in Kaminari is determined by:

  • definition of the per_page element in the model or
  • definition of default_per_page in config.

My question is how to get this number of elements in view?

Update

Excuse me for not knowing. What I want is to show the order of the elements on each page. I have a table title:

<th>#</th> 

And a few columns below:

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

My question is: should some_ruby_code be?

1) If I replaced some_ruby_code with Sale.per_page , it will Sale.per_page error when I decided to remove per_page .

2) If I replace some_ruby_code with Kaminari.config.default_per_page , it will not show the correct value when I add per_page to the model.

I want to know if a method exists in Kaminari that detects the existence of per_page , returns its value if it exists, or Kaminari.config.default_per_page otherwise.

Thanks!

+4
source share
4 answers

You can use the ActiveRecord limit_value method for limit_value , since Kaminari internally uses restriction and offset.

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

With kaminari one and the same:

 User.page(3).per(10).limit_value => 10 User.page(3).per(10).offset_value => 20 
+3
source

I think you want to display the number of elements visible from the total,

if you can use

 <%= page_entries_info @items %> 

Read this github documentation:

https://github.com/amatsuda/kaminari

+1
source

Assuming you have elements for this page that appear in the view, this should work:

@items.length

0
source

Source: https://habr.com/ru/post/1488956/


All Articles