On my events index page, I have a search form that filters the records on the index page based on those search parameters through Ransack.
The search form currently works only when the dance styles and/or event types (both of which are arrays) have one value each, however I would like to be able to search by multiple values of both.
Here's my search form for the dance style and event types fields:
<div class="dancestyle">
<%= f.label "Dance style", class: "form-label" %>
<%= f.select(:dance_styles_id_eq, DanceStyle.all.pluck(:name, :id), { :include_blank => "All dance styles" }, { class: "form-select", id: "select-dancestyle", multiple: true, placeholder: "Any dance style" }) %>
</div>
<div class="eventtype">
<%= f.label "Event Type", class: "form-label" %>
<%= f.select :event_type_id_eq, EventType.all.pluck(:name, :id), { :include_blank => "All event types" }, { id: "select-artist", placeholder: "Any event type", class: "form-select" } %>
</div>
Here's my Event controller index:
def index
if params[:q]
params[:q][:combinator] = "and"
params[:q][:groupings] = []
split_geo = params[:q][:address_or_city_or_state_or_country_or_continent_cont_all].split(/(,\s)+/)
split_geo.map! do |a|
I18n.transliterate a
end
split_geo.each_with_index do |word, index|
params[:q][:groupings][index] = { address_or_city_or_state_or_country_or_continent_cont_all: word }
end
end
@q = Event.ransack(params[:q])
@events = @q.result(distinct: true)
.where(event_status_id: 1)
end
end
Here's the full stack error I'm getting when I set the input fields to allow multiple and search multiple dance styles:
14:35:32 web.1 | Started GET "/events?q%5Bname_cont%5D=&q%5Baddress_or_city_or_state_or_country_or_continent_cont_all%5D=&q%5Bdance_styles_id_eq%5D%5B%5D=&q%5Bdance_styles_id_eq%5D%5B%5D=5&q%5Bdance_styles_id_eq%5D%5B%5D=6&q%5Bevent_type_id_eq%5D=&q%5Bevent_month_eq%5D=&q%5Bevent_year_eq%5D=&commit=Search%21" for ::1 at 2022-10-14 14:35:32 -0500
14:35:32 web.1 | Processing by EventsController#index as HTML
14:35:32 web.1 | Parameters: {"q"=>{"name_cont"=>"", "address_or_city_or_state_or_country_or_continent_cont_all"=>"", "dance_styles_id_eq"=>["", "5", "6"], "event_type_id_eq"=>"", "event_month_eq"=>"", "event_year_eq"=>""}, "commit"=>"Search!"}
14:35:32 web.1 | Completed 500 Internal Server Error in 3ms (ActiveRecord: 0.0ms | Allocations: 2335)
14:35:32 web.1 |
14:35:32 web.1 |
14:35:32 web.1 |
14:35:32 web.1 | NoMethodError (undefined method `to_i' for ["", "5", "6"]:Array
14:35:32 web.1 | Did you mean? to_s
14:35:32 web.1 | to_a
14:35:32 web.1 | to_h):
14:35:32 web.1 |
14:35:32 web.1 | app/controllers/events_controller.rb:38:in `index'
How does the to_i
tie into this? I'm assuming this fix will need to be handled in my events controller.