首页 > 解决方案 > 在 Rails 中显示查询结果时遇到问题

问题描述

我正在 Rails 中创建航班预订程序,但在显示符合条件的 Rails 航班的航班信息时遇到问题。用户可以选择往返机场、乘客和航班日期。如果航班存在,它会显示信息。但是我的航班没有显示。

显示航班的 index.html 表单

  <p>
  <% unless @eligible_flights.empty? %>
        <% @eligible_flights.each do |f| %>
          <%= f.start_airport_id %>
        <% end %>
  <% else %>
        <%= "No Flights Found" %>
  <% end %>
  </p>

飞行控制器查看

  def index
    @flights = Flight.all
    @eligible_flights = Flight.where("start_airport_id = ? AND end_airport_id = ? AND departure_time = ?", 
                                        params[:start_airport],
                                        params[:end_airport],
                                        params[:departure_time])
  end

标签: ruby-on-railsruby

解决方案


我最好的猜测是日期比较无法正常工作,您应该尝试使用 Rails 方式实现您的查询。

@eligible_flights = Flight
                     .where(start_airport_id: params[:start_airport])
                     .where(end_airport_id: params[:end_airport])
                     .where(departure_time: params[:departure_time])

推荐阅读