Rails: How do I autocomplete search for Name but save ID?

前端 未结 2 747
难免孤独
难免孤独 2021-02-02 03:26

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 mem

2条回答
  •  离开以前
    2021-02-02 04:08

    By the http://api.jqueryui.com/autocomplete/#option-source you can set data to the autocomplete.

    You need to change the text_field line like:

    <%= f.text_field :game_name, :class => "mlm mtm", data: {autocomplete_source: Game.order(:name).map { |t| { :label => t.name, :value => t.id } } %>
    

    For more advanced features consider to use select2 or chosen.

提交回复
热议问题