Rails 子布局

发布于 2024-11-01 22:40:02 字数 528 浏览 0 评论 0原文

我有一个消息控制器,其操作如下:发送消息接收消息发送新消息。它们以工具栏形式显示给用户。问题是,当我渲染每个视图时,我还必须手动渲染工具栏。因此,视图的代码如下所示:

sent_messages.html.erb
   <%= render "shared/toolbar" %>
   #  render stuff for sent messages


received_messages.html.erb
   <%= render "shared/toolbar" %>
   # render stuff for received messages


new.html.erb
   <%= render "shared/toolbar" %>
   # render stuff for new message

视图看起来不太干燥。有没有一种方法可以指定我希望 toolbar 在消息控制器中先于其他所有内容呈现?

I have a Messages controller, with actions like: Sent messages,Received messages,Send new message. They are displayed to the user in a toolbar form. The thing is, when I render each view, I have to manually render the toolbar as well. So, here's how the code for the views looks like:

sent_messages.html.erb
   <%= render "shared/toolbar" %>
   #  render stuff for sent messages


received_messages.html.erb
   <%= render "shared/toolbar" %>
   # render stuff for received messages


new.html.erb
   <%= render "shared/toolbar" %>
   # render stuff for new message

The views don't look very DRY. Is there a way I could specify that I want the toolbar to render before everything else, in the Messages controller?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ぺ禁宫浮华殁 2024-11-08 22:40:02

app/views/layouts/application.html.erb

<html>
<head>
  <title></title>
</head>
<body>
  <div id="content"><%= content_for?(:content) ? yield(:content) : yield %></div>
</body>
</html>

app/views/layouts/messages.html.erb

<% content_for :content do %>
  <%= render "shared/toolbar" %>
  <%= yield %>
<% end %>
<%= render file: "layouts/application" %>

app/views/layouts/application.html.erb

<html>
<head>
  <title></title>
</head>
<body>
  <div id="content"><%= content_for?(:content) ? yield(:content) : yield %></div>
</body>
</html>

app/views/layouts/messages.html.erb

<% content_for :content do %>
  <%= render "shared/toolbar" %>
  <%= yield %>
<% end %>
<%= render file: "layouts/application" %>
甚是思念 2024-11-08 22:40:02

来自 文档

假设您有以下 ApplicationController 布局:

app/views/layouts/application.html.erb

<html>
<head>
  <title><%= @page_title or "Page Title" %></title>
  <%= stylesheet_link_tag "layout" %>
  <style><%= yield :stylesheets %></style>
</head>
<body>
  <div id="top_menu">Top menu items here</div>
  <div id="menu">Menu items here</div>
  <div id="content"><%= content_for?(:content) ? yield(:content) : yield %></div>
</body>
</html>

在 NewsController 生成的页面上,您想要隐藏顶部菜单并添加右侧菜单:

app/views/layouts/news.html.erb

<% content_for :stylesheets do %>
  #top_menu {display: none}
  #right_menu {float: right; background-color: yellow; color: black}
<% end %>
<% content_for :content do %>
  <div id="right_menu">Right menu items here</div>
  <%= content_for?(:news_content) ? yield(:news_content) : yield %>
<% end %>
<%= render template: "layouts/application" %>

就是这样。新闻视图将使用新的布局,隐藏顶部菜单并在“内容”div 内添加新的右侧菜单。

From the documentation:

Suppose you have the following ApplicationController layout:

app/views/layouts/application.html.erb

<html>
<head>
  <title><%= @page_title or "Page Title" %></title>
  <%= stylesheet_link_tag "layout" %>
  <style><%= yield :stylesheets %></style>
</head>
<body>
  <div id="top_menu">Top menu items here</div>
  <div id="menu">Menu items here</div>
  <div id="content"><%= content_for?(:content) ? yield(:content) : yield %></div>
</body>
</html>

On pages generated by NewsController, you want to hide the top menu and add a right menu:

app/views/layouts/news.html.erb

<% content_for :stylesheets do %>
  #top_menu {display: none}
  #right_menu {float: right; background-color: yellow; color: black}
<% end %>
<% content_for :content do %>
  <div id="right_menu">Right menu items here</div>
  <%= content_for?(:news_content) ? yield(:news_content) : yield %>
<% end %>
<%= render template: "layouts/application" %>

That's it. The News views will use the new layout, hiding the top menu and adding a new right menu inside the "content" div.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文