This helper uses javascript calendar from http://www.mattkruse.com/javascript/calendarpopup/
This helper is merely encapsulation that lets you have 4 lines of code that would incorporate text_field,link_to and actual popup calendar.
download CalendarPopUp.js to your public/javascripts directory
<%= javascript_include_tag "CalendarPopup.js" %>
<SCRIPT LANGUAGE="JavaScript">
var cal = new CalendarPopup();
</SCRIPT>
Either copy the following on the controller level app/helpers/controler_name_herlper.rb or in a global app/helpers/application_helper.rb
def calendar_js_tag(opt_for_txt={},opt_for_url={})
calHelper = %()
calHelper << %(<table><tr>)
calHelper << %(<td>#{text_field opt_for_txt[:obj_name],opt_for_txt[:attr_name],
:value => convert_date(opt_for_txt[:data]),:style => "width:100px"}</td>\n)
url_java_script_part = "cal.select(document.forms['#{opt_for_url[:form_name]}'].#{opt_for_txt[:obj_name]}_#{opt_for_txt[:attr_name]}," + "'#{opt_for_url[:anchor]}','#{opt_for_url[:format]}')"
calHelper << %(<td>#{link_to_function "select",
url_java_script_part,
{:name => opt_for_url[:anchor],:id => opt_for_url[:anchor]}}</td>)
calHelper << %(</tr></table>)
end
def convert_date(date)
return date.strftime("%m/%d/%Y") if !date.nil?
return nil if date.nil?
end
In case You want image for the link
def calendar_js_tag(opt_for_txt={},opt_for_url={})
calHelper = %()
calHelper << %(<table><tr>)
calHelper << %(<td>#{text_field opt_for_txt[:obj_name],opt_for_txt[:attr_name],
:value => convert_date(opt_for_txt[:data]),
:style => "width:100px"}</td>\n)
url_java_script_part = "cal.select(document.forms['#{opt_for_url[:form_name]}'].#{opt_for_txt[:obj_name]}_#{opt_for_txt[:attr_name]}," +
"'#{opt_for_url[:anchor]}','#{opt_for_url[:format]}')"
calHelper << %(<td>#{link_to_function image_tag("calendar",:border => "0"),
url_java_script_part,
{:name => opt_for_url[:anchor],:id => opt_for_url[:anchor]}}</td>)
calHelper << %(</tr></table>)
end
Note: name for this form will correspond to the calendar_helper Also: anchor should differ for each field, this will signify to javascript where to show the calendar.
<%= form_tag( {:action => "update_documentation_contract"},{:name => "isda_form"}) %>
<%= calendar_js_tag(
{:obj_name => 'isda_ma',:attr_name => 'linkLetterDate',:data => @isda_ma.linkLetterDate},
{:form_name => 'isda_form',:anchor => "anchor2",:format => 'MM/dd/yyyy'}
)%>
<%= end_form_tag %>
def documentation_contract
@legal_entity = LegalEntity.find(session[:legal_entity_id])
@isda_ma = find_or_new
@selected_sub_tab = "documentation_contract"
end
As always everything is refactorable, and as usual I appreciate your input.