0

I'm trying to conditionally render a section of a partial, if the render includes a particular parameter "has_footer".

In _modal.html.erb, I have the following:

<div id="<%= id %>" class="modal hide fade" tabindex="-1" role="dialog" aria-hidden="true">
  <div class="modal-header">
    <button type="button" class="btn-modal-close" data-dismiss="modal" aria-hidden="true">×</button>
    <header><%= title %></header>
  </div>
  <div class="modal-body">
    <%= render partial: content %>
  </div>
  <% if params[:has_footer] %>
    <div class="modal-footer">
      <button class="btn-modal-save">Save</button>
      <button class="btn-modal-cancel" data-dismiss="modal" aria-hidden="true">Cancel</button>
    </div>
  <% end %>
</div>

And I render the partial like this:

<%= render "pages/modal", :has_footer, id: "modal-add-campaign", title: "Add Campaign", content: "pages/modal_add_campaign" %>

Basically, I want to display the div "modal-footer" only if I include the ":has_footer" parameter in my render command.

Any help, please?

Gabriel
  • 103
  • 1
  • 4
  • 13

1 Answers1

1

Variables passed to partials are treated like local variables. So i would do something like:

<%= render :partial => "pages/modal", locals: { has_footer: true, id: "modal-add-campaign", title: "Add Campaign", content: "pages/modal_add_campaign" } %>

And check the variable like this:

if has_footer
Kaeros
  • 1,138
  • 7
  • 7
  • Thanks! This would mean I have to always define either "has_footer: false" or "has_footer: true". Is it possible to let it default to "true" and only passing "false" if required? – Gabriel Mar 12 '13 at 20:02