Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 扩展搜索功能_Ruby On Rails - Fatal编程技术网

Ruby on rails 扩展搜索功能

Ruby on rails 扩展搜索功能,ruby-on-rails,Ruby On Rails,我正在建立一个网站,其中有多个表格,其中详细介绍了该网站的功能,如拼车,比赛,对手和注册用户。我添加了一个搜索功能,可以在网站上搜索比赛,但我不知道如何扩展它,以便用户可以搜索拼车详细信息、对手和注册用户。我可能只是重复我对匹配项所做的操作,但这似乎效率低下,而且我相信一定有办法将所有匹配信息捕获到输入的搜索词中 这里有一个相当多的代码,所以请容忍我- 这在matches\u contoller.rb文件中: def search @search_term = params[:q] st

我正在建立一个网站,其中有多个表格,其中详细介绍了该网站的功能,如拼车,比赛,对手和注册用户。我添加了一个搜索功能,可以在网站上搜索比赛,但我不知道如何扩展它,以便用户可以搜索拼车详细信息、对手和注册用户。我可能只是重复我对匹配项所做的操作,但这似乎效率低下,而且我相信一定有办法将所有匹配信息捕获到输入的搜索词中

这里有一个相当多的代码,所以请容忍我-

这在matches\u contoller.rb文件中:

 def search
 @search_term = params[:q]
 st = "%#{params[:q]}%"
 @matches = Match.where("Opponent like ?", st)


respond_to do |format|
  format.html # index.html.erb
  format.json { render json: @matches }
end  
end
  <%= form_tag("/search", :method => "post") do %>
  <%= label_tag(:q, "Search for:") %>
  <%= text_field_tag(:q) %>
  <%= submit_tag("Search") %>
  <% end %>
 <h1>Search Matches Catalog</h1>
 <br />
 <h3>Searching For: <%= @search_term %></h3>
 <table class="catalog">

 <% if @matches.length == 0  %>
 <br />
 <h2>No matches found for this search term!!</h2>
 <% end %>

 <% @matches.each do |match| %>
 <tr>
    <td><%= match.opponent %></td>
    <td><%= match.game %></td>
    <td><%= match.home_away %></td>
    <td><%= match.kick_off.strftime("%d/%m/%Y at %I:%M") %></td>
    <td><%= match.score %></td>
    <td><%= match.result %></td>

</tr>

<% end %>
</table>

<% if session[:login] == 1 %>
<p><%= link_to 'New match', new_match_path %></p>
<% end %>
 post '/search' => 'matches#search'
        <div class="searchbox">
        <%= render :partial => 'matches/search' %>
        </div>
我在matches文件夹中添加了一个_search.html.erb,代码如下:

 def search
 @search_term = params[:q]
 st = "%#{params[:q]}%"
 @matches = Match.where("Opponent like ?", st)


respond_to do |format|
  format.html # index.html.erb
  format.json { render json: @matches }
end  
end
  <%= form_tag("/search", :method => "post") do %>
  <%= label_tag(:q, "Search for:") %>
  <%= text_field_tag(:q) %>
  <%= submit_tag("Search") %>
  <% end %>
 <h1>Search Matches Catalog</h1>
 <br />
 <h3>Searching For: <%= @search_term %></h3>
 <table class="catalog">

 <% if @matches.length == 0  %>
 <br />
 <h2>No matches found for this search term!!</h2>
 <% end %>

 <% @matches.each do |match| %>
 <tr>
    <td><%= match.opponent %></td>
    <td><%= match.game %></td>
    <td><%= match.home_away %></td>
    <td><%= match.kick_off.strftime("%d/%m/%Y at %I:%M") %></td>
    <td><%= match.score %></td>
    <td><%= match.result %></td>

</tr>

<% end %>
</table>

<% if session[:login] == 1 %>
<p><%= link_to 'New match', new_match_path %></p>
<% end %>
 post '/search' => 'matches#search'
        <div class="searchbox">
        <%= render :partial => 'matches/search' %>
        </div>
最后,它被添加到application.html.erb文件中:

 def search
 @search_term = params[:q]
 st = "%#{params[:q]}%"
 @matches = Match.where("Opponent like ?", st)


respond_to do |format|
  format.html # index.html.erb
  format.json { render json: @matches }
end  
end
  <%= form_tag("/search", :method => "post") do %>
  <%= label_tag(:q, "Search for:") %>
  <%= text_field_tag(:q) %>
  <%= submit_tag("Search") %>
  <% end %>
 <h1>Search Matches Catalog</h1>
 <br />
 <h3>Searching For: <%= @search_term %></h3>
 <table class="catalog">

 <% if @matches.length == 0  %>
 <br />
 <h2>No matches found for this search term!!</h2>
 <% end %>

 <% @matches.each do |match| %>
 <tr>
    <td><%= match.opponent %></td>
    <td><%= match.game %></td>
    <td><%= match.home_away %></td>
    <td><%= match.kick_off.strftime("%d/%m/%Y at %I:%M") %></td>
    <td><%= match.score %></td>
    <td><%= match.result %></td>

</tr>

<% end %>
</table>

<% if session[:login] == 1 %>
<p><%= link_to 'New match', new_match_path %></p>
<% end %>
 post '/search' => 'matches#search'
        <div class="searchbox">
        <%= render :partial => 'matches/search' %>
        </div>

'匹配/搜索'%>
我意识到这有点冗长,任何建议都非常感谢

如果有人这么喜欢我的cloud9开发环境,我很乐意与他们分享我的cloud9开发环境,如果他们觉得这样更容易的话,我可以让他们看看

提前谢谢,我希望问题清楚。

我会建议您,它有灵活的高级搜索索引,您可以在其中添加满足您要求的要搜索的列

一个很棒的
elasticsearch
客户端正在运行。它抽象了elasticsearch DSL中的所有困难,简化了您的任务

首先需要安装
elasticsearch
,然后将
gem'searchkick'
添加到
gem文件中

如果你用Mac电脑 brew安装elasticsearch

Linux发行版也一样,只需在
ubuntu
中将
brew
替换为
apt-get
、在Fedora中将
yum
dnf

searchkick
添加到您的模型中

class Match < ActiveRecord::Base
  searchkick
end
然后运行
Match.reindex
,您可以使用
Match.search(“查询”)


阅读有关
elasticsearch
searchkick
各自文档的更多信息。(以上链接)

感谢您的回复,穆罕默德。我还没有机会测试这个,但会在一天左右给你回复。