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

路由:

  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>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值