使用Datamapper和Sinatra进行简单搜索 [英] Simple Search With Datamapper and Sinatra
本文介绍了使用Datamapper和Sinatra进行简单搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一般对Ruby和后端开发都不熟悉。话虽这么说,我正在尝试创建一个简单的搜索表单。我使用Sinatra作为框架,使用Datamapper作为我的ORM。做这个的最好方式是什么?下面是我的架构,我希望搜索操作同时搜索图块和类别。
I'm fairly new to Ruby and backend development in general. That being said I'm trying to create a simple search form. I'm using Sinatra as the framework and Datamapper as my ORM. What is the best way to do this? Below is my schema I would like the search action to search both the tile and category.
require 'sinatra'
require 'datamapper'
DataMapper.setup(:default, "sqlite3://#{Dir.pwd}/cal.db")
class Event
include DataMapper::Resource
property :id, Serial
property :title, String
property :text, Text
property :contact_name, String
property :contact_email, String
property :location, String
property :event_start_time, String
property :event_end_time, String
property :category, String
property :created_at, DateTime
property :approved, Boolean, :default => false
end
DataMapper.auto_upgrade!
post '/search' do
@results = Event.all
erb :layout
end
============
layout.erb
============ layout.erb
<form action="/search" method="post">
<input type="text" name="query"/><br />
<input type="submit" />
</form>
<% if @results %>
<table>
<%@results.each do |r|%>
<tr valign="top">
<td><%=r.title%></td>
</tr>
<%end%>
</table>
<% end %>
推荐答案
最基本的搜索查询可能是这样的:
The most basic search query could like this:
@events = Event.all(:title.like => "%#{params[:query]}%") | Event.all(:category.like => "%#{params[:query]}%")
这篇关于使用Datamapper和Sinatra进行简单搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文