`
PBFox
  • 浏览: 68025 次
  • 性别: Icon_minigender_1
  • 来自: China
文章分类
社区版块
存档分类
最新评论

rubyonrails : 使用 ez_where 插件

阅读更多
看到一个很不错的插件ez_where,便用它做了个例子,贴出来与大家分享一下:

首先,新建一个项目:ez_where

D:\>rails ez_where
D:\rubyonrails>cd ez_where

其次,执行如下命令,生成rest 形式的脚手架

D:\rubyonrails\ez_where>ruby script/generate scaffold_resource user login:string
firstname:string lastname:string
exists app/models/
exists app/controllers/
exists app/helpers/
create app/views/users
exists app/views/layouts/
exists test/functional/
exists test/unit/
create app/views/users/index.rhtml
create app/views/users/show.rhtml
create app/views/users/new.rhtml
create app/views/users/edit.rhtml
create app/views/layouts/users.rhtml
create public/stylesheets/scaffold.css
create app/models/user.rb
create app/controllers/users_controller.rb
create test/functional/users_controller_test.rb
create app/helpers/users_helper.rb
create test/unit/user_test.rb
create test/fixtures/users.yml
create db/migrate
create db/migrate/001_create_users.rb
route map.resources :users

第三,执行数据迁移
D:\rubyonrails\ez_where>rake db:migrate

下面我们来使用一下ez_where
1.把ez_where 插件从svn://rubyforge.org//var/svn/ez-where下载下来放到...\vendor\plugins目录下

2.执行ruby script/generate controller Search

3.search_contrallor代码如下:

class SearchController < ApplicationController
def index
users
end

def users
if params[:q].blank?
@query = ''
@users = []
else
@query = params[:q].strip
@users = users_matching_query(@query)
end

bump_total(@users.size)
end

private
def bump_total(by)
@total = by + (@total ? @total : 0)
end

def users_matching_query(query)
words = query.split(/\s+/)

User.ez_find(:all) do |user|
user.any do
words.each do |word|
login.nocase =~ "%#{word}%"
end
end
end
end
end

4. application.rhtml 代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<title>Users: <%= controller.action_name %></title>
<%= stylesheet_link_tag 'scaffold' %>
<%= javascript_include_tag :defaults%>
</head>
<body>
<form action="/search">
<label for="search_query">Search</label>
<%= text_field_tag 'q', "Search", {:id=>"search_query",:defaultValue=>"Search",:onfocus=>'clearText(this)', :style=>'color:#ccc;', :onblur=>'refill(this)'} %>
<%= button_submit_tag "Go"%>
</form>
<p style="color: green"><%= flash[:notice] %></p>

<%= yield %>

</body>
</html>
当然,里面用到的button_submit_tag是另外一个插件:real_buttons里面的,在此不做介绍。

5. search下面的 index.rhtml 代码:

Results for "<%=@query%>" (<%= @total %>)
<% if @users and @users.size > 0 -%>
<ul>
<%= render :partial => 'user_list_item', :collection => @users%>
</ul>
<% end -%>

user_list_item.rhtml代码:

<li>
<h3 id="user_name"><%=link_to h(user_list_item.login), user_url(user_list_item) %></h3>
<dl>
<dt>First name:</dt><dd class="about_me"><%=h user_list_item.firstname %></dd>
<dt>Last name:</dt><dd><%=h user_list_item.lastname %></dd>
</dl>
</li>

6.最后在路由信息中加入: map.connect '/search/:action', :controller => 'search'

启动服务,看看效果。








分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics