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

查看:60
本文介绍了从字符串编译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 "\n\t\...
  ...                               ^
  (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.

如何使用<$ c $使模板输出< fieldset> Lorem Ipsum< / fieldset>

推荐答案

普通 Eruby不允许在字符串中使用c> field_set_for 助手吗?使用<%= 的表达式可以阻止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天全站免登陆