Rails:如果存在参数,则渲染部分内容

时间:2013-03-12 19:07:06

标签: ruby-on-rails parameters render partials

如果渲染包含特定参数“has_footer”,我正在尝试有条件地渲染部分部分。

在_modal.html.erb中,我有以下内容:

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

我像这样渲染部分:

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

基本上,我只想在我的渲染命令中包含“:has_footer”参数时才显示div“模态页脚”。

请帮忙吗?

1 个答案:

答案 0 :(得分:1)

传递给partials的变量被视为局部变量。所以我会做类似的事情:

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

并检查变量如下:

if has_footer