2014-04-21 61 views
0

我是ROR開發的初學者。我有導航,我希望在每個頁面上都有搜索框,當我輸入一些關鍵字時,它應該像跨桌面字段查詢一樣。我嘗試使用一些在線教程,但無法做到這一點。 我的表名:教程 這裏是導航欄在Ruby on Rails中使用類似查詢進行搜索

<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 

我不知道如何做這個。請幫助

+0

我不確定如果在雙引號內的'%#{search}%「'中傳遞實際上可以工作,您可能只需要這樣做:'['tutorial_name LIKE%?%',search ]'。 –

+0

相關,但可能會過時:[如何在Arel和Rails 3中執行LIKE查詢?](http://stackoverflow.com/q/4430578/456814)。另請參見[Rails 4 LIKE查詢 - ActiveRecord添加引號](http://stackoverflow.com/q/19105706/456814)。 –

+0

那麼你現在的代碼不工作?你有什麼錯誤嗎?如果是,則共享錯誤日誌。 –

回答

6

錯誤的名字通常是錯誤的想法。我相信你的名字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> 

現在嘗試一下測試。填寫搜索條件並按搜索按鈕。

+0

Hello Gene,謝謝你的幫助。我不明白我應該如何處理模型,我應該在tutorial.rb中編寫此方法,還是希望將我的搜索模型重命名爲教程,然後嘗試使用您的建議,但我不確定如何顯示結果?我創建了一個Index.html.erb文件,這裏是代碼。

教程名稱: <%= @ tutorial.tutorial_name%>

pritesh

+0

好吧我知道了感謝基因 – pritesh

+0

@ Pritesh777我建了一個快速和骯髒的應用程序,應該作爲一種模式,你想什麼做。見附加。 – Gene