2011-10-28 47 views
5

我在Rails 3.1中使用了Ransack Gem。一切都按預期工作。但是,我有一個搜索表單,它將查找使用a Soundex column的人員,我遇到問題。Rails - 如何在Ransack搜索表單中不評估字段

在我的表單中,我有一個「given_name_soundex_cont」字段,用戶將輸入名稱「Joe」。我的控制器將「joe」轉換爲soundex代碼,然後Ransack在「given_name_soundex」列中查找匹配項。

所有匹配的記錄被返回並且看起來很好,除了用戶輸入單詞「joe」的字段現在顯示soundex值(當然,因爲我改變了參數)。

所以我想我可以只添加一個「given_name_cont」字段並隱藏「given_name_soundex_cont」字段,但現在Ransack希望在查詢中包含「given_name_cont」,這是我不想要的。

有誰知道我可以如何在沒有Ransack的Ransack搜索表單中包含字段實際評估它們。這樣我可以指定哪些字段不被評估,哪些不是。

如果有幫助,這是我在我的控制器:

def index 

    if !params[:q].blank? # nil.blank? and [].blank? are true 
    search_params = params[:q] 
    search_params[:given_name_soundex_cont] = convert_to_soundex(search_params[:given_name_soundex_cont]) 
    @q = Person.search(search_params) 
    @results = @q.result.limit(50) 
    else 
    @q = Person.search(params[:q]) 
    @results = [] 
    end 

end 


private 
    def convert_to_soundex(text) 
     Text::Soundex.soundex(text.to_s) 
    end 

在我看來:

<%= search_form_for @q do |f| %> 
    <label>Given Name:</label> 
    <%= f.text_field :given_name_soundex_cont %> 

    <%= f.submit %> 
<% end %> 

回答

0

你可以重複使用params[:q][:given_name_soundex]在視圖中覆蓋傳遞的值#ransack/#search

1

創建一個格式化參數的racksacker。我沒有嘗試過,但我認爲它會起作用(通過https://github.com/ernie/ransack/issues/36)。

ransacker :given_name_soundex, :formatter => proc {|v| Text::Soundex.soundex(v)} do 
    parent.table[:given_name_soundex] 
end 
0

我想實現類似的東西,那就是,通過SQL通配符%替換 客戶輸入空格,以便搜索領域的「富酒吧」將 匹配「富酒吧」,也有「 Foomster Q. Bar「和」Foo Glolubar「。我還希望 將通配符添加到實際搜索參數的開頭和結尾,因此012ff即「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(在 span4span8btn btn-large btn-primary標記由 Twitter's Bootstrap framework提供)視圖的相關部分:

<% 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> 
     <br/> 
     <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_anythingcustomer_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是 加載。)