用mongoid搜索搜索

时间:2017-08-28 07:36:35

标签: ruby-on-rails ruby search mongoid

我有3个模型用户,user_role和store .user_role属于user和store.i有一个成员控制器,我有一个搜索框用于存储,一个用于user_role(两个都是选择框)下面是搜索方法在user_role模型中

def self.filter(params)
  filter = params[:search] || {}
  user_roles = order(role_type: :asc) 
 if filter[:name].present?
  end 

if filter[:email].present?

end  

if filter[:role].present?
  user_roles = user_roles.where(role_type: filter[:role])
end

if filter[:store_id].present?
  user_roles = user_roles.where(store_id: filter[:store_id])
end

user_roles
end

这是我的成员控制器

def index
   if current_user.super_admin?
   @total_user_roles = UserRole.filter(params).order(sort_column + " " 
   + sort_direction)
   @user_roles = @total_user_roles.page(params[:page]).per(10) 
  else
   stores = current_user.user_roles.where(:role_type.in => 
   ['store_manager', 'merchant']).pluck(:store_id)
   @total_user_roles = UserRole.where(:store_id.in => 
   stores).filter(params).order(sort_column + " " + sort_direction)
   @user_roles = @total_user_roles.page(params[:page]).per(10)
 end
 respond_to do |format|
    format.html
    format.csv { send_data @users.to_csv }
 end
 end

def sort_column
  User.attribute_names.include?(params[:sort]) ? params[:sort] : 
 "first_name"
end

def sort_direction
  %w[asc desc].include?(params[:direction]) ? params[:direction] : 
  "asc"
end

我的搜索表单在视图中。我应该在名称和电子邮件字段中更改任何内容,或者只是保留它吗?

<section>
  <div class="col-md-12">
  <div class="row">
  <section class="content-header">
   <div class="white card-2">
    <h3>Users</h3>
     </div>
   </section>
  </div>

 <div class="form white">
  <!-- form start -->
   <form class="form-horizontal">
     <div class="box-body">
      <h3>Search Users </h3>

    <div class="col-md-3">
     <label class="control-label" 
      for="order_search_order_number">First Name</label>
      <input class="form-control" id="order_search_order_number" 
      name="search[name]" type="text" value="">
      <span class="md-input-bar"></span>
      </div>
      <div class="col-md-3">
       <label class="control-label">Email</label>
       <input class="form-control" id="order_search_order_number" 
       name="search[email]" type="text">
       <span class="md-input-bar"></span>
      </div>
      <div class="col-md-3 input">
        <label class="control-label">Roles</label>
        <%= select_tag("search[role]", 
      options_for_select(User::ADMIN_ROLES.map { |e| [e.humanize, e]}, 
      params[:search] && params[:search][:role]), include_blank: true, 
      class: 'select2') %>
        <span class="md-input-bar"></span>
      </div>
      <div class="col-md-3">
        <label class="control-label">Store</label>
       <%= select_tag("search[store_id]", 
      options_for_select(Store.all.order('name ASC').map { |e| 
     [e.name, e.id] }, params[:search] && params[:search][:store_id]), 
    include_blank: true, class: 'select2') %>
        <span class="md-input-bar"></span>
      </div>
    </div>
    <h4 class="pull-right"> Total Users: <b> <%= 
     @total_user_roles.count %></b>
    </h4>
    <br><br>

    <div class="box-footer">
      <button type="submit" class="btn btn-info">Filter</button>
      <a href="/admin/members" class="btn btn-default">Clear</a>
     </div>
    <!-- /.box-footer -->
   </form>
  </div>
  </div>
</section>
<div class="col-md-12">
 <section class="section-data">
 <!-- Section Data Table -->
 <div class="row">
  <div class="col-md-12">
    <div class="box">
      <div class="box-body table-responsive custom-sort">
        <table class="table table-striped table-bordered nowrap data-
        table" cellspacing="0" width="100%">
          <thead>
          <tr>
            <th><%= sortable 'first_name', "Name" %></th>
            <th><%= sortable 'email' %></th>
            <th>Store</th>
            <th><%= sortable 'role' %></th>
            <th>Status</th>
            <th>Action</th>
            <th>Active?</th>
          </tr>
          </thead>
          <tbody>
          <% @user_roles.each do |role| %>

            <tr>
              <td><%= role.user.name%></td>
              <td><%= role.user.email %></td>
              <td><%= role.store.try(:name) %></td>
              <td><%= role.role_type.try(:humanize) %></td>
              <td><%= role.user.pending_invitation? ? "Pending" : 
              "Accepted" %></td>
              <td>
       <a class="ion-edit actions" href="/admin/members/<%= 
               role.id %>/edit"></a>
       <% if role.user != current_user %>
       <a class="ion-android-delete actions red" data-
       sweet-alert-confirm="<%= t(:delete_confirm) %>" data-
       method="delete" href="/admin/members/<%= role.user.id %>"></a>
       <% end %>
              </td>
              <td>
              <div class="onoffswitch">
       <input type="checkbox" name="active" <%= "checked" if 
      role.user.active %> class="onoffswitch-checkbox" id="<%= 
      role.user.id %>" data-id="<%= role.user.id %>">
      <label class="onoffswitch-label" for="
      <%=role.user.id %>">
                    <span class="onoffswitch-inner"></span>
                    <span class="onoffswitch-switch"></span>
                  </label>
                </div>
            </tr>
          <% end %>
          </tbody>
        </table>
        <div class="pull-right">
          <%= paginate @user_roles, :theme => 'twitter-bootstrap-3' %>
        </div>
      </div>
      <!-- /.box-body -->
    </div>
    <!-- /.box -->
  </div>
  </div>
 </section>
</div>

now i want to implement search for user name and email ,i tried lots of ways but cant come up with solution.as Iam not expert need some solution from a expert .iam using mongoid search gem.

0 个答案:

没有答案