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

标签 ruby-on-rails ruby slim-lang

我有一个用 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/

相关文章:

ruby-on-rails - 有没有办法在heroku上使用sendgrid调试邮件程序?或者检查邮件是否实际发送?

ruby-on-rails - 范围为 :module nested routes 的 form_for 路由错误

ruby-on-rails - Rails路线奇怪

ruby - 测试厨房中的 ulimit

ruby - 使用 slim 或 haml 在独立(非 rails)ruby 应用程序中指定布局和模板

ruby-on-rails - 在 rails 4 中显示语法错误

ruby-on-rails - Rails 引擎 gemspec 的 "Could not find gem"

ruby-on-rails - rails : Render collection partial: Getting size of collection inside partial

ruby-on-rails - SLIM 在多行开头添加空格

ruby-on-rails - ruby rails : Use Variable in link_to helper path