简体   繁体   English

Rails Will_Paginate抛出未定义的局部变量或方法

[英]Rails Will_Paginate throwing undefined local variable or method

I've been having trouble with will_paginate based on an example from Michael Hartel's Ruby on Rails book 9.3.3. 根据Michael Hartel的Ruby on Rails书9.3.3中的示例,我在will_paginate方面遇到了麻烦。

Here is my views/professors/index.html.erb: 这是我的views / professors / index.html.erb:

    <% provide(:title, 'All Professors') %>
<h2>All Users</h2>
<%= will_paginate %>
<ul class="users">
  <% @professors.each do |professor| %>
  <li>
    <%= link_to professor.lname, professor %>
  </li>
  <% end %>
</ul>
<%= will_paginate %>

and the professors controller: 和教授的负责人:

  def show
    @professor = Professor.find(params[:id])
    @professors = Professor.paginate(page: params[:page])
  end

I keep getting the: undefined local variable or method `will_paginate' for #class.... error. 我不断收到:#class ....错误的未定义局部变量或方法`will_paginate'。

From what I've seen Hartel's version is a bit different then what the will_paginate docs say to do they have: 从我所见,Hartel的版本与will_paginate文档所说的有所不同:

@posts = Post.paginate(:page => params[:page])

In the controller and 在控制器和

<%= will_paginate @posts %>

on the render page. 在渲染页面上。 I've tried that too and I get an undefined method error. 我也尝试过,但出现未定义的方法错误。 Also tried the variable in parenthisees (which I have seen some answers on here suggesting). 还尝试了在父母名单中的变量(我在这里看到了一些建议的答案)。

You are showing the template for the index action and the controller for the show action. 您正在显示index动作的模板和show动作的控制器。 You also appear to have <%= will_paginate %> twice both without anything passed in. Can you update your question. 您也似乎两次都<%= will_paginate %>都没有传递任何内容。您能更新您的问题吗?

You need to move your @professors assignment to def index and not def show and then it should work when going to localhost:3000/professors or whatever your path is setup as. 您需要将@professors分配移动到def index而不是def show ,然后在转到localhost:3000/professors或任何设置路径时都可以使用。

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

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