How do I make sure that I only load flights on search and not initial page load

Rails baby here! trying to work it out with Rails 7. I have a small toy app here that tries to search for flights then have users book a flight. I have managed to string together the app to a point of showing the flights and successful searches can be done. My problem is on initial page load, the page fills with flight which I only intend to have after a person has searched. Its possible to then search for specific flights but this behavior is a bug I want help to get fixed.

So here is what I have tried. I initially thought of separating the index from the search action methods, initialize @flights with an empty array then have the operation in search where @flights would get populated from the scoped filter_flights lambda in Flight Model. this only works to the extent of not loading index with all flights but when you then try to query, the params hash is empty and no query is generated(hope I am making sense). In short no search is actually carried out. I thought my paths were the issue and did as much ad specifying a get on the search action method in controllers to no avail. How can I solve for this this is my sample code:

Flight Model:

class Flight < ApplicationRecord
  belongs_to :departure_airport, class_name: 'Airport'
  belongs_to :arrival_airport, class_name: 'Airport'

  validates :departure_airport, :arrival_airport, :flight_date, presence: true

  scope :filter_flights, lambda { |departure_airport, arrival_airport, date, passengers|
    flights = all
    if departure_airport.present?
      flights = flights.joins(:departure_airport).where('flights.departure_airport_id = ?', departure_airport)
    end
    if arrival_airport.present?
      flights = flights.joins(:arrival_airport).where('flights.arrival_airport_id = ?', arrival_airport)
    end
    flights = flights.where('flight_date = ?', date) if date.present?
    flights.where('available_passenger_seats >= ?', passengers) if passengers.present?
  }
end

Controller:

class FlightController < ApplicationController
  def index
    @flight_dates = Flight.select(:flight_date).distinct.order(:flight_date)

    # @flights = Flight.includes(:departure_airport, :arrival_airport).order(:flight_date)

    @flights = Flight.filter_flights(params[:departure_airport_id],
                                     params[:arrival_airport_id],
                                     params[:flight_date],
                                     params[:number_of_passengers])
  end
end

and this is the head of my form:

<%= form_with model: @flight ,url: '/', method: :get do |form| %>

To then make sure I get the things I am looking for I was checking first if @flights.present? then display flights accordingly.

I hope this clarified my problem and I can get help. I am learning.

I initially thought of separating the index from the search action methods, initialize @flights with an empty array then have the operation in search where @flights would get populated from the scoped filter_flights lambda in Flight Model. this only works to the extent of not loading index with all flights but when you then try to query, the params hash is empty and no query is generated(hope I am making sense). In short no search is actually carried out. I thought my paths were the issue and did as much ad specifying a get on the search action method in controllers to no avail.

New contributor

Taku Mapfumo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

2

In the index only add flights to @flights when there is something in params otherwise set it as empty.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật