标签: kaminari

如何使用 Kaminari(或 will_paginate)gem 对数组散列进行分页

我现在设法找到了解决方法。index 操作现在在调用 page 之前有一个“order”子句,然后按日期对膳食进行排序和分组。接下来的 hackey 位:@total_pages 和 @pages 在视图中用于提供分页链接,因为它的内置帮助器不适用于 @meals 返回的数组散列。它工作(有点)但它是hackey。将视图内容移动到助手中肯定会有助于清理它,这是我的下一步。

def index
  @meals = Meal.order('date DESC').page(params[:page]).text_search(params[:query]).sort_by(&:date).reverse.group_by(&:date)
  @total_pages = (Meal.all.size / 7.to_f).ceil
  @pages = (1..@total_pages).to_a 
respond_to do |format|
  format.html # index.html.erb
  format.json { render json: @meals }
end
Run Code Online (Sandbox Code Playgroud)

结尾

我真的需要这方面的帮助 - 这让我疯狂了几天。我正在尝试对数组的散列进行分页。我已经尝试过 will_paginate 和 Kaminari,但找不到对结果散列进行分页的方法。

我正在运行 Rails 3.2.6,Ruby 1.9.3 和 PostgreSQL 9.1.3 正在做搜索......

在我的控制器(并使用 Kaminari)中,我有:

def index
  @meals = Meal.text_search(params[:query]).sort_by(&:date).reverse.group_by(&:date).page(params[:page])
respond_to do |format|
  format.html # index.html.erb
  format.json { render json: @meals }
  end
end
Run Code Online (Sandbox Code Playgroud)

在我的模型中,我有以下搜索:

def self.text_search(query)
  if query.present?
    where("description …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails will-paginate kaminari

5
推荐指数
1
解决办法
3840
查看次数

有没有办法修复Kaminari分页产生的网址?

我正在为我的应用程序实现一个聊天系统,其中只有注释列表将使用ajax submit重新加载.

在那里使用Kaminari分页,但是在提交新评论之后,它会在其URL中获得奇怪的字符串.

example-website.com/users/mike/refresh_part?_=1356906069855&page=2

当在其他控制器中提交注释时,它在其url中获得更奇怪的参数参数.只有当它与垃圾邮件错误相关时才 生成url:

example-website.com/shop/walmart/topic/20/comments?authenticity_token=9nUyEQ%2Fa0F114vUe16RXf7jhsPw%2B736E%2BKyZFjiWbkQ%3D&comment[body]=test&commit=Create+Comment&page=2&utf8=✓

我怎样才能解决这个问题?

我的代码是

意见/用户/ show.html.erb

<%= javascript_tag do %>
    jQuery(document).ready(function () {
        refreshPartial();
        setInterval(refreshPartial, 5000)
    });


    function refreshPartial() {
      $.ajax({
        url: "<%= show_user_path(@user) %>/refresh_part",
        type: "GET",
        dataType: "script",
      });
    }
<% end %>
......
<span id="chat">
<%= render 'users/comment' %>
</span>
<%= render 'users/comment_input' %>
Run Code Online (Sandbox Code Playgroud)

意见/用户/ _comment.html.erb

<table>
  <tr>
    <th>ID</th>
    <th>PIC</th>
    <th>Body</th>
    <th>Subject</th>
    <th>Posted by</th>
    <th>Delete</th>
  </tr>

<% @comments.each do |comment| %>
  <tr id="<%= dom_id(comment) %>">
    <td><%= comment.id %></td>
    <td> …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails ruby-on-rails-3 kaminari

5
推荐指数
1
解决办法
5487
查看次数

是否可以在与Kaminari的链接中添加锚点/参数?

我正在使用Kaminari的分页.它的工作非常棒.

我现在想要的一件事是,加上#comment_sectionKaminari产生的网址.

例如,我的观点就是这样.通过单击Kaminari生成的链接加载页面时,我希望它转到本节的顶部.

可能吗?

<a name="comment_section">
<span id="comment">
 <%= render 'users/comment' %>
</span>
<%= paginate @comments, :window => 4 %>
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails ruby-on-rails-3 kaminari

5
推荐指数
1
解决办法
3238
查看次数

如何在视图中获取模型的per_page编号?

Kaminari中页面的项目数由以下内容定义:

  1. 在模型中定义per_page成员,或
  2. 在config中定义default_per_page.

我的问题是,如何获得该数量的项目?

更新

请原谅我不清楚.我想要的是显示每个页面中项目的顺序.我有一个表头:

<th>#</th>
Run Code Online (Sandbox Code Playgroud)

以下几栏:

<td><%= (@sales.current_page - 1) * some_ruby_code + index + 1 %></td>
Run Code Online (Sandbox Code Playgroud)

我的问题是some_ruby_code应该是什么?

1)如果我代替some_ruby_codeSale.per_page,它会抛出错误,当我决定删除per_page.

2)如果我代替some_ruby_codeKaminari.config.default_per_page,也不会当我添加显示正确的价值per_page模型.

我想知道Kaminari中是否有检测存在的方法per_page,如果存在则返回其值,Kaminari.config.default_per_page否则.

谢谢!

ruby-on-rails kaminari

5
推荐指数
1
解决办法
1708
查看次数

jquery_ujs与Rails 4中的Kaminari AJAX冲突?

精简版; jquery_ujs似乎与Kaminari的AJAX支持冲突,我不知道为什么.

在我的Rails 4应用程序中,我在application.js中有以下几行

//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require bootstrap
//= require_tree .
Run Code Online (Sandbox Code Playgroud)

如果我删除jquery_ujs,则以下代码停止工作; 它开始发送GET请求而不是DELETE请求,并且用户在发送GET请求时只接收资源的显示页面.

<%= link_to 'Delete Horse', horse, method: :delete, data: { confirm: 'Are you sure?' } %>
Run Code Online (Sandbox Code Playgroud)

但如果我离开jquery_ujs,Kaminari w/AJAX就会停止工作......

<%= paginate @horses, :remote => true %>
Run Code Online (Sandbox Code Playgroud)

单击时无处可寻(尽管呈现的HTML中的HREF标记是正确的).

如果我从paginate @horses链接中删除:remote => true,则链接开始工作.但是,A)我希望AJAX能够为用户体验而工作,而B)我想理解为什么会发生这种情况.

javascript ajax jquery ruby-on-rails kaminari

5
推荐指数
1
解决办法
667
查看次数

如果下一页存在,Kaminari?

kaminari知道下一页是否存在的方法是什么?凭借will_paginate其简单,你只需要调用的next_page?型号.我想知道是否有任何东西kaminari

pagination ruby-on-rails will-paginate kaminari

5
推荐指数
1
解决办法
3259
查看次数

Rails smart_listing配置

我正在尝试在我的应用中设置smart_listing gem.我在何处以及如何配置每页分页结果的默认值.在smart_listing的文档中提到它使用kaminari.

ruby-on-rails kaminari ruby-on-rails-4 smart-listing

5
推荐指数
1
解决办法
3011
查看次数

自定义网址上的kaminari分页

我有一个由kaminari生成的url案例,paginate因为url取决于条件.这是我的路线

concern :search do
  scope '/search', as: :search do
    get '/', to: 'users#search'
    get '/schedule/:id', to: 'schedules#doctor', as: :schedule
  end
end

concerns :search

scope :dashboard, as: :dashboard do
  concerns :search
end
Run Code Online (Sandbox Code Playgroud)

如您所见,:search可通过/search和访问/dashboard/search.问题是,paginate @doctors/search它基本上都会去search_path,即使我在dashboard_search_path(它应该给出/dashboard/search路径).

我的问题是,我如何通过自定义路径paginate?我想paginatesearch_path,当我打开/search和使用dashboard_search_path,当我在/dashboard/search/path.

您不必提供如何决定,/search或者/dashboard/search我只需要知道如何将其paginate作为参数传递.钽

ruby-on-rails kaminari

5
推荐指数
1
解决办法
885
查看次数

用Kaminari分页跳到midde

假设我有30页与Kaminari分页:

#800 books, 25 per page
=paginate @books, :outer_window => 3, :window => 3
Run Code Online (Sandbox Code Playgroud)

它看起来像这样:

1 2 3 4 ... 30 31 32

没有点击5次就无法进入第16页.

  1. 1*2 3 [4] ... 30 31 32
  2. 1 2 3 4*5 6 [7] ... 30 31 32
  3. 1 2 3 4 5 6 7*8 9 [10] ... 30 31 32
  4. 1 2 3 ... 7 8 9 10*11 12 [13] ... 30 31 32
  5. 1 2 3 ... 10 11 12 13*14 15 [16] ... 30 …

pagination ruby-on-rails kaminari

5
推荐指数
1
解决办法
559
查看次数

ActionView :: Template :: Error(未定义的方法`沉默'为)

我对heroku有一个非常奇怪的问题.我认为看起来像这样:

= content_for :header_title do
  = t('.header_title')

- if @appointments.exists?
  %table.table.table-striped.table-bordered.table-hover
    %thead
      %tr
        %th= t('.id')
        %th= t('.athena_health_id')
        %th= t('.start_time')
        %th= t('.duration')
        %th= t('.provider')
        %th= t('.created_at')
        %th= t('.updated_at')
    %tbody
      = render @appointments

  = paginate @appointments
- else
  %h3.text-center= t('.appointments_not_found')
%hr/
Run Code Online (Sandbox Code Playgroud)

没什么特别的.当我访问在heroku上使用此模板的页面时,我收到:

 ActionView::Template::Error (undefined method `silence' for #<Logger:0x007f7a86267a70>):
Run Code Online (Sandbox Code Playgroud)

规格正在传递.在我当地一切都很好.我不知道发生了什么事.Stacktrace显示问题在于以下行:

= paginate @appointments
Run Code Online (Sandbox Code Playgroud)

我正在使用Rails 5.0和kaminari(1.0.0.alpha).有任何想法吗?

编辑:在我的production.rb中我有:

  if ENV['RAILS_LOG_TO_STDOUT'].present?
    config.logger = ActiveSupport::TaggedLogging.new(Logger.new(STDOUT))
  end

config.log_formatter = ::Logger::Formatter.new
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails heroku kaminari

5
推荐指数
1
解决办法
990
查看次数