javascriptruby-on-railsrubyransackgeocomplete

Search function error if search term is too long Unsupported argument type: 0 (Integer)


My search form works great except if the search terms are too long, I dont know why this happens. Im using ransack for search. I got this for example:

    Started GET "/search?search=Via+Per+Trognano%2C+Riozzo%2C+Metropolitan+City+of+Milan%2C+Italy&start_date=&end_date=&commit=Search" for ::1 at 2020-07-21 17:43:25 +0200
Processing by PagesController#search as HTML
  Parameters: {"search"=>"Via Per Trognano, Riozzo, Metropolitan City of Milan, Italy", "start_date"=>"", "end_date"=>"", "commit"=>"Search"}
Completed 500 Internal Server Error in 228ms (ActiveRecord: 0.0ms | Allocations: 2027)



ArgumentError (Unsupported argument type: 0 (Integer)):

app/controllers/pages_controller.rb:14:in `search'

As u can image my form looks for a car adress

car table:

t.string "location"

search form

    <%= form_tag search_path, method: :get do %>
        <%= text_field_tag :search, params[:search], placeholder: "Where are you going?", class: "form-control" , id:"autolocation"%>
         <%= submit_tag "Search", class: "btn btn-normal btn-block" %>
 
<% end %>

pages_controller

class PagesController < ApplicationController

      def home
        @cars = Car.where(active: true).limit(3)
      end
    
      def search
        if params[:search].present? && params[:search].strip !=""
          session[:loc_search] = params[:search]
        end
    
    arrResult = Array.new
    
    if session[:loc_search] && session[:loc_search] != ""
         @cars_address = Car.where(active: true).near(session[:loc_search], 5, order: 'distance')
       else
         @cars_address = Car.where(active: true).all
       end
    
    @search = @cars_address.ransack(params[:q])
    @cars = @search.result
    
    @arrCars = @cars.to_a
    
    
  

search.js

$('#car_listing').html('<%= j render partial: "cars/cars_list", locals: {cars: @arrCars} %>')
initialize(<%= raw @arrCars.to_json %>)

Solution

  • From https://github.com/activerecord-hackery/ransack#usage

    The default param key for search params is now :q, instead of :search. This is primarily to shorten query strings, though advanced queries (below) will still run afoul of URL length limits in most browsers and require a switch to HTTP POST requests. This key is configurable.

    you need to switch to post method