使用搜索逻辑的日期条件
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的问题的很大一部分似乎正在发生,因为您正在将字符串转换为日期,然后将日期转换回字符串。我相信你可能做得比你需要的更多。
HTML 表单并不真正“理解”日期——它们只是“理解”字符串。所以可以向它们传递字符串而不是日期。换句话说,删除
to_date
就可以了。另外,我更喜欢使用
:include_blank => true
而不是[['','']]
(在我看来,更容易理解),并且我使用了封闭的
code> 标签(标准 html 内容 - 也许你打错了?)。顺便说一下,如果你想指定一个 Date,你可以使用 Date 构造函数。与创建字符串并从中解析日期相比,它的编写时间更短,执行速度更快。
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
.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.
您想要查找指定月份的记录吗?请查看我的 by_star gem/plugin 。
Are you looking to find records for a given month? Check out my by_star gem/plugin for this.