开发者

Simple Search With Datamapper and Sinatra

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'
req开发者_JAVA技巧uire '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 %>


The most basic search query could like this:

@events = Event.all(:title.like => "%#{params[:query]}%") | Event.all(:category.like => "%#{params[:query]}%")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜