会不会因Rails 5.1而分页?

时间:2018-12-14 14:58:27

标签: ruby-on-rails will-paginate ruby-on-rails-5.1

自从升级到Rails 5.1以来,在尝试对未过滤(但未过滤)的用户表结果进行分页时,出现以下错误:

will-paginate error

我使用will-paginate 3.1.6和Rails 5.1.0。我什至不知道是哪条线引起了错误。大概是这条线

ReservationService

由于错误上方,我看到一个数据库调用,要求偏移25限制为25的用户。

我什至尝试为(标准ActiveRecord)@WebMvcTest @RunWith(SpringRunner.class) public class MvcTest { @Autowired private MockMvc mockMvc; @MockBean(name = "reservation") private ReservationService reservationService; //my chnage here @Test public void postReservation() throws Exception { // You need that to specify what should your mock return when getReservation() is called. Without it you will get null when(reservationService.getReservation()).thenReturn(new Reservation()); //my chnage here mockMvc.perform(MockMvcRequestBuilders.post("/reservation")) .andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)) .andExpect(MockMvcResultMatchers.status().isOk()); } } 模型创建format.html { @members = @members.page(params[:page]); render } 方法,但这将错误更改为抱怨,因为它不是字符串。

我需要将呼叫更改为will_paginate吗?我需要更早版本的will_paginate吗?如何使其与Rails 5.1一起使用?

编辑:这是@members填充的方式:

empty?

4 个答案:

答案 0 :(得分:1)

错误很可能是在视图中,您将User的单个实例传递给了page_entries_info而不是集合(您没有提供视图和整个控制器动作,因此很难分辨)

可执行错误示例如下:

#!/usr/bin/env ruby
# frozen_string_literal: true

require "bundler/inline"

install = true
gemfile(install) do
  source "https://rubygems.org"
  gem "rails", "5.1.0"
  gem "will_paginate", '3.1.6'
  gem "sqlite3"
end

require "active_record"
require "action_controller/railtie"
require "will_paginate"
require "logger"
require "rack/test"

# we are skipping initializers so include manually:
require 'will_paginate/active_record'
require 'will_paginate/view_helpers/action_view'

ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Schema.define do
  create_table :users, force: true do |t|
    t.string :country
  end
end

class User < ActiveRecord::Base
  self.per_page = 25
end

class TestApp < Rails::Application
  config.root = __dir__
  config.session_store :cookie_store, key: "cookie_store_key"
  secrets.secret_key_base = "secret_key_base"

  config.logger = Logger.new($stdout)
  ActiveRecord::Base.logger = config.logger
  Rails.logger  = config.logger

  routes.draw{ get "/" => "members#index" }
end

class MembersController < ActionController::Base
  include Rails.application.routes.url_helpers

  def index    
    @members = User.order("id DESC")
    @members = @members.page(params[:page])
    respond_to do |format|
      format.html{
        # passing single instance instead of collection:
        render inline: "<%= page_entries_info(@members.first) %>"
      }
    end
  end
end

require "minitest/autorun"

class BugTest < Minitest::Test
  include Rack::Test::Methods

  def test_returns_success
    User.create!
    get "/?page=1"
    assert last_response.ok?
    puts last_response.body
  end

  private
    def app
      Rails.application
    end
end

答案 1 :(得分:0)

我相信你应该改变

@members = @members.page(params[:page])

@members = User.page(params[:page])

但当然取决于@members的设置。如果以前根据某些搜索条件将@members设置为User的数组,则不应实例化User对象,而应将关系保留在@members中。

答案 2 :(得分:0)

更改您认为导致此问题的行(在您的控制器中):

format.html { @members = @members.page(params[:page]); render }

对此(如果您也使用JavaScript):

@members = Member.paginate(page: params[:page]).order("id DESC")
respond_to do |format|
  format.html
  format.js
end

或者这个(如果您愿意的话,可以使用原始版本):

format.html { @members = Member.paginate(page: params[:page]); render }

在您看来,请尝试以下操作:

<% if current_user.admin_level < 8 %>
  <%= will_paginate @members.where("country = ?", current_user.country) %>
<% end %>

答案 3 :(得分:-1)

可能不是这种情况,但是I've been reading the gem in question,不是吗??:

format.html { @members = @members.paginate(page: params[:page]); render }