使用Rails 3中的will_paginate gem搜索数据库值

时间:2015-04-22 05:31:59

标签: ruby datepicker ruby-on-rails-3.2

我正在使用will_paginate gem根据已经保存在DB中的日期来过滤表。我的db列名和列值就像这个Created_On-> 2015-04-15 23.00.59。在我的搜索字段中,我使用gem 'bootstrap-datepicker-rails', '>= 0.6.21'来接受日历中的日期,它会给出04/15/2015之类的日期。在这种格式下,我无法正确获取数据。请帮我格式化这种类型的日期,与db日期格式相同,并成功从表中搜索。

我正在解释下面的代码。

报告/ view_report.html.erb

<div class="input-group bmargindiv1 date" data-behaviour='datepicker1'>
         <%= form_tag reports_view_report_path, :method => 'get' do %>  
         <%= text_field_tag :search, params[:search], :class => "form-control", :onchange => 'this.form.submit();'%> 
           <% end %>
            <span class="input-group-addon">
                <span class="fa fa-calendar"></span>
            </span>
            <script type="text/javascript">
              $('[data-behaviour~=datepicker1]').datepicker();
            </script>
          </div>
    </div>
    <div class="clearfix"></div>
</div>

<!--1st_total_div-->
    <!--1st_total_div-->
    <div class="block-content">
        <div class="table-responsive">
            <table class="table table-bordered">
            <colgroup>
                <col class="col-md-1 col-sm-1">
                <col class="col-md-2 col-sm-2">
                <col class="col-md-3 col-sm-3">
                <col class="col-md-3 col-sm-3">
                <col class="col-md-1 col-sm-1">
                <col class="col-md-1 col-sm-1">
                <col class="col-md-1 col-sm-1">
            </colgroup>
            <thead>
                <tr>
                <th class="text-center">Sl. No</th>
                <th class="text-center">Receipt Number</th>
                <th class="text-center">Date</th>
                <th class="text-center">Deceased Name</th>
                <th class="text-center">Deceased Adress</th>
                <th class="text-center">Doner Name</th>
                <th class="text-center">Doner Adress</th>
                <th class="text-center">Doner Mobile No</th>
                <th class="text-center">Doner's Relationship</th>
                <th class="text-center">Donation Amount</th>
                <th class="text-center">Date of death</th>
                <th class="text-center">HCSY Status</th>
                <th class="text-center">Deceased photo</th>
                <th class="text-center">Doner photo</th>
                <th class="text-center">Brahmin</th>
                <th class="text-center">Action</th>
                </tr>
            </thead>
            <tbody>
            <% @sdf.each do |sdf| %>
            <tr>
            <td class="text-center"><%= sdf.Sdp_Id %></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.Deceased_Address1 %></td>
            <td class="text-center"><%= sdf.Doner_Name %></td>
            <td class="text-center"><%= sdf.Doner_Address1 %></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"><%= image_tag(sdf.Photo,:width => 50,:height => 50) %></td>
            <td class="text-center"><%= image_tag(sdf.Doner_Photo,:width => 50,:height => 50) %></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> 

控制器/ reports_controller.rb

class ReportsController < ApplicationController
     helper_method :sort_column, :sort_direction
    def view_report
        @sdf =TSdf.all
        @sdf = TSdf.search(params[:search]).order(sort_column + ' ' + sort_direction).paginate(:per_page => 5, :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)  
    if search  
      where('Created_On LIKE ?', "%#{search}%")  
    else  
      scoped  
    end  
  end  
end

的Gemfile

source 'https://rubygems.org'

gem 'rails', '3.2.19'

# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'

gem 'mysql'


# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails',   '~> 3.2.3'
  gem 'coffee-rails', '~> 3.2.1'

  # See https://github.com/sstephenson/execjs#readme for more supported runtimes
  # gem 'therubyracer', :platforms => :ruby

  gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails'

# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'
gem "bcrypt-ruby", :require => "bcrypt"
# To use Jbuilder templates for JSON
# gem 'jbuilder'

# Use unicorn as the app server
# gem 'unicorn'

# Deploy with Capistrano
# gem 'capistrano'

# To use debugger
# gem 'debugger'
gem 'bootstrap-datepicker-rails', '>= 0.6.21'
gem 'will_paginate', '~> 3.0'

即使我因日期时间格式不同而从日历中选择正确的日期,我也无法搜索。请帮我解决此问题。

0 个答案:

没有答案