Monday, November 30, 2009

Using a modal layout with redbox for popup windows

In my current project, I've been using redbox for popup windows. By default, these come up as a plain white box, so I wanted to style them with a colored area for a title, a region at the bottom for buttons, etc. So I created a layout that has all the styles

<div class="modal_box">
<div id="title">
<%= yield :title %>
<span><%= link_to_close_redbox 'close' %></span>
<div id="modal_flashes">
<%= render :partial => '/layouts/flashes' %>
<%= yield %>

then to create a modal window, I just create a partial with the content I want to appear in the modal window

<% content_for :title do %>
How to...
<% end %>
<div class="modal_content content_section">
<p>As the ...</p>
<div class="actions">
<%= link_to_close_redbox 'Close', :class => "modal_action_link" %>

and render that partial in a hidden div on my main view

<div id="advice" style="display:none;">
<%= render :partial => 'advice', :layout => '/layouts/modal' %>

that is pulled by by a link_to_redbox call
<%= link_to_redbox 'advice?', 'advice' %>

In this case, I could have gone farther and pushed more of the styling & divs into the modal template. Then I would have had additional content_for's for :body and :actions. I didn't do that because in other places I needed to have the the popups display forms which included content in the body and submit buttons down in the actions. By making everything other than the title the be the content that was yielded, that was easy to do.


Post a Comment

Subscribe to Post Comments [Atom]

<< Home