Question

I am new to Ruby, and am using Sinatra and Sequel. I'm trying to implement a form to search through the title of my posts.

I'm doing this in my controller:

post '/search'  do
@post = Post.all(:Title.like => "%#{params[:query]}%")
erb :layout
end

And I'm doing this in my layout.erb:

<form action="/search" method="get">
 <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 %>

When I submit, this is the URL I get directed to:

http://localhost:4567/search?query=post 

but it displays the "Sinatra doesn't know this ditty." screen.

What am I missing here?

Was it helpful?

Solution

Your form is doing a HTTP GET

<form action="/search" method="get">

but your Sinatra action is defined to receive HTTP POST requests.

post '/search'  do

I think what is confusing you is that you have a class named Post. The get and post in the actions are not class names, but REST actions. Review routing.

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