简体   繁体   English

使用 Ransack gem 的 Rails 应用程序日期时间搜索

[英]Rails app datetime search using Ransack gem

I was about to implement a search function where users can filter a datetime column.我正要实现搜索 function ,用户可以在其中过滤日期时间列。

I am currently using rails datetime_field_tag however I can't seem to make it work with ransack where people can choose a date with time in hours and minutes then either AM or PM.我目前正在使用 rails datetime_field_tag但是我似乎无法使其与 ransack 一起使用,人们可以选择以小时和分钟为单位的日期,然后是 AM 或 PM。

Users can enter datetime in this format: dd/mm/yyyy hh:mm AM/PM用户可以按以下格式输入日期时间: dd/mm/yyyy hh:mm AM/PM

The column that i am searching for is in datetime format, ex: 2021-05-12 09:09:48我正在搜索的列是日期时间格式,例如: 2021-05-12 09:09:48

What's the best way to implement this one?实现这个的最佳方法是什么? with ransack or not?洗劫与否? and how?如何?
just my first time implementing datetime search function with ransack,只是我第一次用搜查实现日期时间搜索 function,

I tried searching online but i cant seem to find a good solution.我尝试在线搜索,但似乎找不到好的解决方案。 thanks.谢谢。

If you're receiving the datetime in string format from parameters and directly passing it to ransack, then you should convert that string to 24-hour format string (db format can also be used) first and then pass it to ransack.如果您从参数中接收字符串格式的日期时间并直接将其传递给 ransack,则应先将该字符串转换为 24 小时格式字符串(也可以使用 db 格式),然后将其传递给 ransack。

For example例如

query_value = Time.zone.parse(params[:created_at_filter]).to_s(:db)
Person.ransack(created_at_eq: query_value)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM