Как правильно получить параметры от помощника select_date? Моя версия очень надумана и функционирует. Даты, которые проходят через параметры, не имеют префикса 0. Итак, январь - 1, и мне нужно, чтобы он был 01. Таким образом, когда я ищу в БД, я получаю январь, а не ноябрь.
Мой код контроллера ниже.
def daterange
if params[:start_dateCommence] && params[:end_dateCommence]
start_date=params[:start_dateCommence] #params from view can be accessed individually as params[:start_date][:month]
end_date = params[:end_dateCommence] #params from view
start=start_date.values_at("year","month","day") #creates an array of the values Y M D for the end range
endd=end_date.values_at("year","month","day") #creates an array of the values Y M D for the end range
startStr=""
endStr=""
for e in start # for each in start array create a string in the format yyyy-mm-dd
if e.length <2
e="0"+e #this is required so days and months have prepending 0'
end
startStr=startStr+e+"-"
end
for e in endd
if e.length<2
e="0"+e
end
endStr=endStr+e+"-"
end
@startStr=startStr.chop #chops off "-" at end of string
@endStr=endStr.chop
if ($startStr || $endStr)!="0-0-0"
@leases=Lease.find_by_sql ["SELECT * FROM leases WHERE commencement_date >= ? AND commencement_date <= ? ORDER BY commencement_date ASC",$startStr ,$endStr]
end
end
и мой код просмотра
<% form_tag ({:controller => 'leases', :action => "daterange"}) do %><h4>Commencement Date:</h4>
Start:<%= select_date nil, :prefix=>:start_dateCommence, :include_blank => true,:order => [:year,:month, :day]%>
End:<%= select_date nil ,:prefix => :end_dateCommence,:include_blank => true,:order => [:year,:month, :day]%>
<%= submit_tag("Reset", { :name => 'reset', :id => 'reset_button', :type => "reset" }) %>
<%= submit_tag "Submit", :disable_with => "Submitting..." %>
Я использую рельсы 2.3.10 и ruby 1.8.7. Опять же, это работает, но далеко от идеального или идиоматического.