从字符串编译 ERB 代码时出错 [英] Error compiling ERB code from string

查看:17
本文介绍了从字符串编译 ERB 代码时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的视图助手 (Rails 4.0) 编写一些测试,并尝试在执行它的字符串中编译 ERB 代码.但是,为了简单起见,我在这里使用了 rails 表单助手中的常用方法,但收到了相同的错误:

I'm writing some tests for my view helper (Rails 4.0) and trying to compile ERB code in a string that executes it. However, for the sake of simplicity, I am using a common method found in the rails form helpers here and receiving the same error:

Failure/Error: ERB.new(template).result
SyntaxError:
  (erb):1: syntax error, unexpected ')'
  ...out.concat(( field_set_tag do ).to_s); _erbout.concat "
	...
  ...                               ^
  (erb):4: syntax error, unexpected end-of-input, expecting ')'
  ; _erbout.force_encoding(__ENCODING__)
                                        ^

这是我正在执行的代码:

This is the code I'm executing:

template = <<-TEMPLATE
<%= field_set_tag do %>
    Lorem Ipsum
<% end %>
TEMPLATE

ERB.new(template).result

我发现了一些使用 <% 而不是 <%= 的建议,但这导致 Lorem Ipsum 成为只有输出.我也尝试过使用 HAML 而不是 ERB,但得到了类似的结果.

I've found some suggestions to use the <% instead of <%= but this results in Lorem Ipsum being the only output. I've also tried using HAML instead of ERB but came out with similar results.

如何使用字符串中的 field_set_for 帮助器让我的模板输出

Lorem Ipsum
?

How can I get my template to output <fieldset>Lorem Ipsum</fieldset> using the field_set_for helper inside a string?

推荐答案

Normal" Eruby 不允许使用 <%= 的表达式阻塞 Rails 使用它们的方式.Rails 扩展了Erubis Eruby 处理程序添加对它们的支持.您的测试只是尝试直接使用 Erb,因此此支持不可用.

"Normal" Eruby doesn’t allow for expressions using <%= to have blocks the way Rails uses them. Rails extends the Erubis Eruby handler to add support for them. Your test is just tring to use Erb directly, so this support isn’t available.

您需要确保已加载此支持才能使其正常工作.我可以让它工作:

You need to make sure this support is loaded for this to work. I can get get it working with this:

ActionView::Template::Handlers::Erubis.new(template).evaluate(ActionView::Base.new)

我不知道这是否是最好的方法.检查 RSpec 文档,可能有更好的方法来测试这样的视图部分.

I don’t know if this is the best way though. Check the RSpec docs, there may be a better way for testing parts of views like this.

这篇关于从字符串编译 ERB 代码时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆