同様のことを達成したかった、つまり、
検索フィールド "Foo Bar"がSQLのワイルドカード%
"Foo Bar"にマッチするだけでなく、 "Foomster Q. Bar"や "Foo Glolubar"と一致します。私も望んでいた
実際の検索パラメータの先頭と末尾にワイルドカードを追加する
その "Fiefoo Newton Barrister"もマッチするだろう。
方法は次のとおりです。 app/controllers/customers_controller.rb
の関連部分:
class CustomersController < ApplicationController
# GET /customers
# GET /customers.json
def index
@search = Customer.search(params[:q])
@customers = @search.result
...
respond_to do |format|
format.html # index.html.erb
format.json { render json: @customers }
end
end
...
end
app/views/customers/index.html.erb
ビューの関連部分(
span4
、 span8
および btn btn-large btn-primary
マークアップは
TwitterのBootstrapフレームワーク):
<% require 'action_view' %>
<div class="row">
...
<div class="span4"> <!-- The search criteria -->
...
<%= search_form_for @search do |f| %> <!-- this is a Ransack-provided form -->
<div class="field">
<%= f.label :customer_name_whitespaces_match_anything, "Name is or contains:" %>
<%= f.text_field :customer_name_whitespaces_match_anything, class: "my-textbox" %>
...
<%= f.label :customer_address_whitespaces_match_anything, "Address is or contains:" %>
<%= f.text_field :customer_address_whitespaces_match_anything, class: "my-textbox" %>
...
</div>
<div class="actions">
<%= f.submit "Search", class: "btn btn-large btn-primary" %>
</div>
<% end %>
</div>
<div class="span8"> <!-- The search results -->
...
<table border="1" cellpadding="5">
<tr>
<th><%= sort_link(@search, :customer_name, "Customer name") %></th>
...
<th><%= sort_link(@search, :customer_address, "Address") %></th>
...
</tr>
<% @customers.each do |c| %>
<tr>
<td><%= link_to c.customer_name, customer_path(c, search: @search) %></td>
...
<td><%= c.customer_address %></td>
...
</tr>
<% end %>
</table>
</div>
...
</div>
検索述語 customer_name_whitespaces_match_anything
に気づくでしょう。
customer_address_whitespaces_match_anything
をクリックします。これらは、カスタム
config/initializers/ransack.rb
ファイルで定義した検索述語です。その
内容は次のとおりです。
Ransack.configure do |config|
config.add_predicate 'whitespaces_match_anything',
:arel_predicate => 'matches', # so we can use the SQL wildcard "%"
# Format the incoming value: replace spaces by the SQL wildcard "%".
:formatter => proc {|v| "%"+v.gsub(" ","%")+"%"}
end
検索語は、SQLに与えられる前にカスタム述語によって掘り起こされます
クエリの後に表示されますが、検索後、検索フォームの入力フィールドにはまだ
ユーザの入力を最初に検索します。
(検索結果には、顧客の名前から個人へのリンクがあります
app/views/customers/show.html.erb
というビューを表示させる
ロードされます。)