我的搜索表单效果很好,除非搜索词太长,我不知道为什么会这样。我正在使用ransack进行搜索。例如,我得到了这个:
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'
如您所见,我的表格寻找汽车地址
车表:
t.string "location"
搜索表格
<%= 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
搜索.js
$('#car_listing').html('<%= j render partial: "cars/cars_list", locals: {cars: @arrCars} %>')
initialize(<%= raw @arrCars.to_json %>)