views:

31

answers:

3

Rails 2.2.2

_competition.html.erb

<p>This is packed</p>
<% pack do %>
// some javascript
<% end %>

competitions_helper.rb

def pack(&block)
  yield + 'PACKED'
end

However 'PACKED' does not get appended to my javascript string, its as if pack do has no effect. It seems like there is some oddness going off with context of the blocks and the actual view rendering because if I remove the call to yield in the helper and return a hard coded string I get no output from pack.

A: 

I didn't test it, but you could try:

yield << 'PACKED'

It is just a different way of combining 2 strings.

PlanetMaster
Thanks but, the issue isn't with the way the strings are concat'd. If I do not yield, and just return a string it works as expected. Calling yield seems to re-render the partial (which calls the helper).
Kris
A: 

As a work around, for the time being, putting the javascript in another partial and passing it to the helper function using render_to_string, ie. without the need to call yield works.

<%= pack(render_to_string :partial => '_competition_javascript') %>
Kris
A: 
def pack(&block)
  concat(capture(&block) + 'PACKED')
end
Simone Carletti