views:

252

answers:

1

Help me understand this; I'm learning Sinatra (and Rails for that matter, er, and Ruby).

Say I'm doing a search app. The search form is laid out in one div, and the results will be laid out in another. The search form is rendered into the div by a previous view (maybe from a login form).

I want to process the form params, perform the search, and render the results into the results div.

If I have a single "yield" in the layout and render the divs from different views, the results div erases the search div when it renders.

If I define the divs in the default layout, then just render the content, obviously the layout will be messed up: there would have to be two "yields" and I don't think Sinatra supports passing blocks in to yields.

I tried foca's sinatra-content-for plugin, and that seems closer to what I need. But I can't figure out where to place the "yield_content" statements.

If I have this haml in my layout:

#search
  -# search form
  = yield_content :search
#results
  -# search results
  = yield_content :results

... this in my search view:

 - content_for :search do
 %form{:method => "post"... etc.

... and this in the results view:

- content_for :results do
%table{:class => 'results'... etc.

This sort of works but when I render the results view, the search div is emptied out. I would like to have it remain. Am I doing something wrong? How should I set this up?

+1  A: 

I think you mean you ALWAYS want to show 2 divs, but in a new search they should be empty and on a results page they should be populated. You can probably get away w/ one haml template, and just populate it diff'tly on the request method:

get "/search" do
    # render haml
end

post "/search" do
    # set instance variables: @search & @results
    # run search
    # render haml
end

(Sorry this is very pseudo... not at a real computer.)

sevennineteen
Thanks. Ya I figured that out eventually :-)
thermans