GitLab7.6.2の検索フォームでUTF-8の文字列を使う場合
問題
検索フォームでUTF-8のも文字列を使って検索してしまうと次のようなエラーが出てきます。
Encoding::CompatibilityError (incompatible encoding regexp match (UTF-8 regexp with ASCII-8BIT string)): app/models/repository.rb:162:in `method_missing' lib/gitlab/project_search_results.rb:51:in `blobs' lib/gitlab/project_search_results.rb:20:in `objects' app/controllers/search_controller.rb:33:in `show'
ASCII-8BITの文字列に対してUTF-8の文字列で比較してしまっていると怒られています。
(gitのwiki作成に用いられているgollum-libは、ASCII-8BITの文字列を扱っているらしいので)
解決策
app/controllers/search_controller.rbの変数termの行を変更しました。
def autocomplete # term = params[:term] term = params[:term].force_encoding('ASCII-8BIT') # ここの行を変更 @project = Project.find(params[:project_id]) if params[:project_id].present? @ref = params[:project_ref] if params[:project_ref].present? render json: search_autocomplete_opts(term).to_json end
params[:term]は検索フォームに入力された文字列なので、そのparams[:term]をforce_encodingでASCII-8BITにエンコーディングしてあげることで、UTF-8の文字列を入力した際にも強制的にASCII-8BITに変換して検索にかけることができるようになります。