Simple Search With Datamapper and Sinatra

南笙酒味 提交于 2019-12-06 07:16:51

问题


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 %>

回答1:


The most basic search query could like this:

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


来源:https://stackoverflow.com/questions/5747670/simple-search-with-datamapper-and-sinatra

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!