【问题标题】:Two content_for the same yield renders in all the views (Rails 3)在所有视图中呈现两个 content_for 相同的产量(Rails 3)
【发布时间】:2013-08-30 00:10:14
【问题描述】:

我有两个单独的视图,它们为名为 :icons 的命名 yield 块输出内容。

当我加载这两个页面时,我总是在 :icons 中看到 2 个视图的内容... :-(

查看 1:项目#索引

<% content_for :icons do %>
    Project Icons ...
<% end %>
...

视图 2:树#显示

<% content_for :icons do %>
  <a href="javascript:void(0)" id="dynatree-expand-all">Expand</a> 
  <a href="javascript:void(0)" id="dynatree-collapse-all">Collapse</a> 
  <a href="javascript:void(0)" id="dynatree-transfer">Import</a> 

  <div id="viewname_selector">
    <%= form_tag({:controller => :loopview, :action => :show}, { :id => "viewname_form" ,:remote => true}) do %>
        <%= collection_select(:viewname, :id, @viewnames, :id, :name, :selected => @current_viewname_id) %>
    <% end %>
  </div>
<% end %>

而application.html.erb中的调用是

<div id="icons">
  <%= yield :icons %>
</div>

当我加载 Projects#index 时,我只有项目图标(所以没关系)。

当我加载 Tree#index 时,我有树形图标和项目图标...

我做错了什么?如何只获取树视图中的树图标?

编辑:树#index的完整源代码

<% content_for :head do %>
<title>Dynatree View <%= sicadea %></title>
<%= javascript_include_tag "leanModal/jquery.leanModal.min" %>  

<%#   For the dynatree  %>
<%= stylesheet_link_tag    "dynatree/skin/ui.dynatree.css" %>
<%= stylesheet_link_tag    "dynatree/skin/ui.dynatree.custom.css" %>
<%= javascript_include_tag "cookie/jquery.cookie.js" %>
<%= javascript_include_tag "dynatree/jquery.dynatree-1.2.4.js" %>
<%= stylesheet_link_tag    "contextMenu/jquery.contextMenu.css" %>
<%= javascript_include_tag "contextMenu/jquery.contextMenu-custom.js" %>
<%= javascript_include_tag "contextMenu/jquery.ui.position.js" %>
<%= javascript_include_tag "dynatree.js" %>

<%# for the poin'ts table %>
<%= stylesheet_link_tag    "loopview.css" %>
<% end %>

<% content_for :icons do %>
<a href="javascript:void(0)" id="dynatree-expand-all">Expand</a>  |
<a href="javascript:void(0)" id="dynatree-collapse-all">Collapse</a>  |
<a href="javascript:void(0)" id="dynatree-transfer">Import</a> |

<div id="viewname_selector">
    <%= form_tag({:controller => :loopview, :action => :show}, { :id => "viewname_form" ,:remote => true}) do %>
        <%= collection_select(:viewname, :id, @viewnames, :id, :name, :selected => @current_viewname_id) %>
    <% end %>
</div>
<% end %>



<div id="leftpanel">
<!-- Leftpanel Context menus -->
<ul id="myMenu" class="contextMenu">
    <li class="rename"><a href="#rename">Rename</a></li>
    <li class="new_child"><a href="#new_child">New Child</a></li>
    <li class="copy"><a href="#copy">Copy</a></li>
    <li class="paste"><a href="#paste">Paste</a></li>
    <li class="delete"><a href="#delete">Delete</a></li>
</ul>
<ul id="ProjectMenu" class="contextMenu">
    <li class="new_child"><a href="#new_child">New Child</a></li>
</ul>

<!-- Leftpanel Tree title -->
<div id='dynatree-title' data-project-id='<%= current_project.id %>'>
    <span></span><%= current_project.name %>
</div>

<!-- Leftpanel Dynatree -->
<%= render_project_tree(current_project, "tree") %> 

<!-- Leftpanel "New Child" Form -->
<%= render :partial => "lean_new_child" %>
</div>


<!-- Right Panel -->
<div id="dynatree-details">
    <%= render :template => 'projects/show' %>
</div>

【问题讨论】:

    标签: ruby-on-rails render yield content-for


    【解决方案1】:

    我可以知道你为什么在 application.html.erb 中添加以下代码。

    <div id="icons">
      <%= yield :icons %>
    </div>
    

    我认为这也只会加载到您的 Tree#index 页面中..

    【讨论】:

    • 不,这是为了创建一个块 chere,您可以通过 content_for 添加内容。它没有加载树#index。
    • :icons 是从哪里来的?从模型?你能展示你的完整视图代码树#index吗?
    • 它当然来自 。问题中添加了tree#index的源代码
    • 好的,是 tree#index 将项目页面加载为默认页面......然后添加了项目的图标......解决了
    猜你喜欢
    • 2011-04-06
    • 2010-12-19
    • 2015-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-22
    • 2020-03-08
    • 1970-01-01
    相关资源
    最近更新 更多