路由:
namespace :admin do
root 'users#index'
resources :users do
collection do
get 'search'
end
end
end
controller
def index
@users = User.page(params[:page] || 1).per_page(params[:per_page] || 10).order("id desc")
enddef search
@users = User.page(params[:page] || 1).per_page(params[:per_page] || 10).order("id desc").where(["username like ?", "%#{params[:username]}%"])
render action: :index
end
view
<h1>管理员后台-用户列表</h1>
<div class="row">
<div class="col-sm-12">
<form action="<%= search_admin_users_path %>" method="get">
用户名:<input type="text" name="username" value="<%= params[:username] %>" />
<input type="submit" name="搜索">
</form>
<table class="table">
<tr>
<th>ID</th>
<th>用户名</th>
<th></th>
</tr>
<% @users.each do |user| %>
<tr>
<td><%= user.id %></td>
<td><%= user.username %></td>
<td></td>
</tr>
<% end -%>
</table>
<%= will_paginate @users, renderer: BootstrapPagination::Rails %>
</div>
</div>