这可能是一个不起眼的人。我正在建立一个搜索表单。
在模型document.rb中,我有:
pg_search_scope :search_full_text,
:against => :full_text,
:using => {
:tsearch => {
:prefix => true
}
}
在documents_controller.rb中,我有:
def find
$results = Document.search_full_text(params[:ch_acte][:text])
end
但是NOTHING会被发送到数据库。服务器日志只显示:
Started POST "/documents/find" for ::1 at 2017-01-19 08:48:07 +0100
Processing by DocumentsController#find as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"ZkqVYMuqMqnUjLer/FVdBdtv4cycp71dXqPQw6j0mfHKX5ptin7p7YiYFj8bNtjciQDmHzbKtBnZoILpGGvl8Q==", "ch_acte"=>{"text"=>"complet", "words"=>"", #[cut for brievity]}, "commit"=>"Cherche"}
Rendering documents/find.html.erb within layouts/messources
Rendered documents/find.html.erb within layouts/messources (0.4ms)
Completed 200 OK in 216ms (Views: 210.2ms | ActiveRecord: 0.0ms)
除了模型中的方法pg_search_scope并在控制器中调用该方法之外,我该怎么做才能将它发送到数据库?
当我在rails控制台中运行Document.search_full_text("esp")
时,它可以正常工作。
更新
我在documents/find.html.erb
中添加了这个:
<% $results.each do |m| %>
<p>The id is <%= m.id %>.</p>
<% end %>
我得到一个显示我的菜单的页面,之后只有白色...
答案 0 :(得分:3)
你应该明白Rails试图尽可能高效。在构建搜索时,在您尝试访问搜索结果之前,它不会执行。
所以你会这样做......
def find
@documents = Document.search_full_text(params[:ch_acte][:text])
end
然后在find.html.erb
你可能会......
<% @documents.each do |document| %>
<%= document.name %>
<% end %>
执行@documents.each
行以检索文档时,只有 执行查询...即仅在需要执行时才执行。