0

在我的用户索引页面上,我试图在顶部有一个搜索引擎,以便您可以通过电子邮件搜索用户,而不是扫描整个列表。但是,我收到错误:

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
4

1 回答 1

1

您没有在@users = User.search(params[:search]). 尝试添加.page(params[:page]).per_page(10),看看是否有效。

更新:

抱歉,我忘了.find返回一个数组而不是一个关系。将您的搜索方法更改为:

def self.search(search)
  where('email LIKE ?', "%#{search}%")
end
于 2012-10-14T02:38:23.720 回答