RoR中的分页无法正常工作

时间:2016-08-28 19:25:07

标签: ruby-on-rails pagination

我正在制作一个小型ruby-on-rails计划,偶然发现了一个分页问题。安装宝石后进行分页:

gem 'will_paginate', '3.0.7'
gem 'bootstrap-will_paginate', '0.0.10'

我将控制器类更改为:

def index
    @allArticles = Article.paginate(page: params[:page], per_page: 5)
end

并将 html.erb 文件更改为:

<div align="center">
  <%= will_paginate %>
</div>

<%= render 'article', obj: @allArticles %>

<div align="center">
  <%= will_paginate %>
</div>

当我在本地运行服务器时,它给了我这个问题:

=> Booting Puma
=> Rails 5.0.0 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
Exiting
/home/inson/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.0/lib/active_support/i18n_railtie.rb:45:in `map': undefined method `existent' for #<String:0x00000004c89818> (NoMethodError)
Did you mean?  extend
    from /home/inson/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.0/lib/active_support/i18n_railtie.rb:45:in `block in initialize_i18n'
    from /home/inson/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.0/lib/active_support/i18n_railtie.rb:41:in `each'
    from /home/inson/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.0/lib/active_support/i18n_railtie.rb:41:in `initialize_i18n'
    from /home/inson/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.0/lib/active_support/i18n_railtie.rb:15:in `block in <class:Railtie>'
    from /home/inson/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.0/lib/active_support/lazy_load_hooks.rb:36:in `execute_hook'
    from /home/inson/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.0/lib/active_support/lazy_load_hooks.rb:45:in `block in run_load_hooks'
    from /home/inson/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.0/lib/active_support/lazy_load_hooks.rb:44:in `each'
    from /home/inson/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.0/lib/active_support/lazy_load_hooks.rb:44:in `run_load_hooks'
    from /home/inson/.rvm/gems/ruby-2.3.0/gems/railties-5.0.0/lib/rails/application/finisher.rb:65:in `block in <module:Finisher>'
    from /home/inson/.rvm/gems/ruby-2.3.0/gems/railties-5.0.0/lib/rails/initializable.rb:30:in `instance_exec'
    from /home/inson/.rvm/gems/ruby-2.3.0/gems/railties-5.0.0/lib/rails/initializable.rb:30:in `run'
    from /home/inson/.rvm/gems/ruby-2.3.0/gems/railties-5.0.0/lib/rails/initializable.rb:55:in `block in run_initializers'
    from /home/inson/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/tsort.rb:228:in `block in tsort_each'
    from /home/inson/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component'
    from /home/inson/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/tsort.rb:431:in `each_strongly_connected_component_from'
    from /home/inson/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/tsort.rb:349:in `block in each_strongly_connected_component'
    from /home/inson/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/tsort.rb:347:in `each'
    from /home/inson/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/tsort.rb:347:in `call'
    from /home/inson/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/tsort.rb:347:in `each_strongly_connected_component'
    from /home/inson/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/tsort.rb:226:in `tsort_each'
    from /home/inson/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/tsort.rb:205:in `tsort_each'
    from /home/inson/.rvm/gems/ruby-2.3.0/gems/railties-5.0.0/lib/rails/initializable.rb:54:in `run_initializers'
    from /home/inson/.rvm/gems/ruby-2.3.0/gems/railties-5.0.0/lib/rails/application.rb:352:in `initialize!'
    from /home/inson/Desktop/Ruby/code/rails-projects/alpha-blog/config/environment.rb:5:in `<top (required)>'
    from /home/inson/Desktop/Ruby/code/rails-projects/alpha-blog/config.ru:3:in `require_relative'
    from /home/inson/Desktop/Ruby/code/rails-projects/alpha-blog/config.ru:3:in `block in <main>'
    from /home/inson/.rvm/gems/ruby-2.3.0/gems/rack-2.0.1/lib/rack/builder.rb:55:in `instance_eval'
    from /home/inson/.rvm/gems/ruby-2.3.0/gems/rack-2.0.1/lib/rack/builder.rb:55:in `initialize'
    from /home/inson/Desktop/Ruby/code/rails-projects/alpha-blog/config.ru:in `new'
    from /home/inson/Desktop/Ruby/code/rails-projects/alpha-blog/config.ru:in `<main>'
    from /home/inson/.rvm/gems/ruby-2.3.0/gems/rack-2.0.1/lib/rack/builder.rb:49:in `eval'
    from /home/inson/.rvm/gems/ruby-2.3.0/gems/rack-2.0.1/lib/rack/builder.rb:49:in `new_from_string'
    from /home/inson/.rvm/gems/ruby-2.3.0/gems/rack-2.0.1/lib/rack/builder.rb:40:in `parse_file'
    from /home/inson/.rvm/gems/ruby-2.3.0/gems/rack-2.0.1/lib/rack/server.rb:318:in `build_app_and_options_from_config'
    from /home/inson/.rvm/gems/ruby-2.3.0/gems/rack-2.0.1/lib/rack/server.rb:218:in `app'
    from /home/inson/.rvm/gems/ruby-2.3.0/gems/railties-5.0.0/lib/rails/commands/server.rb:59:in `app'
    from /home/inson/.rvm/gems/ruby-2.3.0/gems/rack-2.0.1/lib/rack/server.rb:353:in `wrapped_app'
    from /home/inson/.rvm/gems/ruby-2.3.0/gems/railties-5.0.0/lib/rails/commands/server.rb:124:in `log_to_stdout'
    from /home/inson/.rvm/gems/ruby-2.3.0/gems/railties-5.0.0/lib/rails/commands/server.rb:77:in `start'
    from /home/inson/.rvm/gems/ruby-2.3.0/gems/railties-5.0.0/lib/rails/commands/commands_tasks.rb:90:in `block in server'
    from /home/inson/.rvm/gems/ruby-2.3.0/gems/railties-5.0.0/lib/rails/commands/commands_tasks.rb:85:in `tap'
    from /home/inson/.rvm/gems/ruby-2.3.0/gems/railties-5.0.0/lib/rails/commands/commands_tasks.rb:85:in `server'
    from /home/inson/.rvm/gems/ruby-2.3.0/gems/railties-5.0.0/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
    from /home/inson/.rvm/gems/ruby-2.3.0/gems/railties-5.0.0/lib/rails/commands.rb:18:in `<top (required)>'
    from /home/inson/Desktop/Ruby/code/rails-projects/alpha-blog/bin/rails:9:in `require'
    from /home/inson/Desktop/Ruby/code/rails-projects/alpha-blog/bin/rails:9:in `<top (required)>'
    from /home/inson/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/client/rails.rb:28:in `load'
    from /home/inson/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/client/rails.rb:28:in `call'
    from /home/inson/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/client/command.rb:7:in `call'
    from /home/inson/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/client.rb:30:in `run'
    from /home/inson/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/bin/spring:49:in `<top (required)>'
    from /home/inson/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/binstub.rb:11:in `load'
    from /home/inson/.rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/binstub.rb:11:in `<top (required)>'
    from /home/inson/Desktop/Ruby/code/rails-projects/alpha-blog/bin/spring:13:in `require'
    from /home/inson/Desktop/Ruby/code/rails-projects/alpha-blog/bin/spring:13:in `<top (required)>'
    from bin/rails:3:in `load'
    from bin/rails:3:in `<main>'

我不知道为什么它会给我一个问题。

1 个答案:

答案 0 :(得分:0)

我正在使用相同的插件来处理我正在开发的小型API。这是关于我如何使用will-paginate

的小片段
# GET /posts
      def index @posts = paginate Post.all, per_page: 10 
           render :json => { 
                             :data => @posts,
                             :success => true
           } 
       end

您可以更改配置文件中的全局参数。或者你可以像填充变量时那样明确设置某些参数。

希望它有所帮助!