在Rails 5中的循环内渲染局部 [英] Render partials inside loop in Rails 5

查看:94
本文介绍了在Rails 5中的循环内渲染局部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:如何通过不同的变量进行局部(必须是通用的)循环?

Question: How do I make a partial (that has to be generic) loop through different variables?

我有一个选项卡式页面,我想在其中使用部分页面以避免重复HTML.选项卡是视频"和文章".这是完全相同的HTML,但是我要遍历@videos的视频和@articles的文章.

I have a tabbed page where I want to use a partial to avoid duplicating my HTML. The tabs are "Videos" and "Articles". It's the exact same HTML but I want to iterate through @videos for videos and @articles for articles.

想法是使产品部分完全通用,然后以某种方式传入我要迭代的@videos或@articles.

The idea was to make the product partial completely generic and then somehow pass in @videos or @articles that I want to iterate through.

部分:_product.html.slim

Partial: _product.html.slim

.col-md-5
  .thumbnail
    .thumb.spacer-sm
      - if product.image.blank?
        iframe allowfullscreen="" frameborder="0" mozallowfullscreen="" src="https://player.vimeo.com/product/#{product.vimeo_id}" webkitallowfullscreen=""
      - elsif product.vimeo_id.blank?
        = image_tag(product.image.url,
          class: 'img img-responsive img-rounded')
    .caption
      .content-group-sm.media
        .media-body
          h6.text-semibold.no-margin
            = product.name
          small.text-muted
            | by 
            = product.user.name
        - unless product.price.blank?
          h6.text-success.media-right.no-margin-bottom.text-semibold
            | $
            = product.price
      = product.description
    .panel-footer.panel-footer-transparent
      .heading-elements
        ul.list-inline.list-inline-separate.heading-text
          li
            = link_to 'Delete', product_path(product), method: :delete
          li
            = link_to 'Edit', edit_product_path(product)

HTML视图呈现部分视图:

HTML view rendering the partial:

 .page-container.spacer-minus
    .page-content
      .row
        .col-md-4
          .sidebar-default.sidebar-separate
            .sidebar-content
              .content-group
                .panel.no-border-radius-top
                  ul.navigation
                    li.navigation-header Navigation
                    li.active
                      a data-toggle="tab" href="#videos"
                        i.icon-video-camera2
                        | Videos
                    li
                      a data-toggle="tab" href="#articles"
                        i.icon-graduation
                        | Articles

        .col-md-8
          .tab-content
            #videos.tab-pane.fade.in.active
              .row
                - @videos.each do |product|
                  = render 'shared/product'

            #articles.tab-pane.fade
              .row
                - @articles.each do |product|
                  = render 'shared/product'

我只需要循环就可以了解要迭代的变量.我不能在部分文件中包含@video或@article,因为那样会破坏拥有通用部分文件的目的.

I just need my loop to understand what variable I want to iterate through. I cannot include @video or @article in the partial since that will defeat the purpose of having a generic partial.

通过此实现,我得到了错误: undefined local variable or method `product' for #<#<Class:0x007fc58053ae60>:0x007fc58884ea68>

With this implementation, I get the error: undefined local variable or method `product' for #<#<Class:0x007fc58053ae60>:0x007fc58884ea68>

推荐答案

尝试以下代码:

#videos.tab-pane.fade.in.active .row - 

@videos.each do |video| 

  = render 'shared/product', product: video 

#articles.tab-pane.fade .row -       

@articles.each do |article| 

 = render 'shared/product', product: article

这篇关于在Rails 5中的循环内渲染局部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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