问题
I'm trying to implement a form to search through the title of my posts.
This is the controller code:
post '/search' do
@results = Post.all(:Title.like => "%#{params[:query]}%")
erb :layout
end
This is the layout.erb code:
<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 get an error saying 'undefined method `like' for: Title: Symbol'.
回答1:
Try
@results = DB[:posts].where(Sequel.like(:Title, "%#{params[:query]}%"))
回答2:
@results = DB[:posts].where{title.like("%#{params[:query]}%")}
ref.: https://github.com/jeremyevans/sequel/issues/1103
来源:https://stackoverflow.com/questions/20474810/how-to-do-a-sequel-query-with-like