用户中的参数错误#index

ArgumentError in Users#index

显示 /home/bdme551/bdme21/app/views/users/index.html.erb 第 4 行出现的位置:

@users 变量似乎为空。您是否忘记传递 will_paginate 的集合对象? 提取的源代码(大约第 4 行):

<h1>All users</h1>

<%= will_paginate %>

<ul class="users">
  <%= render @users %>

我知道我错过了什么。但是,我想不通。有谁可以帮助我吗?谢谢。

Users_controller

def index
    @users = User.all.paginate(:page => params[:page], :per_page => 5)
end

控制台日志

 ActionView::Template::Error (The @users variable appears to be empty. Did you forget to pass the collection object for will_paginate?):
    1: <% provide(:title, 'All users') %>
    2: <h1>All users</h1>
    3: 
    4: <%= will_paginate %>
    5: 
    6: <ul class="users">
    7:   <% @users.each do |user| %>

app/views/users/index.html.erb:4:in `_app_views_users_index_html_erb__4020588401683243365_70237529196060'
  Rendering /home/bdme551/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb within rescues/layout
  Rendering /home/bdme551/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
  Rendered /home/bdme551/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (6.7ms)
  Rendering /home/bdme551/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
  Rendered /home/bdme551/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (3.0ms)
  Rendering /home/bdme551/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
  Rendered /home/bdme551/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.9ms)
  Rendered /home/bdme551/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb within rescues/layout (32.0ms)
DEPRECATION WARNING: #original_exception is deprecated. Use #cause instead. (called from process_request at /usr/lib/ruby/vendor_ruby/phusion_passenger/rack/thread_handler_extension.rb:97)
  Rendering /home/bdme551/.rvm/gems/ruby-2.3.1/gems/web-console-3.1.1/lib/web_console/templates/index.html.erb
  Rendered /home/bdme551/.rvm/gems/ruby-2.3.1/gems/web-console-3.1.1/lib/web_console/templates/_markup.html.erb (0.9ms)
  Rendering /home/bdme551/.rvm/gems/ruby-2.3.1/gems/web-console-3.1.1/lib/web_console/templates/console.js.erb within layouts/javascript
  Rendering /home/bdme551/.rvm/gems/ruby-2.3.1/gems/web-console-3.1.1/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string
  Rendered /home/bdme551/.rvm/gems/ruby-2.3.1/gems/web-console-3.1.1/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.7ms)
  Rendering /home/bdme551/.rvm/gems/ruby-2.3.1/gems/web-console-3.1.1/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string
  Rendered /home/bdme551/.rvm/gems/ruby-2.3.1/gems/web-console-3.1.1/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.4ms)
  Rendering /home/bdme551/.rvm/gems/ruby-2.3.1/gems/web-console-3.1.1/lib/web_console/templates/style.css.erb within layouts/inlined_string
  Rendered /home/bdme551/.rvm/gems/ruby-2.3.1/gems/web-console-3.1.1/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.8ms)
  Rendered /home/bdme551/.rvm/gems/ruby-2.3.1/gems/web-console-3.1.1/lib/web_console/templates/console.js.erb within layouts/javascript (22.3ms)
  Rendering /home/bdme551/.rvm/gems/ruby-2.3.1/gems/web-console-3.1.1/lib/web_console/templates/main.js.erb within layouts/javascript
  Rendered /home/bdme551/.rvm/gems/ruby-2.3.1/gems/web-console-3.1.1/lib/web_console/templates/main.js.erb within layouts/javascript (0.5ms)
  Rendered /home/bdme551/.rvm/gems/ruby-2.3.1/gems/web-console-3.1.1/lib/web_console/templates/index.html.erb (39.0ms)

尝试像这样在您的索引操作中获取用户集合

@users = User.all.paginate(:page => params[:page], :per_page => 2) 

并在您的 index.html.erb 中执行此操作

<%= will_paginate @users %>

尝试添加 per_page

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

并将 @users 变量传递给 will_paginate

<%= will_paginate @users %>

为 index.html.erb 尝试此代码:

<% provide(:title, 'All users') %>
<h1>All users</h1>

<%= will_paginate %>

<ul class="users">
   <% @users.each do |user| %>
      <%= render user %>
   <% end %>
</ul>

<%= will_paginate %>

以及 app/views/users/_user 的类似内容。html.erb

<li>
   <%= link_to user.name, user %>
</li>

如果简单地删除分页会怎样?

假设您在视图的第 4 行输了 "will_paginate"

并且您将控制器操作从

更改为

@users = User.paginate(页面:参数[:页面],per_page:5)

只是

@users = User.all

我知道您希望分页有效,但让我们从已知的好的开始,迭代地朝着您需要的工作解决方案努力。你说你有两个用户,但是 User.all 找到他们了吗?

将以下内容添加到 Gemfile

gem 'will_paginate', '~> 3.1.0'

和运行bundle install