Rails 子布局
我有一个消息控制器,其操作如下:发送消息
、接收消息
、发送新消息
。它们以工具栏形式显示给用户。问题是,当我渲染每个视图时,我还必须手动渲染工具栏。因此,视图的代码如下所示:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
app/views/layouts/application.html.erb
app/views/layouts/messages.html.erb
app/views/layouts/application.html.erb
app/views/layouts/messages.html.erb
来自 文档:
假设您有以下 ApplicationController 布局:
app/views/layouts/application.html.erb
在 NewsController 生成的页面上,您想要隐藏顶部菜单并添加右侧菜单:
app/views/layouts/news.html.erb
就是这样。新闻视图将使用新的布局,隐藏顶部菜单并在“内容”div 内添加新的右侧菜单。
From the documentation:
Suppose you have the following ApplicationController layout:
app/views/layouts/application.html.erb
On pages generated by NewsController, you want to hide the top menu and add a right menu:
app/views/layouts/news.html.erb
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.