2013-08-29 2 views
0

주어진 값에 따라 내 개체를 필터링하는 검색 (필터링) 양식을 만들었습니다. 회사 모델이 있으며 그 속성에 따라 검색이 이루어집니다. 이건 내 모델 company.rb레일 3 부분 검색

class Company < ActiveRecord::Base 
    attr_accessible :city, :country, :fax, :name, :reseller, :sector, :street_address, :telephone, :id 
    has_many :users , dependent: :destroy 

    def name_for_form 
    "#{name}" 
    end 

    def self.search(search) 
    if search 
     q = "%#{search}" 
     where('name LIKE ? OR city LIKE ? OR country LIKE ? OR street_address LIKE ? OR telephone LIKE ? OR fax LIKE ? OR sector LIKE ?', 
      q,q,q,q,q,q,q) 
    else 
     scoped 
    end 
    end 

    validates :city, presence: true 
    validates :country, presence: true 
    validates :fax, presence: true 
    validates :name, presence: true 
    validates :sector, presence: true 
    validates :street_address, presence: true 
    validates :telephone, presence: true 
end 

이다 나는 3 개 회사 이름을했다고 가정하자이 내 companies_controller.rb

helper_method :sort_column, :sort_direction 
def index 
    @companies = Company.search(params[:search]).order(sort_column + ' ' + sort_direction).paginate(:per_page => 10, :page => params[:page]) 
end 

입니다

<% provide(:title, 'All companies') %> 
<h1>All companies</h1> 

<%= form_tag companies_path, :method => 'get' do %> 
    <%= hidden_field_tag :direction, params[:direction] %> 
    <%= hidden_field_tag :sort, params[:sort] %> 
    <p> 
    <%= text_field_tag :search, params[:search] %> 
    <%= submit_tag "Search", :name => nil %> 
    </p> 
<% end %> 

<table class="pretty" border="1" cellpadding="10"> 
    <tr> 
    <th><%= sortable "name" %></th> 
    <th><%= sortable "city" %></th> 
    <th><%= sortable "country" %></th> 
    <th><%= sortable "street_address" %></th> 
    <th><%= sortable "sector" %></th> 
    <th><%= sortable "telephone" %></th> 
    <th><%= sortable "fax" %></th> 
    <th>DELETE</th> 
    </tr> 

    <% for company in @companies %> 
    <tr class="<%= cycle('oddrow', 'evenrow') -%>"> 
    <td><%= link_to company.name, company %></td> 
    <td><%= company.city %></td> 
    <td><%= company.country %></td> 
    <td><%= company.street_address %></td> 
    <td><%= company.sector %></td> 
    <td><%= company.telephone %></td> 
    <td><%= company.fax %></td> 
    <td><% if current_user.admin? %> 
      || <%= link_to "delete", company, method: :delete, 
           data: { confirm: "You sure?" } %> 
     <% end %></td> 
    </tr> 
    <% end %> 
</table> 
<%= will_paginate @companies %> 

: 이것은 내 index.html.erb입니다 칼라 하리, 칼라 하리 2, 칼라 하리 2. 나는 칼라 하리를 검색 할 때 단지 1 개의 회사 인 칼라 하리만을 발견했다. 나는 그것이 칼라 하리 2 또는 칼라 하리 2에서 칼라하라를 발견 할 수 없다는 것을 의미합니다. 일치하는 항목 만 찾습니다. kala를 검색하면 아무 것도 발견되지 않습니다. 가장 간단하게 어떻게 수정할 수 있습니까? 나는 레일을 처음 사용하고 많은 것들을 엉망으로 만들고 싶지 않습니다. 검색 쿼리의 끝에 와일드 카드를 추가하고 당신이 원하는 얻을 것이다

답변

2

간단한 변화 : LIKE와 함께 사용하면

q = "%#{search}%" 

% 아무것도 일치 않도록 현재 작성된 코드 아무것도 일치합니다 그 은 입력과 함께으로 끝납니다 ('afoo', 'b_foo'및 '1 3 5 x foo'의 'foo'와 일치 할 것입니다). 그러나 끝에 일치하는 와일드 카드가 없으면 일치하지 않습니다 그 것들은 쿼리가 포함되어 있지만 (그래서 'foo'는 일치하지 않습니다. 'f oobar '또는'afoox ').

+0

고맙습니다. – kalahari

관련 문제