2013-11-21 1 views
2

나는 속편을 사용하고 있습니다. 내 app.rb에서 , 나는 포스트 클래스에 ruby ​​및 sinatra가있는 내 블로그의 간단한 검색 양식

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

그리고 blog_model.rb에 Layout.erb

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

를 쓴이 :

def self.search(query) 
    #where(:title, query) -> This would return an exact match of the query 
    where("title like ?", "%#{query}%") 
end 

그리고이납니다 :/검색시 LocalJumpError 주어진 블록이 없습니다 (yield). 이 코드를 올바르게 수행 했습니까? 미리 감사드립니다.

+0

당신이 로그에서 전체 오류 메시지를하시기 바랍니다 게시 할 수 없습니다 :

  • 은으로 ERB 호출을 교체? 또한 우리는'search()'메소드를 어디서나 사용하지 않았고, 첫 번째 스 니펫에서'where()'를 사용해야하는 곳에'find()'를 사용합니다. –

  • 답변

    2

    문제는 erb 파일의 이름이 입니다. layout.erb입니다.

    Sinatra는 항상 layout.erb을 검색합니다. 명시 적으로 다른 레이아웃 파일을 나타내지 않으면 페이지 템플릿을 처리합니다.

    1. layout.erb 파일의 이름을 바꿉니다 :

      <!doctype html> 
      <html> 
      <head> 
      ... 
      <body> 
      ... 
          <%= yield %> insert the content here 
      ... 
      </html> 
      

      는 두 가지 해결책이 있습니다 :이 파일은 형태를 갖는다. erb :layout, layout: false