Rails - Ransak qidiruv shaklidagi maydonni qanday baholaysiz

Raylardagi Ransak gemini foydalanayapman 3.1. Hamma narsa kutilganidek ishlaydi. Biroq, menda yashaydigan Soundex ustuni dan foydalanib, shaxsni qidirishga mo'ljallangan qidiruv varaqasi bor bilan bog'liq muammo.

Mening shaklimda "given_name_soundex_cont" maydoni bor, bu foydalanuvchi "Joe" nomini kiritadi. My controller "joe" ni soundex kodiga o'zgartirgandan ko'ra, keyin Ransack "given_name_soundex" ustunidagi o'yinni izlaydi.

Barcha mos keladigan yozuvlar qaytarib yuboriladi va foydalanuvchi joydagi "joe" so'zini kiritgan joy endi o'z o'rniga "soundex" qiymatini ko'rsatadi (albatta men paramni almashtirganim uchun).

Shuning uchun men "given_name_cont" maydonini qo'sha olaman va "given_name_soundex_cont" maydonini yashirishi mumkin, deb o'ylayman, lekin hozir Ransack so'ramaganimda "given_name_cont" ni qo'shishni xohlaydi, men istamasdim.

Ransak ularni aslida baholashsiz Ransak qidirish shaklidagi joylarni qanday qilib qo'shishim mumkinligini biladimi? Shunday qilib, men qanday maydonlarni baholash kerakligini belgilashim mumkin.

Agar bu men yordamchimga yordam bergan bo'lsa:

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

Va mening fikrimcha:

<%= search_form_for @q do |f| %>
  
  <%= f.text_field :given_name_soundex_cont %>

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

3 javoblar

Parametrni formatlaydigan ishlov beruvchi yaratish. Buni sinab ko'rmadim, lekin menimcha ishlaydi ( 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
1
qo'shib qo'ydi

#ransack / #search ga yuborilgan qiymatni bekor qilish uchun params [: q] [: given_name_soundex] -ni qayta ishlatishingiz mumkin.

0
qo'shib qo'ydi

Men shunga o'xshash biror narsaga erishmoqchi edim, ya'ni bo'sh joylarni almashtirdim SQL joker kodi % tomonidan mijozning kiritishi, shuning uchun "Foo Bar" qidiruv maydoni ishlatiladi "Foo Bar" bilan o'yinda, shuningdek, "Foomster Q. Bar" va "Foo Glolubar". Men ham xohladim Jadvalni haqiqiy qidirish parametrining boshiga va oxiriga qo'shish uchun "Fiefoo Nyuton Barrister" ham o'yin bo'lishi mumkin edi.

Mana qanday qilib. app/controllers/clients_controller.rb ning tegishli qismlari:

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 ko'rinishidagi tegishli qismlar ( span4 , span8 va btn btn-large btn-primary belgilarini taqdim etadi. Twitterning 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>
      
<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>

Qidiruv kodini customer_name_whitespaces_match_anything va customer_address_whitespaces_match_anything . Ular odatiga mos keladi config/initialisers/ransack.rb faylida aniqlagan qidiruv predicate. Uning tarkibi:

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

Endi qidiruv so'zi SQLga berilishdan oldin, odatdagidek, predikat tomonidan munglanadi so'rovlar, ammo qidirishdan so'ng, qidiruv formasining kirish joylari hali ham ko'rsatiladi qidiruvni dastlabki tarzda foydalanuvchi kiritish.

(Qidiruv natijalarida men mijozning ismidan shaxsga bog'langanman mijozga ko'rinadi, bu esa app/views/customers/show.html.erb ko'rinishiga olib keladi yuklangan.)

0
qo'shib qo'ydi