Slim Rails中的变量

3

我有一个用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
1个回答

4
原因是您的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

谢谢您的回复,我尝试过了,但是仍然出现了相同的错误。 - Jhon
你没有将其指定为部分@mar使用= render partial: '...' - Sebastián Palma

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接