douze2890241475 2013-04-28 02:26
浏览 17
已采纳

如何在Rails中创建动态布局列

I can create this in PHP, but the same thing in rails won't work:

<div class="col">
    <div class="row">content 1 here</div>
    <div class="row">content 2 here</div>
</div>
<div class="col">
    <div class="row">content 3 here</div>
</div>

These are all dynamic, so I am trying to generate new columns whenever the col field in the database changes. So, content 1 has a col of 1, content 2, col = 1, content 3 has col = 2. The content moves around so I will be updating the column number in the database via ajax. The problem is how to load all the content boxes and in the right columns. What is the best practice for doing this in Rails? Here is what I would do in PHP (psuedocode)

<?php
    // this will be content from database
    $content = $dbdata;
    // start first column
    echo '<div class="col">';
    foreach($content as $thiscontent){
        // output row divs with content
        echo '<div class="row">'.$thiscontent['body'].'</div>';
        if($lastcol == $thiscontent['col']){
             // end col and create new col div
             echo '</div><div class="col">';
        }
        $lastcol = $thiscontent['col'];
    }
    // ends last column
    echo '</div>';
?>

I could just be a noob and going about this the wrong way, so feel free to educate me on how this should be done. Is my database not the way to determine col number? Does rails handle iteration differently than I suspect?

  • 写回答

1条回答 默认 最新

  • doujiaci7976 2013-04-28 06:47
    关注

    In rails you could try do this. Put it in a view file:

        // start first column
        <div class="col">
        <% @dbdata.each do |thiscontent| %>
          // output row divs with content
          <div class="row"><%= thiscontent.body %></div>
          <% if !lastcol.nil? and lastcol == thiscontent.col %>
            // end col and create new col div
            </div><div class="col">
          <% end
          lastcol = thiscontent.col
        end %>
        // ends last column
      </div>
    

    Note that you should pass @dbdata to a view from the controller

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP