【问题标题】:Search using like query in Ruby on Rails在 Ruby on Rails 中使用类似查询进行搜索
【发布时间】:2014-06-06 02:56:32
【问题描述】:

我是 ROR 开发的初学者。我有导航,我想在每个页面上都有搜索框,当我输入一些关键字时,它应该像跨表字段的查询一样。我尝试使用一些在线教程,但无法做到。 我的表名:教程 这是我在导航栏上的搜索表单

<li><%= link_to 'Login', :controller => 'access', :action => 'login'  %></li>
<li><%= link_to 'Sign Up', :controller => 'users', :action => 'new'  %></li>
<li><%= link_to 'Logout', :controller => 'access', :action => 'logout'  %></li>
<div align="right">
<%= form_tag("/search", method: "get") do %>
  <%= label_tag(:q, "Search for:") %>
  <%= text_field_tag(:q) %>
  <%= submit_tag("Search") %>
<% end %>
</div>

这是我的控制器

class SearchController < ApplicationController 

  def show
    @tutorial = Tutorial.find(params[:q])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @tutorial }
    end
  end
end

这是我的模型

class Search < ActiveRecord::Base
  def @tutorial.search(search)
    if search
      find(:all, :conditions => ['tutorial_name LIKE ?', "%#{search}%"])
    else
      find(:all)
    end
  end
end

我不知道该怎么做。请帮忙

【问题讨论】:

标签: ruby-on-rails ruby


【解决方案1】:

通常,一个坏名字表示错误的想法。我相信您的型号Search 属于此类。应该叫Tutorial,不是吗?搜索是您对模型执行的操作,而不是模型本身。

如果这个猜测是正确的并且模型现在被称为 Tutorial 并且它有一个名为 name 的字段是一个字符串,那么你的模型将是

class Tutorial < ActiveRecord::Base

  def self.search(pattern)
    if pattern.blank?  # blank? covers both nil and empty string
      all
    else
      where('name LIKE ?', "%#{pattern}%")
    end
  end

end

这使得模型在如何搜索教程名称方面变得“智能”:Tutorial.search('foo') 现在将返回名称中包含 foo 的所有教程记录。

所以我们可以创建一个使用这个新功能的控制器:

class SearchController < ApplicationController 

  def show
    @tutorials = Tutorial.search(params[:q])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @tutorial }
    end
  end
end

相应的视图必须显示教程。你的没有。最简单的方法是编写一个只呈现一个教程的部分。假设它叫_tutorial.html.erb

那么在Search的视图中,需要添加

<%= render :partial => @tutorials %>

实际显示搜索结果。

加法

我将构建一个小例子。

# Make a new rails app called learning_system
rails new learning_system             

# Make a new scaffold for a Tutorial model.
rails g scaffold Tutorial name:string description:text 

# Now edit app/models/tutorial.rb to add the def above.

# Build tables for the model.
rake db:migrate

rails s # start the web server

# Now hit http://0.0.0.0:3000/tutorials with a browser to create some records.

<cntrl-C> to kill the web server

mkdir app/views/shared
gedit app/views/shared/_search_box.html.erb
# Edit this file to contain just the <%= form_tag you have above.

# Now add a header at the top of any view you like, e.g. 
# at the top of app/views/tutorials/index.html.erb as below
# (or you could use the layout to put it on all pages):

<h1>Listing tutorials</h1>
<%= render :partial => 'shared/search_box' %>

# Make a controller and view template for searches
rails g controller search show  

# Edit config/routes.rb to the route you want:  get "search" => 'search#show'

# Verify routes:

rake routes
       search GET    /search/:id(.:format)         search#show
    tutorials GET    /tutorials(.:format)          tutorials#index
              POST   /tutorials(.:format)          tutorials#create
 new_tutorial GET    /tutorials/new(.:format)      tutorials#new
edit_tutorial GET    /tutorials/:id/edit(.:format) tutorials#edit
     tutorial GET    /tutorials/:id(.:format)      tutorials#show
              PUT    /tutorials/:id(.:format)      tutorials#update
              DELETE /tutorials/:id(.:format)      tutorials#destroy

# Edit app/controllers/search_controller.rb as above.

# Create app/views/tutorial/_tutorial.html.erb with following content:
<tr>
<td><%= tutorial.name %></td>
<td><%= tutorial.description %></td>
</tr>

# Edit app/views/search/show.html.erb to have following content:
<h1>Show Search Results</h1>
<table>
<%= render :partial => @tutorials %>
</table>

现在尝试一个小测试。填写搜索条件并按“搜索”按钮。

【讨论】:

  • 您好基因,感谢您的帮助。我不明白我应该如何处理模型,我应该在 tutorial.rb 中编写此方法还是您希望我将搜索模型重命名为教程,我尝试使用您的建议,但我不确定如何显示我的结果?我已经创建了一个 Index.html.erb 文件,这是它的代码。

    教程名称:

  • @Pritesh777 我构建了一个快速而肮脏的应用程序,它应该作为你正在尝试做的事情的模式。见补充。
  • 这个答案太详细了,我想给@Gene捐款。最好的老师不在教室里。
  • @ChidozieNnachor 这很有趣。我教本科计算机科学 24 年。
  • @Gene,对不起。我对答案感到敬畏。现在加起来了。特此撤回我之前的声明。
【解决方案2】:

Rails 6 上,您可以在controller.rb 中使用search 操作实现:

  def search
    keyword = params[:q] #you can get this params from the value of the search form input
    @posts = Post.where("title LIKE ?", "%#{keyword}%")
  end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-24
    • 1970-01-01
    • 2011-03-11
    • 2012-04-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多