2016-08-12 14 views
0

私は自分のレールアプリを簡単に検索しました。しかし、私はそれが、キーワードが乱暴になっても動作させたい。 「hello world」と書かれたツイートがある場合、「hello world」と入力するとソートできますが、「world hello」と入力すると動作しません。どうすれば修正できますか?ここに私のコードは次のとおりです。Rails:シンプルな検索フォーム、キーワードが乱暴に表示される

tweet_controller.rb

def index 
@tweets = Tweet.all 
    if params[:search] 
    @tweets = Tweet.search(params[:search]) 
    else 
    @tweets = Tweet.all 
    end 
end 

tweet.rb

def self.search(search) 
    where("status LIKE ?", "%#{search}%") 
end 

とビュー

<%= form_tag(tweets_path, :method => "get", id: "search-form") do %> 
    <%= text_field_tag :search, params[:search], placeholder: "Search Tweets" %> 
    <%= submit_tag "Search" %> 
<% end %> 

ありがとう!

答えて

0

これを試してみてください:

tweet.rb full-text searchについては

def self.search(search) 
search_string = search.split(" ").first ##split and search by first keyword 
where("status LIKE ?", "%#{search}%") 
end 

、あなたがsolar

のような任意の検索エンジンを使用することができます
関連する問題