はじめに railsを使ったウェブアプリケーションで、テキストフィールドにオートコンプリートをつけたいときに、rails4-autocompleteというgemを使うと便利。基本的な使い方はこちらの参考URLを参照。 このgemを使った際にオートコンプリートのフィールドの挙動をカスタマイズする方法を記録します。 環境 ruby 2.2.1 rails 4.2.3 前提 今回の実装例は、 外部キー「fuga_id」を持つ「hogeモデル」のフォームから オートコンプリートを使って「fugasテーブル」を細かい条件で検索し オートコンプリートが提案する文言も変更したうえでfuga_idをsubmitする場合 を想定。gemによって作られるアクションを下記のようにオーバーライドして実装すると柔軟に対応できる。 実装手順