【问题标题】:Rails: Render partial content if parameter is presentRails:如果存在参数,则呈现部分内容
【发布时间】:2013-03-12 19:07:05
【问题描述】:

如果渲染包含特定参数“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“modal-footer”。

有什么帮助吗?

【问题讨论】:

    标签: ruby-on-rails parameters render partials


    【解决方案1】:

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

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

    然后像这样检查变量:

    if has_footer
    

    【讨论】:

    • 谢谢!这意味着我必须始终定义“has_footer: false”或“has_footer: true”。是否可以让它默认为“true”并且只在需要时传递“false”?
    猜你喜欢
    • 1970-01-01
    • 2014-01-13
    • 2014-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-08
    • 1970-01-01
    相关资源
    最近更新 更多