使用搜索逻辑的日期条件

发布于 2024-08-13 07:19:17 字数 896 浏览 5 评论 0原文

Rails 插件 - Searchlogic,来自二进制逻辑 - 提供按日期过滤的功能。我的表单设置如下...

<% form_for @search do |f| %>
    <%= f.label :start %> <%= f.select :due_at_after, [[['','']],['November', '2009-11-01'.to_date],['December', '2009-12-01'.to_date]]  %><br>
    <%= f.label :end %> <%= f.select :due_at_before, [[['','']],['December', '2009-12-01'.to_date],['January', '2010-01-01'.to_date]]  %>
    <%= f.submit 'Search' %>
<% end %>

但是,我从控制器中生成的 @search 对象返回的日期...

@search = Task.with_member(current_user).search(params[:search])

正在生成一个如下所示的日期参数

{:due_at_after=>Sat, 31 Oct 2009 20:00:00 EDT -04:00}

使用这两种格式,表单将不显示所选的下拉菜单。看起来 searchlogic 对象中的格式也使用时区调整时间。

关于如何处理这个问题有什么想法吗?

The Rails plugin - Searchlogic, from binary logic - offers the ability to filter by date. I have my form set up like so...

<% form_for @search do |f| %>
    <%= f.label :start %> <%= f.select :due_at_after, [[['','']],['November', '2009-11-01'.to_date],['December', '2009-12-01'.to_date]]  %><br>
    <%= f.label :end %> <%= f.select :due_at_before, [[['','']],['December', '2009-12-01'.to_date],['January', '2010-01-01'.to_date]]  %>
    <%= f.submit 'Search' %>
<% end %>

But, the date I'm getting back from the @search object as generated in the controller...

@search = Task.with_member(current_user).search(params[:search])

is generating a date param that looks like this

{:due_at_after=>Sat, 31 Oct 2009 20:00:00 EDT -04:00}

With the two formats, the form will not show the drop-down as selected. It looks as if the format in the searchlogic object uses a timezone adjusted time, too.

Any thoughts on how to handle this?

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

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

发布评论

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

评论(2

若沐 2024-08-20 07:19:17

您的问题的很大一部分似乎正在发生,因为您正在将字符串转换为日期,然后将日期转换回字符串。我相信你可能做得比你需要的更多。

HTML 表单并不真正“理解”日期——它们只是“理解”字符串。所以可以向它们传递字符串而不是日期。换句话说,删除to_date就可以了。

<% form_for @search do |f| %>
    <%= f.label :start %>
    <%= f.select :due_at_after,
          ['November', '2009-11-01'],['December', '2009-12-01']],
          :include_blank => true
    %>
    <br/>
    <%= f.label :end %>
    <%= f.select :due_at_before,
          [['December', '2009-12-01'],['January', '2010-01-01']],
          :include_blank => true
    %>
    <%= f.submit 'Search' %>
<% end %>

另外,我更喜欢使用 :include_blank => true 而不是 [['','']] (在我看来,更容易理解),并且我使用了封闭的
code> 标签(标准 html 内容 - 也许你打错了?)。

顺便说一下,如果你想指定一个 Date,你可以使用 Date 构造函数。与创建字符串并从中解析日期相比,它的编写时间更短,执行速度更快。

#Date(2009,11,1) is faster, shorter, and equivalent
Date(2009,11,1) == '2009-11-01'.to_date # --> true

A big part of your issue seems to be happening because you are converting strings into dates, and dates back to strings. I believe you might be doing it more than you need to.

HTML forms don't really "understand" dates - they just "understand" strings. So it is ok to pass them strings instead dates. In other words, it is ok to remove the to_date.

<% form_for @search do |f| %>
    <%= f.label :start %>
    <%= f.select :due_at_after,
          ['November', '2009-11-01'],['December', '2009-12-01']],
          :include_blank => true
    %>
    <br/>
    <%= f.label :end %>
    <%= f.select :due_at_before,
          [['December', '2009-12-01'],['January', '2010-01-01']],
          :include_blank => true
    %>
    <%= f.submit 'Search' %>
<% end %>

Also, I prefer using :include_blank => true instead of [['','']] (more human-readable, in my opinion), and I used a closed <br/> tag (standard html stuff - maybe you made a typo?).

By the way, if you want to specify a Date, you can use a Date constructor. It is shorter to write and faster to execute than creating a string and parsing a date from it.

#Date(2009,11,1) is faster, shorter, and equivalent
Date(2009,11,1) == '2009-11-01'.to_date # --> true
偏闹i 2024-08-20 07:19:17

您想要查找指定月份的记录吗?请查看我的 by_star gem/plugin 。

Are you looking to find records for a given month? Check out my by_star gem/plugin for this.

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