2017-11-13 8 views
0

질문 : 다른 변수를 통해 부분 (즉, 일반) 루프를 만들려면 어떻게해야합니까?Rails 5의 루프 내부에서 부분을 렌더링하십시오.

내 HTML을 복제하지 않도록 부분적으로 사용하려는 탭이있는 페이지가 있습니다. 탭은 "비디오"및 "기사"입니다. 그것은 정확히 똑같은 HTML이지만 비디오 용 @videos와 기사 용 @article을 반복하고 싶습니다.

아이디어는 제품 부분을 완전히 일반화 한 다음 반복적으로 수행 할 @videos 또는 @articles에 전달됩니다.

부분 : 부분을 렌더링 _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보기 :

.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을 partial에 포함 할 수 없기 때문에 generic 부분을 갖는 목적을 무효화 할 수 있습니다. 이 구현으로

, 나는 오류를 얻을 : undefined local variable or method `product' for #<#<Class:0x007fc58053ae60>:0x007fc58884ea68>

+0

이 게시물은 부분이 렌더링되기 전에 for 루프를 수행 할 수 있음을 나타냅니다. https://stackoverflow.com/questions/1850905/passing-object-into-a-rails-partial-render – Andy

+0

지역에 부분 전달 –

+0

@AakashGupta는 어떻게 작성합니까? – Andy

답변

3

이 코드를보십시오 :

#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 
+0

WORKS! 힘내 친구 야. https://t2.rbxcdn.com/3dbe330c9785be99b8b807e3452d1e80 – Andy

0

안녕이 꽤 물건입니다 당신이 비디오는 다음이 시도 후 기사가오고 싶어합니다. 지역 주민 그것은 ​​작동이

  .row 
      - @videos.each do |video| 
       = render 'shared/product', locals: {product: video} 
      .row 
      - @articles.each do |article| 
       = render 'shared/product' , locals: {product: article} 

을 시도

= render partial: "product", collection: @videos 

= render partial: "product", collection: @articles 

은 기본보기에서 루프를 제거하지만 경우에 당신은 사용하고 있지 않습니다.