Search code examples
ruby-on-railsrubyrestmodel-view-controllerrubygems

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.


Solution

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