1

所以,我是初学者,我知道layoutrails中有。

但我想要一个许多页面将使用的共享部分,包括所有mvc其他部分,而不仅仅是v .就像.Net webform中的母版页一样。

例如,每个站点都可以管理页面,并且该页面将使用由几个菜单组成的通用左侧边栏。

我怎么能意识到这一点?

4

3 回答 3

1

您真的应该阅读Rails 布局和渲染指南以了解在 Rails 中渲染。

Rails 提供了 layouts、partials 和 content_for 块来渲染视图。

一个示例主布局,用于说明部分和动态 content_for 部分的使用:

# layouts/application.html.erb
<html>
  <head>
    <%= yield :head %>
  </head>
  <body>
    <ul class="breadcrumbs">
      <%= yield :breadcrumbs %>
    </ul>
    <div class="sidebar">
      <%= render 'sidebar' %>
    </div>
    <div id="content">
      <%= yield %>
    </div>
  </body>
</html>

主要布局侧边栏部分:

# layouts/_sidebar.html.erb
<ul class="sidebar">
  <li><%= link_to 'Posts', posts_path %></li>
  <li><%= link_to 'My Profile', current_user %></li>
</ul>

show为面包屑呈现动态布局内容的帖子控制器的动作视图模板:

# posts/show.html.erb
<% content_for :breadcrumbs do %>
  <li><%= link_to 'Posts', posts_path %></li>
  <li><%= @post.title %></li>
<% end %>
<h1><%= @post.title %></h1>
<p><%= @post.content %></p>
...
于 2013-09-12T04:45:55.237 回答
0

您可以使用嵌套布局和局部http://guides.rubyonrails.org/layouts_and_rendering.html#using-nested-layouts来实现此目的

于 2013-09-12T04:38:11.707 回答
0

您的应用程序常规布局在 ./app/views/layouts/application.html.erb 中定义

按照建议,查看关于 %yield% http://guides.rubyonrails.org/layouts_and_rendering.html#understanding-yield的链接并阅读

享受

于 2013-09-12T04:45:19.623 回答