#37 Simple Search Form

本文介绍了一种在Ruby on Rails应用中实现简单搜索表单的方法。通过展示具体的代码示例,包括视图部分的表单创建及控制器与模型中的搜索逻辑实现,使读者能够快速掌握如何在项目中加入搜索功能。
A search form is quite different than other forms, this is because it does not deal with model's attributes. See a good way to add a simple search form in this episode.
<!-- projects/index.rhtml -->
<% form_tag projects_path, :method => 'get' do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
# projects_controller.rb
def index
@projects = Project.search(params[:search])
end

# models/project.rb
def self.search(search)
if search
find(:all, :conditions => ['name LIKE ?', "%#{search}%"])
else
find(:all)
end
end
<script language="Javascript"> function doReport(){ var form = document.forms[&#39;searchForm&#39;]; var newLot = form.elements["newLotId"].value; if (newLot !=""){ if(form.elements["formType"].value==""){ alert("Search New Lot should specify the subject!"); form.elements["formType"].focus(); return false; } } form.submit(); } </script> <#setting datetime_format="yyyy-MM-dd"/> <#import "/decorators/includes/macros.ftl" as m> <#assign ww = JspTaglibs["/WEB-INF/webwork.tld"] /> <#assign base = req.contextPath /> <div class="app"> <@ww.form name="&#39;searchForm&#39;" method="&#39;POST&#39;" action="&#39;doSearch.action&#39;" theme="&#39;simple&#39;"> <input type="hidden" name="paginationSupport.startIndex" value="0"/> <input type="hidden" name="paginationSupport.countOnEachPage" value="10"/> <#include "/search/searchCriterion.ftl"> <br/> <#if paginationSupport.items?has_content> <@m.pager "searchForm", ""/> <h3>Search Result List</h3> <table border="1" cellspacing="2" cellpadding="3" width="100%"> <tr class="b"> <th>Case Type</th> <th>Product ID</th> <th>Applicant</th> <th>Lot ID</th> <th>Lot Type</th> <th>Create Date</th> <th>Status</th> </tr> <#list paginationSupport.items as p> <tr class="b"> <td> <#if p.formType?if_exists=="Testing Terminate Request Form"> Terminate <#elseif p.formType?if_exists=="Testing Unterminate Request Form"> Unterminate <#elseif p.formType?if_exists=="Testing Scrap Request Form"> Scrap <#elseif p.formType?if_exists=="Testing Unscrap Request Form"> Unscrap <#elseif p.formType?if_exists=="Testing STR Request Form"> <#if p.item?if_exists=="STR"> STR <#else> Commission </#if> <#else> Unknown Case </#if> </td> <td><a href="${base}/user/viewRequestForm.action?requestFormId=${p.id}">${p.prodId?default("N/A")}</a></td> <td><@m.directoryLink p.applicant?if_exists/></td> <td>${p.lotId?if_exists}</td> <td>${p.lotType?if_exists}</td> <td>${p.submitDate?if_exists}</td> <td>${p.status?if_exists}</td> </tr> </#list> </table> <#else> No related result. </#if> </@ww.form> </div>
08-29
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值