Error compiling ERB code from string
I am writing some tests for my view helper (Rails 4.0) and trying to compile the ERB code in a line that executes it. However, for the sake of simplicity, I'm using the generic method found in the rails form handlers here and getting 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 am executing:
template = <<-TEMPLATE
<%= field_set_tag do %>
Lorem Ipsum
<% end %>
TEMPLATE
ERB.new(template).result
I have found several suggestions to use <%
instead <%=
, but this results in the Lorem Ipsum
only way out. I also tried using HAML instead of ERB but came out with similar results.
How can I get my template to output <fieldset>Lorem Ipsum</fieldset>
using field_set_for
an inline helper?
source to share
"Plain" Eruby does not allow using expressions to <%=
block the way Rails uses them. Rails extends the Eruby Eruby handler to add support for these . Your test is just a tring to use Erb directly, so this support is not available.
You need to make sure this support is loaded for this. I can make it work with this:
ActionView::Template::Handlers::Erubis.new(template).evaluate(ActionView::Base.new)
I don't know if this is really the best way. Check the RSpec docs, there might be a better way to check parts of views like this.
source to share