Ruby on rails slimrails中的变量
我已经用Slim编写了模板,我想用Ruby来使用它,但index.Slim中有这些错误 在template.slim中:Ruby on rails slimrails中的变量,ruby-on-rails,ruby,slim-lang,Ruby On Rails,Ruby,Slim Lang,我已经用Slim编写了模板,我想用Ruby来使用它,但index.Slim中有这些错误 在template.slim中: .content-i .content-box == Slim::Template.new(content_box_file).render(Object.new, 'template_vars' => template_vars) 但它给出了这样一个错误: undefined local variable or method `c
.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\u box\u文件变量作为本地变量传递:
= render partial: '_partials/template', locals: { content_box_file: content_box_file }
注意:您需要使用渲染部分:
完整的工作流程:
# 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
thnx对于您的响应,我尝试了,但得到了相同的错误您没有将其指定为分部@mar use=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