首页 > 解决方案 > Rails Locals - 传递连接的模型数据

问题描述

我的模型结构非常稳固。我有 MarketingDeliverySystem has_many MarketingSections。MarketingSections 有_many MarketingVideos。

我还有另一个部分:GroupDevelopment has_many GroupSections。GroupSections 有_many GroupVideos。

我正在尝试使用部分来传递变量,从而将其全部干燥。

我有以下要传递给部分的内容:

= render partial: '/sales_presentations/sales_presentation',
                    locals: { marketing_delivery_system: @marketing_delivery_system,
                              first_video: first_marketing_video(@marketing_delivery_system),
                              sales_presentation: @marketing_delivery_system}

然后在部分我有以下内容:

.rounded-box-header.blue-bg #{sales_presentation.title}   
 ul
  - sales_presentation.sections.ordered.each_with_index do |section, index|
   - list_class = 'section show'
   - list_class = 'section hide' if index != 0
     li
      = link_to section.title, '#', class: 'section', data: { id: section.id }
              ul class="#{list_class}" data-section-id="#{section.id}"
                - section.videos.ordered.each do |video|
                  li.video
                    = link_to video.title, '#',
                                           class: 'video video-link',
                                           data: { video: video.youtube_link,
                                                   sales_presentation: sales_presentation.title.parameterize }
      .seven.columns
        .row
          div id="#{sales_presentation.title.parameterize}-container"
            video {
              id="#{sales_presentation.title.parameterize}-video-player"
              class="video-js vjs-default-skin videos"
              height=400
              poster=""
              controls preload='none'
              data-default-url="#{first_video(sales_presentation)&.youtube_link}"

在我更新本地人之前,我之前在顶部遇到了 sales_presentation.title 的问题。

我的问题/问题是我如何通过当地人使用 sales_presentation.sections 而不是使用@marketing_delivery_system.marketing.sections?

我以为我可以通过当地人把它放进去:sales_presentation.sections:@marketing_delivery_system.marketing_sections 但我最终遇到了一个巨大的语法错误。

我还尝试为这两个创建部分视图,然后将整个视图中的 sales_presentation 更改为 mod。然后将 mod.sections 更改为 mod_section 并将其在本地设置为 mod_section:@marketing_delivery_system.marketing_section。然后问题就变成了我最终需要在迭代的后期点击视频。那么这有同样的问题。

标签: ruby-on-rails

解决方案


您误解了localsin partials 的含义。

说我们有

<%= render partial: 'image', locals: {size: @image.size, extension: @image.extension} %>

这意味着image现在我们可以使用局部变量sizeextension(键)作为@image.size@image.extension(值)。

放入locals: {}所有你想要的局部变量。

所以你不能用当地人写sales_presentation.sections: @marketing_delivery_system.marketing.sections

但是你可以sales_presentation_sections: @marketing_delivery_system.marketing.section

你也有这个代码的问题:

locals: { marketing_delivery_system: @marketing_delivery_system,
          first_video: first_marketing_video(@marketing_delivery_system),
          sales_presentation: @marketing_delivery_system }

marketing_delivery_system并将sales_presentation具有相同的值。


推荐阅读