I realize this is not possible in the way my question is worded, but is there a way to still get the outcome I'm looking for.
In a Rails 3.2 app I have a form with City and Location fields:
<%= f.select :city, City.all, :prompt => "Select..." %>
<%= f.input :location_name, :input_html => { data:{ autocomplete_source: Location.order(:name).map(&:name) } } %>
The location field uses Twitter Bootstrap's typeahead function to display an autocomplete list of entries.
A Location belongs to a City. I want to filter the list of autocomplete entries based on the City value selected by the user.
Basically, I need to dynamically change
... autocomplete_source: Location.order(:name).map(&:name) ...
to
... autocomplete_source: Location.where('city_id => ?', '@myform.city.id').order(:name).map(&:name) ...
In coffeescript I could do something like:
jQuery ->
$('#myform_city_id').change ->
$('#myform_location_name').attr('data-autocomplete-source','#{Location.where(city_id: @myform.city_id).order(:name).map(&:name)}')
But the active record query is not called, it simply gets passed in as a string.
What is the best way to dynamically change this autocomplete-source attribute with the new query?