ruby-on-rails - Rails:如何从搜索表单中获取日期
问题描述
所以,我有一个搜索表单,我试图从 url 中获取日期,但我没有得到它。
<%= form_with(url: "/flights", method: "get", local: true) do %>
<%= label_tag :from %>
<%= select_tag(:start_airport, options_for_select(@airport_options)) %> <br> <br>
<%= label_tag :to %>
<%= select_tag(:finish_airport, options_for_select(@airport_options)) %> <br> <br>
<%= label_tag :passengers %>
<%= select_tag(:passengers, options_for_select(@passengers)) %> <br> <br>
<%= select_tag(:date, options_for_select(@dates_options)) %> <br> <br>
<%= submit_tag("Search") %>
我想我必须改变日期,但是当我尝试
@flights = Flight.where(start_airport_id: params[:start_airport], finish_airport_id: params[:finish_airport], date: date: DateTime.strptime(params[:date], "%d %b %H:%M"))
我没有将 nil 隐式转换为 String 错误。
航班控制器.rb
def index
@airport_options = Airport.all.map { |a| [a.code, a.id] }
@passengers = (1..4).to_a
@dates_options = Flight.order('date ASC').pluck(:date).map { |date| date.strftime("%d %b %H:%M") }
@flights = Flight.where(start_airport_id: params[:start_airport], finish_airport_id: params[:finish_airport], date: params[:date])
end
我数据库中的日期格式为“Sat, 02 May 2020 22:21:00 UTC +00:00”。
在调试器中:--- !ruby/object:ActionController::Parameters 参数:!ruby/hash:ActiveSupport::HashWithIndifferentAccess
日期:5月2日22:21
行动:索引允许:假
解决方案
推荐阅读
- javascript - Typescript - 获取满足条件的数组
- python - Python 3.7.1 ANT+ TKinter - 在 Tkinter 中显示来自 ANT+ 设备的实时数据
- sql - 获取过去 6 个月的条目总和(不完整的月份)
- javascript - 如何通过 vue-cli 3 配置 webpack 以使用 sass
- php - Laravel 队列 - 作业之间暂停
- php - Symfony 致命错误:require():无法打开所需的 'C:\xampp\htdocs\app\cache\prod/doctrine/orm/Proxies\__CG__MediaBundleEntityMedia.php'
- php - symfony 4 - 所有页面仅用于记录
- javascript - Panel toggleDown 仅选中复选框。Javascript/jQuery
- neo4j - 匹配满足两个或多个关系 (AND) 关系的 Neo4j 节点的表达式
- fortran - Fortran 中子程序的有条件使用