ruby-on-rails - 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。然后问题就变成了我最终需要在迭代的后期点击视频。那么这有同样的问题。
解决方案
您误解了locals
in partials 的含义。
说我们有
<%= render partial: 'image', locals: {size: @image.size, extension: @image.extension} %>
这意味着image
现在我们可以使用局部变量size
和extension
(键)作为@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
具有相同的值。
推荐阅读
- java - 嵌入式 Jetty 上传失败并出现异常:Content-Type != multipart/form-data
- javascript - Google Spreadsheet node.js 移动行或排序表
- django - 如何在 django 中将表格的内容显示为表格?
- r - 复制行但每行增加时间(+1 分钟)
- java - JAXB 编组异常 - 作为一个元素,因为它缺少 @XmlRootElement 注释
- mysql - 请问如何通过sql制定规则?
- python-3.x - 如何解析这种类型的日期时间数据
- aws-lambda - 从 Lex 对 Lambda 的调用被终止
- postgresql - JPA 级联 - 当连接表中的 id 不同时,删除不起作用
- android-studio - 在适配器内校准片段方法但我的值变为空应该添加什么?