使用 Datamapper 和 Sinatra 进行简单搜索
总的来说,我对 Ruby 和后端开发相当陌生。话虽这么说,我正在尝试创建一个简单的搜索表单。我使用 Sinatra 作为框架,使用 Datamapper 作为 ORM。最好的方法是什么?下面是我的架构,我希望搜索操作可以搜索图块和类别。
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
=========== 布局.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 %>
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
<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 %>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最基本的搜索查询可能是这样的:
The most basic search query could like this: