我试图在日期字段中搜索:
<%= f.label :datestarted_cont, "Date started Contains", class: "slbl four" %>
<%= f.text_field :datestarted_cont, class: "frm-element turquoise" %>
但它不起作用。我想至少搜索一年。我怎么能这样做?
实际上我没有使用单独的搜索方法。这是我的索引方法,我做了所有的事情:
def index
def serach
params[:q][:datestarted_cont] = change_date_format(params[:q][:datestarted_cont]) if !(params[:q][:datestarted_cont]).blank?
end
def change_date_format(date)
if date.include? '/'
date_array = date.split("/")
new_date = date_array[1]+"/"+date_array[2]+"/"+date_array[0]
puts new_date
return new_date
else
return date
end
end
############ Turn empty params[:district] to nil and params[:district] "Select District" to nil
if params[:q] != nil
params[:q][:district_cont] = nil_paramsdistrict(params[:q][:district_cont])
end
############ Turn empty @district to nil
@district = nil_district(@district)
############ Set the District. If params[:district] is not nil, @district = params[:district]. If params[:district] is nil and @district is not nil params[:district] = @district
set_district
if params[:commit] == "Clear"
params[:q] = nil
end
@search = Awardunit.search(params[:q])
@awardunits = @search.result.paginate(page: params[:page], per_page: 20)
@unitlist = @search.result
@units = @unitlist.size
@disabledunits = @unitlist.where(disabled: true).size
@leaders = 0
@unitlist.each do |unit|
@leaders = @leaders + unit.awardleaders.size
end
@disabledleaders = 0
@unitlist.each do |unit|
@disabledleaders = @disabledleaders + unit.awardleaders.where(disabled: true).size
end
@asstleaders = 0
@unitlist.each do |unit|
@asstleaders = @asstleaders + unit.asstawardleaders.size
end
@assessors = 0
@unitlist.each do |unit|
@assessors = @assessors + unit.assessors.size
end
@approvedassessors = 0
@unitlist.each do |unit|
@approvedassessors = @approvedassessors + unit.assessors.where(approved: true).size
end
@members = 0
@unitlist.each do |unit|
@members = @members + unit.members.size
end
@disabledmembers = 0
@unitlist.each do |unit|
@disabledmembers = @disabledmembers + unit.members.where(disabled: true).size
end
@bronzemembers = 0
@unitlist.each do |unit|
@bronzemembers = @bronzemembers + unit.members.where(currentaward: "Bronze").size
end
@silvermembers = 0
@unitlist.each do |unit|
@silvermembers = @silvermembers + unit.members.where(currentaward: "Silver").size
end
@goldmembers = 0
@unitlist.each do |unit|
@goldmembers = @goldmembers + unit.members.where(currentaward: "Gold").size
end
respond_to do |format|
format.html
format.pdf do
pdf = MemberPdf.new(@search.result.order( 'nyaaid ASC' ), view_context, Awardunit.all.size)
send_data pdf.render, filename: "AwardUnits_List.pdf", type: "application/pdf", disposition: "inline"
end
end
end
答案 0 :(得分:1)
试试这个
<强> _form.html.erb 强>
<%= f.label :datestarted_cont, "Date started Contains", class: "slbl four" %>
<%= f.text_field :datestarted_cont, class: "frm-element turquoise" %>
<强>更新强>
def index
params[:q][:datestarted_cont] = change_date_format(params[:q][:datestarted_cont]) if !(params[:q][:datestarted_cont]).blank?
############ Turn empty params[:district] to nil and params[:district] "Select District" to nil
if params[:q]!= nil
params[:q][:district_cont] = nil_paramsdistrict(params[:q][:district_cont])
end
############ Turn empty @district to nil
@district = nil_district(@district)
############ Set the District. If params[:district] is not nil, @district = params[:district]. If params[:district] is nil and @district is not nil params[:district] = @district
set_district
if params[:commit] == "Clear"
params[:q] = nil
end
@search = Awardunit.search(params[:q])
@awardunits = @search.result.paginate(page: params[:page], per_page: 20)
@unitlist = @search.result
@units = @unitlist.size
@disabledunits = @unitlist.where(disabled: true).size
@leaders = 0
@unitlist.each do |unit|
@leaders = @leaders + unit.awardleaders.size
end
@disabledleaders = 0
@unitlist.each do |unit|
@disabledleaders = @disabledleaders + unit.awardleaders.where(disabled: true).size
end
@asstleaders = 0
@unitlist.each do |unit|
@asstleaders = @asstleaders + unit.asstawardleaders.size
end
@assessors = 0
@unitlist.each do |unit|
@assessors = @assessors + unit.assessors.size
end
@approvedassessors = 0
@unitlist.each do |unit|
@approvedassessors = @approvedassessors + unit.assessors.where(approved: true).size
end
@members = 0
@unitlist.each do |unit|
@members = @members + unit.members.size
end
@disabledmembers = 0
@unitlist.each do |unit|
@disabledmembers = @disabledmembers + unit.members.where(disabled: true).size
end
@bronzemembers = 0
@unitlist.each do |unit|
@bronzemembers = @bronzemembers + unit.members.where(currentaward: "Bronze").size
end
@silvermembers = 0
@unitlist.each do |unit|
@silvermembers = @silvermembers + unit.members.where(currentaward: "Silver").size
end
@goldmembers = 0
@unitlist.each do |unit|
@goldmembers = @goldmembers + unit.members.where(currentaward: "Gold").size
end
respond_to do |format|
format.html
format.pdf do
pdf = MemberPdf.new(@search.result.order( 'nyaaid ASC' ), view_context, Awardunit.all.size)
send_data pdf.render, filename: "AwardUnits_List.pdf", type: "application/pdf", disposition: "inline"
end
end
end
<强> application_controller.rb 强>
def change_date_format(date)
if date.include? '/'
date_array = date.split("/")
new_date = date_array[1]+"/"+date_array[2]+"/"+date_array[0]
puts new_date
return new_date
else
return date
end
end