Question

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'.

Was it helpful?

Solution

Try

@results = DB[:posts].where(Sequel.like(:Title, "%#{params[:query]}%"))

OTHER TIPS

@results = DB[:posts].where{title.like("%#{params[:query]}%")}

ref.: https://github.com/jeremyevans/sequel/issues/1103

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top