按日期范围生成报告 [英] Generating a report by date range in rails

查看:120
本文介绍了按日期范围生成报告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您将如何在Rails应用中按用户选择的日期范围生成报告?最佳日期范围选择器是什么?

How would you go about producing reports by user selected date ranges in a rails app? What are the best date range pickers?

针对patrick的编辑:我正在寻找一些小部件和活动记录建议,但我真正好奇的是如何彻底显示日期范围列表根据用户选择的日期。

edit in response to patrick : I am looking for a bit of both widget and active record advice but what I am really curious about is how to restfully display a date ranged list based on user selected dates.

推荐答案

我们是在这里询问接口问题(即,您想要一个小部件)还是ActiveRecord问题?

Are we asking an interface question here (i.e. you want a widget) or an ActiveRecord question?

1)默认Rails解决方案:请参见 date_select 文档此处

2)使用插件:为什么要编写代码?我个人喜欢 CalendarDateSelect 插件,在需要范围时使用一对吸盘。

2) Use a plugin : Why write code? I personally like the CalendarDateSelect plugin, using a pair of the suckers when I need a range.

3)使Javascript小部件适应Rails :集成Yahoo UI库(YUI)日历(全是Javascript)到Rails。从Rails的角度来看,这是填充 params [:start_date] params [:end_date] 的另一种方法。 YUI日历具有对范围的本地支持。

3) Adapt a Javascript widget to Rails: It is almost trivial to integrate something like the Yahoo UI library (YUI) Calendar, which is all Javascript, to Rails. From the perspective of Rails its just another way to populate the params[:start_date] and params[:end_date]. YUI Calendar has native support for ranges.

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。

2) CalendarDateSelect: Rather similar to the above, just with sexier visible UI.

3)适应Javascript小部件:通常,这意味着某些表单元素会将日期输入作为字符串。对您而言,这是个好消息,因为Date.parse是一些严肃的魔术。 Rails将为您初始化params [:some_form_element_name]。

3) Adapt a Javascript widget: Typically this means that some form element will have the date input as a string. Great news for you, since Date.parse is some serious magic. The params[:some_form_element_name] will be initialized by Rails for you.

#goes in controller.  Please handle errors yourself -- Javascript != trusted input.
report_start_date = Date.parse(params[:report_start_date])



写对ActiveRecord的调用



简单易用。

Writing the call to ActiveRecord

Easy as pie.

  #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

这篇关于按日期范围生成报告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆