【问题标题】:Using Ruby on Rails, How can I put data in a new table line?使用 Ruby on Rails,如何将数据放入新的表行?
【发布时间】:2016-07-19 14:34:42
【问题描述】:

我是 Ruby on Rails 的初学者。 而且我是韩国人。所以我的话有点奇怪... 我的问题是这个... 如果我有 10 个数据,我想将第 1~5 个数据放在第一行,将第 6~10 个数据放在第二行。

like this

我试过这个代码

              <table border height=300 width=300 align=center>
                <thead>
                  <tr style="font size:20;">
                    <th></th>
                    <th></th>
                  </tr>
                </thead>

                <tbody>
                  <% if current_user.samulham.nil? %>
                    <tr>
                      <% @samulham.each do |x| %>
                         <% if x.user_id.present?%>
                           <td><%= "X" %></td>
                         <% else %>
                           <td><%= link_to "#{x.lockernumber}", {:controller => "home", :action => "register", :cur_user => current_user.id, :cur_samulham => x.id}, method: :post %></td>
                         <% end %>
                      <% end %>
                    </tr>
                  <% end %>
                </tbody>
              </table>

感谢您的考虑。 :)

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-4 ruby-on-rails-3.2


    【解决方案1】:

    如果您的数据集大小始终为 10,您可以对其进行硬编码,如下所示:

      <tbody>
                          <% if current_user.samulham.nil? %>
                            <tr>
                              <% @samulham.first(5)each do |x| %>
                                 <% if x.user_id.present?%>
                                   <td><%= "X" %></td>
                                 <% else %>
                                   <td><%= link_to "#{x.lockernumber}", {:controller => "home", :action => "register", :cur_user => current_user.id, :cur_samulham => x.id}, method: :post %></td>
                                 <% end %>
                              <% end %>
                            </tr>
                            <tr>
                              <% @samulham.last(5)each do |x| %>
                                 <% if x.user_id.present?%>
                                   <td><%= "X" %></td>
                                 <% else %>
                                   <td><%= link_to "#{x.lockernumber}", {:controller => "home", :action => "register", :cur_user => current_user.id, :cur_samulham => x.id}, method: :post %></td>
                                 <% end %>
                              <% end %>
                            </tr>
                          <% end %>
         </tbody>
    

    编辑:但如果您想对 5 条记录组进行通用处理,您可以执行以下操作:

    @samulham.in_groups_of(5).each do |group|
                            <tr>
                              <% group.each do |x| %>
                                 <% if x.user_id.present?%>
                                   <td><%= "X" %></td>
                                 <% else %>
                                   <td><%= link_to "#{x.lockernumber}", {:controller => "home", :action => "register", :cur_user => current_user.id, :cur_samulham => x.id}, method: :post %></td>
                                 <% end %>
                              <% end %>
                            </tr>
    end
    

    【讨论】:

    • 非常感谢您的回答!但是,我的数据集大小通常超过 10... 我只想在一行中设置 5 个数据。
    • @SunHuKim 我编辑了帖子以帮助将解决方案概括化。希望对你有帮助
    • 对我真的很有帮助!!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2014-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多