如何在 ruby​​ on rails 中创建 facebook 风格的新闻提要? [英] How do I create a facebook style news feed in ruby on rails?

查看:26
本文介绍了如何在 ruby​​ on rails 中创建 facebook 风格的新闻提要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将对象融合到一个流中.

I am trying to fuse to objects into a single stream.

我创建了一个名为 Newsfeed 的新模型和控制器.我有它,以便新闻源 has_many :messages 和 has_many :images 以及图像和消息属于 :newsfeed.我现在正在设置控制器,我有:

I created a new model and controller called Newsfeed. I have it so that newsfeed has_many :messages and has_many :images and images and messages belong_to :newsfeed. I am setting up the controller right now and I have:

def index
    @messages = Messages.all
    @images = Images.all
    @feed = ?
end

当每个都需要单独格式化时,我如何融合它们以便它可以从两者中提取并按时间顺序排列它们?

How do I fuse them so it can pull from both and put them in chronological order when each requires separate formating?

非常感谢任何帮助,谢谢.

Any help would be greatly appreciated, thanks.

推荐答案

你可以这样做:

控制器:

def index
    messages = Messages.all
    images = Images.all
    @feed = (messages + images).order_by(&:created_at)
end

查看 index.html.erb :

view index.html.erb :

<%= render @feed %>

查看 _feed.html.erb :

view _feed.html.erb :

<% if feed.is_a? Message %>
  <%= render "message", :locals => {:message => feed}%>
<% else %>
  <%= render "image", :locals => {:image => feed}%>
<% end %>

并添加 2 个部分 _message.html.erb 和 _image.html.erb

And add 2 partials _message.html.erb and _image.html.erb

这篇关于如何在 ruby​​ on rails 中创建 facebook 风格的新闻提要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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