Rails中具有动态内容的边栏的设计模式 [英] Design pattern for side bar with dynamic content in Rails

查看:163
本文介绍了Rails中具有动态内容的边栏的设计模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有一个右侧栏,每个页面都有内容更改。
例如,当我在朋友页面时,侧栏应显示新朋友。
当我在帐户页面时,侧栏应显示最近的活动。

I would like to have a right side bar with content changes for each page. For example, when I am in Friends page, the side bar should display New Friends. When I am in Account page, the side bar should display Recent Activities.

我应该如何去尊重Rails设计模式?我听说过细胞宝宝,但是我不确定是否使用它。

How should I go about this to respect Rails design patterns? I heard about Cells gem, but I am not sure if I use it.

推荐答案

命名收益部分

<div id="main-content">
    <%= yield %>
</div>
<div id="side-content">
    <%= yield(:side_bar) %>
</div>

然后在您的视图中,使用content_for将内容放入命名收益中

Then in your views put content into the named yield using content_for

# friends view ....
<% content_for(:side_bar) do %>
    <%= render :partial => "shared/new_friends" %>
<% end %>

# account view ....
<% content_for(:side_bar) do %>
    <%= render :partial => "shared/recent_activity" %>
<% end %>    

这要求您明确说明每个视图在边栏中显示哪些内容,
可能使它动态做的更好?可能取决于具体情况和您的偏好

this requires you to be explicit about what content appears in the side bar for every view, maybe having it do it dynamically is better? probably depends on the specific situation and your preference

另请参见 - http://guides.rubyonrails.org/layouts_and_rendering.html#understanding-yield

这篇关于Rails中具有动态内容的边栏的设计模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆