Rails Locals-传递连接的模型数据

时间:2019-04-05 19:55:38

标签: ruby-on-rails

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

我还有另一个部分:GroupDevelopment has_many GroupSections。 GroupSections has_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。问题随之而来,我最终需要在迭代的后期打视频。所以那有同样的问题。

1 个答案:

答案 0 :(得分:0)

您误解了locals在分词中的含义。

说我们有

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

这意味着现在在image部分中,我们可以将局部变量sizeextension(键)用作@image.size@image.extension(值)。 / p>

输入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_systemsales_presentation将具有相同的值。