代码之家  ›  专栏  ›  技术社区  ›  marcamillion blelump

如何将局部变量发送到仅在单击链接后呈现的部分?

  •  0
  • marcamillion blelump  · 技术社区  · 11 年前

    我有一个Bootstrap模式,当单击此链接时会触发:

    <a class="plus" href="javascript:;" data-toggle="modal" data-target="#myModal">&nbsp;</a>
    

    然后我把这个放在我的 _footer.html.erb

    <%= render 'shared/tag_users_modal' %>
    

    然后有了这个标准的Bootstrap模式:

    <!-- Modal -->
    <div class="modal tagging fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title" id="myModalLabel">Tag User</h4>
          </div>
          <div class="modal-body">
                    <%= simple_form_for @node, url: add_tagged_user_node_path(@node), method: :post do |f| %>
                      <%= f.error_notification %>
    
                      <div class="form-inputs">
                            <%= f.input_field :user, label: "Users", collection: @users, as: :check_boxes, checked: @node.user_tags %>
                      </div>                            
    
                      <div class="form-actions">
                        <%= f.button :submit %>
                      </div>
                    <% end %>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            <button type="button" class="btn btn-primary">Save changes</button>
          </div>
        </div>
      </div>
    </div>
    

    这行不通,因为 @node 为零-即未通过。我不能从我的 _footer 分部,所以我希望能够在模态的原始执行中传递它。

    我该怎么做?或者我如何以另一种方式做到这一点?

    编辑1

    为了添加更多上下文,调用Bootstrap Modal的初始链接实际上位于另一个局部变量 node 也传递给了它。这个叫做这样:
    <%=渲染部分:“shared/box”,局部变量:{node:node}%>

    但我需要继续执行模式,只有在第一次单击切换链接后才启动 shared/box .

    总而言之: - /shared/box 分部具有激发BS模态的原始链接。 - /shared/footer' contains a render of another partial 标记用户模式 . - /共享/标记用户模式 contains the modal, but I need the @节点 object passed to it and the add_tagged_user_node_path`工作。

    现在它没有通过,因为我想不出一种方法来获得原件 node: node 链中的第三部分。

    1 回复  |  直到 11 年前
        1
  •  1
  •   SHS    11 年前

    因此,在分部中最好使用局部变量而不是实例变量。

    # your main view file:
    <%= render partial: path_to_partial, locals: {bool: true} %>
    
    # your partial:
    <%= bool ? 1 : 2 %>
    

    使用这些选项,分部将显示1。

    此外,作为 locals 选项是使用 render 而不是 render partial 像这样:

    # your main view file:
    <%= render path_to_partial, bool: true %>
    

    从上面,你可以推断你必须改变 @node 在你的部分 node 并指定 {node: @node} 无论您在渲染局部。