在 Rails 中按日期范围生成报告

发布于 2024-07-06 22:12:55 字数 126 浏览 7 评论 0 原文

您将如何根据用户在 Rails 应用程序中选择的日期范围生成报告? 最好的日期范围选择器是什么?

编辑回应帕特里克:我正在寻找一些小部件和活动记录建议,但我真正好奇的是如何根据用户选择的日期平静地显示日期范围列表。

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝天白云 2024-07-13 22:12:55

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

日期选择小部件

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

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

3) 使 Javascript 小部件适应 Rails:集成像 Yahoo UI 库 (YUI) 这样的东西几乎是微不足道的 日历,全部是 Javascript,到 Rails。 从 Rails 的角度来看,这只是填充 params[:start_date] 和 params[:end_date] 的另一种方式。 YUI 日历对范围有原生支持。

从小部件获取数据

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 是一个严肃的魔法。 Rails 将为您初始化 params[:some_form_element_name] 。

#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

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

Date Picking Widgets

1) Default Rails Solution: See date_select documentation here.

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) 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.

Getting the data from the Widgets

1) Default Rails Solution See date_select documentation here.

#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: Rather similar to the above, just with sexier visible UI.

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])

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
南街女流氓 2024-07-13 22:12:55

让 URL 参数控制所选记录的范围并不是一种非 RESTful 做法。 在索引操作中,您可以按照 Patrick 建议执行以下操作:

  #initialize start_date and end_date up here, by pulling from params probably
  @models = SomeModel.find(:all, :conditions => ['date >= ? and date <= ?', params[:start_date], params[:end_date]])

然后在索引视图中创建一个表单,将 ?start_date=2008-01-01&end_date=2008-12-31 添加到 URL。 请记住,它是用户提供的输入,因此请小心对待。 如果您在索引操作中将其放回到屏幕上,请务必按如下方式操作:

Showing records starting on 
<%= h start_date %> 
and ending on 
<%= h end_date %>

It's not an unRESTful practice to have URL parameters control the range of selected records. In your index action, you can do what Patrick suggested and have this:

  #initialize start_date and end_date up here, by pulling from params probably
  @models = SomeModel.find(:all, :conditions => ['date >= ? and date <= ?', params[:start_date], params[:end_date]])

Then in your index view, create a form that tacks on ?start_date=2008-01-01&end_date=2008-12-31 to the URL. Remember that it's user-supplied input, so be careful with it. If you put it back on the screen in your index action, be sure to do it like this:

Showing records starting on 
<%= h start_date %> 
and ending on 
<%= h end_date %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文