2

I would like to use the ruby block/yield concept in an erb (to be specific, so that I can use form_helpers). When I do so I get syntax errors, but not in my erb code; in the outputted code once erb's have been processed.

For example, let's say I have the following helper:

def test_method   # assume this returns some string
    yield if block_given?
end

and I have this erb code:

<%= test_method do %>
    <h1>asdf</h1>
<% end %>

the result is:

syntax error, unexpected ')' ; @_out_buf.concat(( test_method do ).to_s)

I understand that the first line is converted to code independently of the other lines. What I don't understand is why or how to fix it. thanks!


ruby: 1.9.3-p392

sinatra: 1.42

anything else I can provide?

Pat Newell
  • 2,219
  • 2
  • 18
  • 23
  • possible duplicate of [How to implement form\_tag helpers without actionview?](http://stackoverflow.com/questions/17581256/how-to-implement-form-tag-helpers-without-actionview) – matt Sep 20 '13 at 20:14

1 Answers1

3

Just remove output helper before test_method call in erb so it looks like:

<% test_method do %>

= helper expects a singleline expression to be given, not multiline.

More detailed info could be found here, here and here.

Community
  • 1
  • 1
icanhazbroccoli
  • 1,035
  • 9
  • 15
  • does that mean that you can't use padrino's form_for in an erb (a helper method which returns a string)? http://www.padrinorb.com/guides/application-helpers#formbuilders – Pat Newell Sep 23 '13 at 12:55
  • Sure you can. Note a dash before form_for helper call in examples. You could use it the same way in your code: ```<%- test_method do %>``` – icanhazbroccoli Sep 23 '13 at 12:59
  • thanks for the help @4pcbr. I must have some non-standard erb rendering engine, because I get the following with the `<%- %>` syntax: `undefined method '-@' for #` -- an error message for which it is quite difficult to search. – Pat Newell Sep 25 '13 at 02:24
  • Does it work if you remove ```<%-``` and use it just as ```<% ... %>```? – icanhazbroccoli Sep 25 '13 at 08:10