Home > Mobile >  Rails search with will Paginate
Rails search with will Paginate

Time:11-02

Below the code I am using now.Current code pagination will show 100 users per page.Currently search functionlaity implemented using JQuery code.Since pagination showing 100 records, search only working for 100 records. I need to implement search with will paginate.If I search users,It will fetch details from database and show in table .Can anyone please suggest how implement this with will paginate in rails?

Controller

 @user_list = User.all.paginate(:page => params[:page], :per_page => 100)

html.erb

 <%= will_paginate @user_list %>
<table id="users_access_table">
  <thead>
    <tr>
    <th>Name</th>
    <th>Email</th>
    <th>Curernt Access</th>
    <th>API User Access</th>
     </tr>
  </thead>
  <tbody id="table_body">
  <br>
  <div class="col-md-4" style="float: left;">
    <input hljs-string">" title="Search" type="text" id="userinput" onkeyup=" searchUsers()" placeholder="Search &#x1F50D; :">
  </div>
  <br>
  <br>
  <% @user_list.each do |user| %>
    <tr>
      <td>
        <%=user['name']%>
      </td>
      <td>
        <%=user['email']%>
      </td>
       <td>
        <%=user['access']%>
      </td>
       <td>
        <%=user['api_users']%>
      </td>
      
 </td>
        
       <td>
 

CodePudding user response:

You need to make changes in the controller level and UI side as well.

I am considering the parameters are coming to the controller from the UI side, and making changes in the query on the user controller.

Instead of all you need to create LIKE query

@user_list = User.all.paginate(:page => params[:page], :per_page => 100)

change it with

@user_list = User.where("name LIKE ? OR email LIKE ?", "%#{search_param}%", "%#{search_param}%").page(params[:page]).per(100)

hope it helps you to resolve your query.

  • Related