您将如何在rails应用中按用户选择的日期范围生成报告?什么是最好的日期范围采摘者?
编辑以响应帕特里克:我正在寻找一些小部件和活动记录建议,但我真正好奇的是如何根据用户选择的日期安排显示日期范围列表.
我们在这里问一个界面问题(即你想要一个小部件)还是一个ActiveRecord问题?
日期选择小部件1) 默认Rails解决方案:请参阅此处的date_select
文档.
2) 使用插件:为什么要编写代码?我个人喜欢CalendarDateSelect插件,当我需要一个范围时,使用一对吸盘.
3) 将Javascript小部件调整为Rails:将类似Yahoo UI库(YUI)日历(全部是Javascript )的东西集成到Rails中几乎是微不足道的.从Rails的角度来看,它只是另一种填充params[:start_date]
和params[:end_date]
.YUI Calendar具有对范围的本机支持.
1) 默认Rails解决方案 请参阅此处的date_select
文档.
#an application helper method you'll find helpful #credit to http://blog.zerosum.org/2007/5/9/deconstructing-date_select # Reconstruct a date object from date_select helper form params def build_date_from_params(field_name, params) Date.new(params["#{field_name.to_s}(1i)"].to_i, params["#{field_name.to_s}(2i)"].to_i, params["#{field_name.to_s}(3i)"].to_i) end #goes into view <%= date_select "report", "start_date", ... %> <%= date_select "report", "end_date", ... %> #goes into controller -- add your own error handling/defaults, please! report_start_date = build_date_from_params("start_date", params[:report]) report_end_date = build_date_from_params("end_date", params[:report])
2) CalendarDateSelect:与上面的相似,只是更加性感的可见UI.
3) 调整Javascript小部件:通常这意味着某些表单元素将日期输入作为字符串.对你来说是个好消息,因为Date.parse是一个严重的魔术.params [:some_form_element_name]将由Rails为您初始化.
#goes in controller. Please handle errors yourself -- Javascript != trusted input. report_start_date = Date.parse(params[:report_start_date])将调用写入ActiveRecord
非常简单.
#initialize start_date and end_date up here, by pulling from params probably @models = SomeModel.find(:all, :conditions => ['date >= ? and date <= ?', start_date, end_date]) #do something with models