简体   繁体   English

rails-在遍历数组时添加html div标签

[英]rails - adding html div tags while looping through array

I'm trying to display a conversation in my html.erb file. 我正在尝试在html.erb文件中显示对话。

I'm using a specific template and want to copy their chat section. 我正在使用特定的模板,并希望复制其聊天部分。

Here's the chat section: 这是聊天部分:

http://webapplayers.com/inspinia_admin-v2.7.1/chat_view.html http://webapplayers.com/inspinia_admin-v2.7.1/chat_view.html

I noticed that their chat "bubbles" are just different divs. 我注意到他们的聊天“泡沫”只是不同的div。

Like this: 像这样:

 <div class="chat-message left">
    <img class="message-avatar" src="<%= image_path('a1.jpg') %>" alt="">
    <div class="message">
        <a class="message-author" href="#"> Michael Smith </a>
        <span class="message-date"> Mon Jan 26 2015 - 18:39:23 </span>
            <span class="message-content">
                Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
            </span>
    </div>
</div>

<div class="chat-message right">
    <img class="message-avatar" src="<%= image_path('a4.jpg') %>" alt="">
    <div class="message">
        <a class="message-author" href="#"> Karl Jordan </a>
        <span class="message-date">  Fri Jan 25 2015 - 11:12:36 </span>
            <span class="message-content">
                Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover.
            </span>
    </div>
</div>

If i have an array of @messages in my html.erb file, how can I traverse the array and for every message add one of those div sections in order to make my chat view? 如果我的html.erb文件中有一个@messages数组,我html.erb如何遍历该数组,并为每条消息添加一个div节以创建聊天视图?

Alternatively, I will have to put it in some sort of table with each row styled depending on whether the message is incoming or outbound. 另外,我必须将其放在某种表中,每行的样式都取决于消息是传入还是传出。

You can do this by just iterating @messages with @messages.each do |message|...end and then within the block have your divs with the message contents. 您可以通过仅使用@messages迭代@messages做到这一点。每个@messages.each do |message|...end ,然后在该块中包含具有消息内容的div。 A cleaner way might be to set up a _chat_view.html.erb partial with these divs and then in your main html.erb file you can do render partial: 'chat_view', collection: @messages and you still get the message object in the partial that you can use. 一个清洁的方式可能是建立一个_chat_view.html.erb部分与这些div,然后在你的主html.erb文件,你可以做render partial: 'chat_view', collection: @messages ,你仍然得到message对象中可以使用的部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM