Gabriel
Gabriel

Reputation: 103

Rails: Render partial content if parameter is present

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?

Upvotes: 0

Views: 1339

Answers (1)

Kaeros
Kaeros

Reputation: 1138

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

Upvotes: 1

Related Questions