Search code examples
ruby-on-railssimple-form

search function not working - Ruby on rails


I'm new to Ruby on Rails and trying to implement a search function with simple form to filter out availabilities results. After I press search button, it still stays on the same page and not running the search function in the controller. The permitted is false. I'm wondering why this might occur. Is there anything wrong with the search_params I wrote? Rails version is 6.0.2.1.

views/availabilities/_searchform.html.erb

<form>
    <div id="aDiv">
        <%= simple_form_for :search, :url => search_rides_result_path, :method => :get do |f| %>
        <div class="form-row">
            <div class="form-group col-md-2">
                <%= f.input :start_city, label: 'Start City', class: "form-control", error: 'Start address is mandatory, please specify one' %>
            </div>
            <div class="form-group col-md-4">
                <%= f.input :start_street_address, label: 'Start Street Address', class: "form-control" %>
            </div>
        </div>
        <div class="form-row">
            <div class="form-group col-md-2">
                <%= f.input :end_city, label: 'Destination City', class: "form-control" %>
            </div>
            <div class="form-group col-md-4">
                <%= f.input :end_street_address, label: 'Destionation Street Address', class: "form-control" %>
            </div>
        </div>
        <div class="form-row">
            <div class="form-group col-md-4">
                <%= f.input :trip_time, as: :datetime, inline_label: 'Yes, remember me', class: "form-control" %>
            </div>
            <div class="form-group col-md-2">
                <%= f.input :lowest_acceptable_price, label: 'Expected Price', class: "form-control" %>
            </div>
        </div>
            <%= f.submit "Search", class: "btn btn-primary" %>
        <% end %>
    </div>
</form>

routes.rb file.

  get 'search_rides', to: "availabilities#index"
  get 'search_rides_result', to: "availabilities#search"

availabilities_controller.rb

  def index
    @availabilities = Availability.unmatched
  end

  def search
    @availabilities = Availability.unmatched.search(search_params[:start_city])
  end

  def search_params
    params.require(:search).permit(:start_city, :start_street_address, :end_city, :end_street_address, :trip_time, :lowest_acceptable_price)
  end

models/availability.rb

class Availability < ApplicationRecord
    has_many :users, :through => :posts
    has_one :request
    scope :unmatched, ->{ where(matched_request_id: -1) }

    def self.search (params)
        puts "start searching"
        results = city_matches(start_city, end_city, params[:start_city], params[:end_city]) 
        return nil unless results
        results
    end

Solution

  • Instead of using 2 different methods for search, try combining both in index method. Your index method will now look as follows:

    def index
      if params[:search]
        @availabilities = Availability.unmatched.search(search_params[:start_city])
      else
        @availabilities = Availability.unmatched
      end
    end
    

    Change the form url to search_rides_path. This way search results will be rendered in same view upon form submission.