Ruby-on-rails 在 Rails 中搜索
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12010780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Search in Rails
提问by Dmytro Vasin
I have a table of "posts" with attributes that include titleand body.
我有一个“帖子”表,其属性包括title和body。
posts_controller.rb:
post_controller.rb:
class PostsController < ApplicationController
def index
@posts = Post.search(params[:search], params[:id])
end
end
index.html.erb:
index.html.erb:
<%= form_tag posts_path, :method => 'get' do %>
<%= text_field_tag :search, params[:search]%>
<%= submit_tag "Search", :name => nil %>
<% end %>
<hr />
<table>
<tr>
<th>Title</th>
<th>Text</th>
</tr>
<tr>
<td><hr></td>
<td><hr></td>
</tr>
<% @posts.each do |post| %>
<tr>
<td><%= post.title %></td>
<td><%= post.text %></td>
<td><%= link_to 'Show', :action => :show, :id => post.id %></td>
<td><%= link_to 'Edit', :action => :edit, :id => post.id %></td>
<td><%= link_to 'Destroy', { :action => :destroy, :id => post.id }, :method => :delete, :confirm => 'Are you sure?' %></td>
</tr>
<tr>
<td><hr></td>
<td><hr></td>
</tr>
<% end %>
</table>
and post.rb
和post.rb
def self.search(search, id)
if search
where(['name LIKE ?', "%#{search}%"])
else
scoped
end
end
When I submit the search params, I get an error message:
当我提交搜索参数时,我收到一条错误消息:
ActiveRecord::StatementInvalid in Posts#index
SQLite3::SQLException: no such column: name: SELECT "posts".* FROM "posts" WHERE (name LIKE '%lorem%')
Extracted source (around line #23):
23: <% @posts.each do |post| %>
APD: I want to search by 'title'.
APD:我想按“标题”搜索。
回答by Aaron Gray
Although this is not a direct answer to your question, here are some resources that may help you as you are learning about search and getting it implemented in your Rails app.
虽然这不是您问题的直接答案,但这里有一些资源可以帮助您学习搜索并在您的 Rails 应用程序中实现它。
Powerful search functionality with the Sunspot gem
List of the most popular search tools for Ruby
---------------UPDATE----------------
- - - - - - - -更新 - - - - - - - -
Elasticsearch has been growing in popularity due to some modern awesomeness that it has, such as instant indexing. It has a ruby gem named tire. Definitely worth a look.
由于 Elasticsearch 具有一些现代功能,例如即时索引,它越来越受欢迎。它有一颗名为“轮胎”的红宝石。绝对值得一看。
---------------UPDATE 2----------------
---------------更新2----------------
Tire has been retired, and has been replaced by Elasticsearch-ruby
Tyre 已退役,并已被Elasticsearch-ruby取代
回答by Mothirajha
Elasticsearch with gem searchkick
Elasticsearch 与 gem searchkick
note: searchkick is also contributed by tire) makes you more comfortable to work on.
注意:searchkick 也是由轮胎贡献的)让你更舒适地工作。
For more refer:
更多请参考:
回答by atw
For people coming here and looking for a solution in Rails 4, try the following example:
对于来到这里并在 Rails 4 中寻找解决方案的人,请尝试以下示例:
Posts Controller:
帖子控制器:
class PostsController < ApplicationController
def index
if params[:search]
@posts = Post.search(params[:search]).order("created_at DESC")
else
@posts = Post.all.order('created_at DESC')
end
end
end
Post Model:
岗位型号:
def self.search(search)
# Title is for the above case, the OP incorrectly had 'name'
where("title LIKE ?", "%#{search}%")
end
The index.html.erb remains the same apart from the search form:
index.html.erb 除了搜索表单之外保持不变:
<%= form_tag(posts_path, :method => "get", id: "search-form") do %>
<%= text_field_tag :search, params[:search], placeholder: "Search Posts" %>
<%= submit_tag "Search" %>
<% end %>
Note that the paths, controllers and columns you are using may be different.
请注意,您使用的路径、控制器和列可能不同。

