我想我也有同样的问题。 Using multiple yields to insert content
我尝试了解决方案。我试图拥有 <%= yield :content %>
在我的 application.html.erb 中并且有 content_for :content
和 yield
在我看来,里面。但它不适用于我的应用程序。您能否详细解释一下我的问题或提供有关我的问题的示例场景?
内部链接不得重新加载,因此我不会在将显示的每个模板上使用渲染部分。
我最近才开始学习 Rails,所以这对我来说有点困惑。谢谢。
我尝试过这个;这只是一个例子,稍后我会修复侧边栏的连接。
在我的 application.html.erb 中
<!DOCTYPE html>
<html>
<head>
<title>Clinks</title>
<%= stylesheet_link_tag "application", media: "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
<body>
<section id="container" >
<%= render 'layouts/header' %>
<%= render 'layouts/sidebar' %>
<section id="main-content">
<section class="wrapper">
<div class="row">
<!-- %= yield % -->
<%=yield(:content) %>
</div><!--/row -->
</section><!--/wrapper -->
</section><!--/main-content -->
</section><!--/container -->
</body>
</html>
在我的menu_tables.html.erb里面layouts
文件夹。
<% content_for(:content) do %>
<%= render 'menu_tables/sidemenu' %>
<%= yield %>
<% end %>
<%= render template: "layouts/application" %>
在我的routes.rb中
Rails.application.routes.draw do
root :to => 'pages#home'
resources :menu_tables, except: [:show]
end
然后在menu_tables Controller 下的_sidemenu.html.erb中是链接的代码,因此侧边栏的每个链接都有不同的侧菜单。
最佳答案
可能您需要告诉 Controller 使用布局“菜单”
。在链接中,您提供了匹配的 Controller 名称和布局,因此它“神奇地”工作了。
请参阅guides :
2.2.14 Finding Layouts
To find the current layout, Rails first looks for a file in app/views/layouts with the same base name as the controller. For example, rendering actions from the PhotosController class will use app/views/layouts/photos.html.erb (or app/views/layouts/photos.builder). If there is no such controller-specific layout, Rails will use app/views/layouts/application.html.erb or app/views/layouts/application.builder. If there is no .erb layout, Rails will use a .builder layout if one exists. Rails also provides several ways to more precisely assign specific layouts to individual controllers and actions.
(注意:您可以检查日志中使用的布局,查找类似于 Rendered menu_items/index.html.erb inside layouts/application
的行)
关于ruby - 产量 :content doesn't show content_for :content,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32001284/