2014-04-21 62 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