I've used this video http://railscasts.com/episodes/102-auto-complete-association-revised to set up an autocomplete search input in a form for my app. (The video may be for members only so I'll post my code as well. Essentially it searches a column of the DB (name) and autocompletes in a dropdown as you type. This all works fine however, what I'd like the form to do is submit the ID that correlates to the name, not the name itself.
I'm assuming theres no simple way to do this in just the view. Any help would be great. Code below, let me know if any other code would be helpful.
Thanks
Controller:
def game_name
game.try(:name)
end
def game_name=(name)
self.game = Game.find_by_name(name) if name.present?
end
Coffee:
jQuery ->
$('#play_game_name').autocomplete
source: $('#play_game_name').data('autocomplete-source')
In the view:
<%= f.label :game_name, "Search for a game" %>
<%= f.text_field :game_name, :class => "mlm mtm", data: {autocomplete_source: Game.order(:name).map(&:name)} %>