[英]Search using like query in Ruby on Rails
我是 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
我不知道該怎么做。 請幫忙
一個糟糕的名字表明錯誤的想法通常是正確的。 我相信你的名字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>
現在嘗試一個小測試。 填寫搜索條件,然后按“搜索”按鈕。
在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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.