简体   繁体   English

Rails:Ransack和will_paginate(未定义的方法“ total_pages”错误)

[英]Rails: Ransack and will_paginate (undefined method 'total_pages' error)

I'm attempting to use will_paginate for a search with Ransack for pagination, and I'm getting the following error: undefined method `total_pages' for # 我试图使用will_paginate与Ransack进行分页搜索,但出现以下错误:#的未定义方法`total_pages'

I'm not sure what it is that I'm doing incorrectly here and have tried a number of possible solutions with no luck. 我不确定我在这里做错了什么,并且尝试了许多可能的解决方案而没有运气。 Is it possible to use these together and what am I doing incorrectly here? 是否可以将它们一起使用,我在这里做错了什么? Thanks. 谢谢。

coin_controller.rb coin_controller.rb

class CoinsController < ApplicationController

  load_and_authorize_resource param_method: :question_params
  before_action :find_coin, only: [:edit, :update, :destroy ]
  before_action :authenticate_user!, except: [:index, :create, :show]

  def index      
    @search = Coin.ransack(params[:q])
    @coins = @search.result.paginate(page: params[:page], per_page: params[:per_page])

  end

  .
  .
  .

end 

index.html.erb index.html.erb

<% provide(:title, "Coins") %>
<% @coins = @coins.sort_by &:currency_name %>

<%= will_paginate @coins %>
<div class="row">       
  <div class="col-md-8" id="pagination_table">
    <% @coins.each do |coin| %>
      <% if coin.accepted %>
        <ul>
          <%= image_tag coin.picture.thumb.url if coin.picture? %>
          <%= link_to coin.currency_name, :action => 'show', :id => coin %>
        </ul>
      <% end %> 
    <% end %>
    <% if can? :edit, Coin %>   
      <hr><b>Pending Approval</b><hr>
      <% @coins.each do |coin| %>               
        <% if not coin.accepted %>
          <ul>
            <%= image_tag coin.picture.thumb.url if coin.picture? %>
            <%= link_to coin.currency_name, :action => 'show', :id => coin %>
          </ul>
        <% end %>
      <% end %>
    <% end %>
</div>

<div class="col-md-3" id="side-bar">    
  <%= link_to "Submit a New Coin", new_coin_path, class: "btn btn-default" %>
  <fieldset class="search-field">
    <legend>Search All Coins</legend>
      <%= search_form_for @search do |f| %>
        <div class="field">
          <%= f.label :currency_name_cont, "Search by Name" %>
          <%= f.text_field :currency_name_cont %>
        </div>
        <div class="actions"><%= f.submit "Search" %></div>
        <div class="field">
          <%= f.label "Search by Genre" %><br />
          <%= f.collection_check_boxes :genres_id_in_any, Genre.all, :id, :displayname do |b| %>
            <div class="collection-check-box">
              <%= b.check_box %>
              <%= b.label %>
            </div>
          <% end %>
        </div>
        <div class="actions"><%= f.submit "Search" %></div>
    <% end %>
  </fieldset>
  </div>
</div>

<script>
  function openNav() {
    document.getElementById("mySidenav").style.width = "250px";
  }

  function closeNav() {
    document.getElementById("mySidenav").style.width = "0";
  }
</script>

I also tried using Kaminari and got an identical error. 我也尝试使用Kaminari,并得到了相同的错误。

Try this solution... 试试这个解决方案...

Instead of this line 代替这条线

<%= will_paginate @coins %>

To

<%= will_paginate @coins if @coins.present? %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM