简体   繁体   中英

Rails app datetime search using Ransack gem

I was about to implement a search function where users can filter a datetime column.

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.

Users can enter datetime in this format: 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

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,

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.

For example

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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