gpt4 book ai didi

ruby-on-rails - 细长 rails 中的变量

转载 作者:太空宇宙 更新时间:2023-11-03 18:05:41 25 4
gpt4 key购买 nike

我有一个用 Slim 编写的模板,我想将它与 Ruby 一起使用,但我在 index.slim 中遇到了这些错误。

   -content_panel_file = '_partials/content_panel_5'
-content_box_file = '_partials/content_box_2'
- menu_file = '_partials/menu_side'
= render '_partials/template'

在 template.slim 中:

.content-i
.content-box
== Slim::Template.new(content_box_file).render(Object.new, 'template_vars' => template_vars)

但它给出了这个错误:

undefined local variable or method `content_box_file' for #<#<Class:0x00000003b28308>:0x00000003b685c0>
Did you mean? content_for

最佳答案

原因是您的部分文件中不存在您的 content_box_file,您正试图访问不在当前“范围”中的局部变量。

尝试在渲染方法中将您的 content_box_file 变量作为本地变量传递:

= render partial: '_partials/template', locals: { content_box_file: content_box_file }

请注意,您需要使用 render partial: ...

完整的工作流程:

# model/index.html.slim
- content_box_file = 'app/views/_partials/content_box_2.slim'
= render partial: '_partials/template', locals: { content_box_file: content_box_file }

# _partials/_template.html.slim:
- template_vars = 'Hallo'
== Slim::Template.new(content_box_file).render(Object.new, template_vars: template_vars)

# _partials/content_box_2.html.slim:
== template_vars

关于ruby-on-rails - 细长 rails 中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46461557/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com