如果使用Rails 3找不到搜索,则显示通知

时间:2015-06-04 06:23:19

标签: ruby ruby-on-rails-3.2

我使用will_paginate gem按照两个日期从表中搜索数据。我的要求是如果没有找到搜索数据,则应该向用户i.e-"No record found in between these two date."发送一个通知。下面给出了Mu代码文件

  

view_report.html.erb:

<div class="col-md-4 col-md-offset-1">
        <div class="text-left">
            To
        </div>

        <div class="input-group bmargindiv1 date" id='datepicker1' data-date="2015-05-01" data-date-format="yyyy-mm-dd">
           <%= text_field_tag :search1, params[:search1], :class => "form-control" %>
            <span class="add-on input-group-addon"><i class="fa fa-calendar"></i></span>
          </div>
          <script type="text/javascript">
          $('#datepicker1').datepicker({
                 format: "yyyy-mm-dd"
              });
          </script>
    </div>
    <%= submit_tag "Search",:class => "btn btn-info btn-lg" %>
    <div class="clearfix"></div>
    <% end %>
</div>

<!--1st_total_div-->
    <!--1st_total_div-->
    <div class="block-content">
        <div class="table-responsive">
            <table class="table table-bordered">
            <colgroup>
                <col class="col-new-md-1">
                <col class="col-new-md-1">
                <col class="col-new-md-1">
                <col class="col-new-md-1">
                <col class="col-new-md-1">
                <col class="col-new-md-1">
                <col class="col-new-md-1">
                <col class="col-new-md-1">
                <col class="col-new-md-1">
                <col class="col-new-md-1">
                <col class="col-new-md-1">
                <col class="col-new-md-1">
                <col class="col-new-md-1">
                <col class="col-new-md-1">
                <col class="col-new-md-1">
                <col class="col-new-md-1">
            </colgroup>
            <thead>
                <tr>
                <th class="text-center">Sl. No</th>
                <th class="text-center">Receipt<br>Number</th>
                <th class="text-center">Date</th>
                <th class="text-center">Deceased<br>Name</th>
                <th class="text-center">Doner<br>Name</th>
                <th class="text-center">Doner<br>Mobile No</th>
                <th class="text-center">Doner's<br>Relationship</th>
                <th class="text-center">Donation<br>Amount</th>
                <th class="text-center">Date of<br>death</th>
                <th class="text-center">HCSY<br>Status</th>
                <th class="text-center">Brahmin</th>
                <th class="text-center">Action</th>
                </tr>
            </thead>
            <tbody>
            <% @sdf.each do |sdf| %>
            <% @count=@count+1 %>
            <tr>
            <td class="text-center"><%= @count %></td>
             <td class="text-center"><%= sdf.Receipt_No %></td>
            <td class="text-center"><%= sdf.Created_On %></td>
            <td class="text-center"><%= sdf.Deceased_Name %></td>
            <td class="text-center"><%= sdf.Doner_Name %></td>
            <td class="text-center"><%= sdf.Doner_MobileNo %></td>
            <td class="text-center"><%= sdf.Doner_Relationship_Other %></td>
            <td class="text-center"><%= sdf.Donation_Amount %></td>
            <td class="text-center"><%= sdf.Date_Of_Death %></td>
            <td class="text-center"><%= sdf.HCSY_Status %></td>
            <td class="text-center"><%= sdf.Brahmin %></td>
            <td class="text-center">
            <div class="btn-group">
            <a href="/homes/home/?receipt_no=<%= sdf.Receipt_No %>" data-toggle="tooltip" title="" class="btn btn-xs btn-success" data-original-title="View">
                <i class="fa fa-eye"></i>
            </a>
            </div>
            </td>
            </tr>
            <% end %>
            </tbody>
            </table>
             <%= will_paginate @sdf %>  
        </div>
    </div>
    <!--end_1st_total_div-->

<!--end_1st_total_div-->

</div>
  

reports_controller.rb:

class ReportsController < ApplicationController
     helper_method :sort_column, :sort_direction
    def view_report
        @count=0
        @sdf =TSdf.all
        @sdf = TSdf.search(params[:search],params[:search1]).order(sort_column + ' ' + sort_direction).paginate(:per_page => 10, :page => params[:page])  
    end
    def sort_column
     TSdf.column_names.include?(params[:sort]) ? params[:sort] : "Created_On"
  end

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

t_sdf.rb:

class TSdf < ActiveRecord::Base
  # attr_accessible :title, :body
  self.table_name = 'T_SDF'
  def self.search(search,search1)  
    if search && search1 
      #where('Created_On LIKE ?', "%#{search}%")
      where("Created_On >= ? AND Created_On <= ?", search, search1 )  
    else  
      scoped  
    end  
  end  
end

请帮我按照要求添加此通知。

1 个答案:

答案 0 :(得分:1)

试试这个:

 <% if @sdf.blank? %>
   <p>No record found in between these two date.</p>
 <% else %>   
   <table class="table table-bordered">
     ....
     <thead>
       ....
     </thead>
     <tbody>
       <% @sdf.each do |sdf| %>
         ....
       <% end %>
     </tbody>
   </table>
   <%= will_paginate @sdf %> 
 <% end %>