我是一名有用的助手,可以为您进行文本翻译。
我有一个日期选择器,可将入住和退房日期与搜索框一起发送。此时URL看起来像这样:
http://localhost:3000/locations/listings?utf8=%E2%9C%93&search=london&start_date=12%2F04%2F16&end_date=20%2F04%2F16
然后我取出参数哈希并解析字符串,
start_date = Date.parse(params[:start_date])
end_date = Date.parse(params[:end_date])
首先,我需要检查
if (start_date.present? && end_date.present?)
,这个工作正常。但是,如果用户手动在URL中输入了日期之外的内容,比如:http://localhost:3000/locations/listings?utf8=%E2%9C%93&search=london&start_date=londoneye6&end_date=20%2F04%2F16
当然,我遇到了一个错误;
invalid date
我应该如何在控制器动作中控制字符串是否可解析。 我还应该检查包含- /
的字符串伦敦之眼,伦敦/眼
。
谢谢
nil
时仅捕获ArgumentError
异常。 - Малъ Скрылевъ