Hi there

You can pass multiple params to JQuery Autocomplete with Rails

I searched around for a long time and couldn’t find a good and simple answer to this setup.
Here is what I worked out;

In JS/Coffeescript
$(“#id_of_field_that_needs_autocomplete_list").live "keyup.autocomplete", ->
variable = $(“#id_of_field_containing_addition_value_you_want_to_pass").val()
$(this).autocomplete
source: $('#id_of_field_that_needs_autocomplete_list-autocomplete-input').data('autocomplete-source')
autoFocus: true
return

In View
<%= f.text_field :description, data: {autocomplete_source: path_to_controller_action_containing_autocomplete_query_results}, class: 'autocomplete-container', id: ’this_field-autocomplete-input', :placeholder => ‘Enter some gibberish' %>

In Controller
def autocomplete_method_name
@autocomplete_array_variable = Model_name.where(“variable_name_column like ?", "%#{params[:variable]}%").where(“main_query_column_name like ?", "%#{params[:term]}%").uniq.pluck(:main_query_column_name)
render json: @autocomplete_array_variable
end

In Routes
resources :controller_name do
get :autocomplete_method_name, on: :collection
end


[I tried to make this context neutral, but it’s hard to separate everything out and make sure I included everything you would need to know without any context. If I missed anything, feel free to comment and ask, or email me.]