rails 管理员用户管理简单实现

来源:互联网 发布:京东大数据部门怎么样 编辑:程序博客网 时间:2024/06/10 01:07

路由:

  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")

end


def 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>


0 0
原创粉丝点击