在我的用户索引页面上我试图在顶部设置搜索引擎,以便您可以通过电子邮件搜索用户,而不是扫描整个列表。但是,我收到了错误:
undefined method 'total_pages'
当我搜索任何一个词时。
有人可以帮忙吗?谢谢!
用户/ index.html.erb
<h1>All users</h1>
<%= form_tag users_path, :method => "get" do %>
<%= label_tag(:search, "Search for user by email:") %>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
<% end %>
<%= will_paginate @users %>
<br>
<% @users.each do |user| %>
<div>
<%= link_to (image_tag user.profilepic? ? user.profilepic : "questionmark.png", :size => "50x50"), user_path(user) %>
<%= link_to user.name, user_path(user) %> (<%= user.email %>)
<% unless current_user.friends.include?(user) || current_user == user %>
<span class="floatright"><%= link_to "Add friend", user_friends_path(:user_id => current_user.id, :friend_id => user.id), :method => :post %></span>
<% end %>
<% if current_user.friends.include?(user) %>
<span class="floatright"><%= link_to "Remove friend", user_friends_path(:user_id => current_user.id, :friend_id => user.id), :method => :delete, :confirm => "Are you sure you want to remove #{user.name} from your friends?" %></span>
<% end %>
</div>
<% end %>
用户控制器
def index
if params[:search]
@users = User.search(params[:search])
else
@users = User.order("email").page(params[:page]).per_page(10)
end
end
用户模型
def self.search(search)
find(:all, :conditions => ['email LIKE ?', "%#{search}%"])
end
答案 0 :(得分:1)
您未在@users = User.search(params[:search])
中调用分页方法。尝试添加.page(params[:page]).per_page(10)
并查看是否有效。
<强>更新强>
抱歉,我忘了.find
返回一个数组,而不是关系。将搜索方法更改为:
def self.search(search)
where('email LIKE ?', "%#{search}%")
end