【问题标题】:How to access a single attribute of associated model inside a loop?如何访问循环内关联模型的单个属性?
【发布时间】:2015-11-16 09:56:29
【问题描述】:

我非常需要你的帮助。我有很多:通过关系模型:产品、类别和分类(作为中间模型)。在我看来,我想遍历@products 并获取类别 每次迭代的字段“标题”。

这是我的看法:

<div class="col-md-9">
                <div class="row">
                    <section id="projects">
                        <ul id="thumbs">
                            <!-- Item Project and Filter Name -->
                        <% @products.each do |product| %>
                            <% product.categories.each do |category| %>
                                <li class="item-thumbs col-md-4 <%= category.title %> ">  
                            <% end %>
                               <!-- Fancybox - Gallery Enabled - Title - Full Image -->
                                <a class="hover-wrap fancybox" data-fancybox-group="gallery"    title="The City" >
                                    <span class="overlay-img"></span>
                                    <%= icon 'font-icon-plus' %>
                                </a>
                                <!-- Thumb Image and Description -->
                                <%= image_tag product.product_images, alt: product.description %>
                                <p><%= product.price %></p>
                            </li>                            
                            <!-- End Item Project -->  
                        <% end %>   
                        </ul>

                    </section>

                </div>

如您所见,我需要在每次迭代中获取类别的标题,以便将其插入到 li“类”并将每个产品选项卡链接到菜单“li”。

问题是,当我在@products 循环中遍历@categories 时,它只执行一次,因此我没有得到每个产品的类别标题。

【问题讨论】:

  • 什么只执行一次?这看起来会遍历 @products 中的每个产品,然后,对于每个产品具有的每个类别,生成您的 &lt;li&gt; 元素。
  • 我应该在@products 的每次迭代中获取“title”属性,如下所示))也感谢您的努力)))

标签: ruby-on-rails


【解决方案1】:

在我看来,您的循环重复创建了开头的 li 标记,而不是像您想要的那样收集所有类别标题,从而导致 HTML 无效。尝试像这样编写您的标记:

<div class="col-md-9">
    <div class="row">
        <section id="projects">
            <ul id="thumbs">
                <!-- Item Project and Filter Name -->
                <% @products.each do |product| %>
                    <!-- RELEVANT CHANGES RIGHT HERE! -->
                    <li class="item-thumbs col-md-4 <%= product.categories.pluck(:title).join(' ') %>">
                        <!-- Fancybox - Gallery Enabled - Title - Full Image -->
                        <a class="hover-wrap fancybox" data-fancybox-group="gallery" title="The City" >
                            <span class="overlay-img"></span>
                            <%= icon 'font-icon-plus' %>
                        </a>
                        <!-- Thumb Image and Description -->
                        <%= image_tag product.product_images, alt: product.description %>
                        <p><%= product.price %></p>
                    </li>                            
                    <!-- End Item Project -->
                <% end %>
            </ul>
        </section>
    </div>
</div>

这使用 Rails 方便的pluck 方法直接从数据库中获取所有相关的类别标题,然后将它们与空格连接在一起以形成有效的class 字符串。这是您想要的效果吗?

【讨论】:

  • 非常感谢,它的工作原理完全符合预期。你让我摆脱了一周的折磨)))
猜你喜欢
  • 1970-01-01
  • 2014-04-27
  • 1970-01-01
  • 2016-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多