Hello, I''m having a hard time understanding how you are supposed to write helpers that take blocks now in Rails 3. According to 7.4.2 here: http://edgeguides.rubyonrails.org/3_0_release_notes.html you are supposed to write your helpers to return their result as text, rather than append to the accumulating content. How do I execute the code in the block that has been passed to the helper without it appending to the content? My instincts, which are clearly wrong, are to just use "yield" like I would in any other method that takes a block. However, within the context of a helper method. "yield" seems to have a side effect of appending to the accumulating content. So, I have to do the following to suppress the intended output of the helper because the yield is already appending it: module FooHelper def none_if_empty yield return '''' end end What is the proper way to write this helper in Rails 3? Thanks, Avram -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
I haven''t tested this in rails3, but this is what I do to capture the output of a block in rails 2. #my_helper.rb if block_given? block_text = capture(&block) end #view_file.html.erb <% my_helper do %> ##anything output here is captured and returned to block_text <% end %> Hope that helps. I wasn''t sure if your question was rails3 specific or if you just happened to be working with rails 3. Luke On Sep 3, 10:38 am, Avram Dorfman <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hello, > > I''m having a hard time understanding how you are supposed to write > helpers that take blocks now in Rails 3. > > According to 7.4.2 here: > > http://edgeguides.rubyonrails.org/3_0_release_notes.html > > you are supposed to write your helpers to return their result as text, > rather than append to the accumulating content. > > How do I execute the code in the block that has been passed to the > helper without it appending to the content? My instincts, which are > clearly wrong, are to just use "yield" like I would in any other method > that takes a block. However, within the context of a helper method. > "yield" seems to have a side effect of appending to the accumulating > content. So, I have to do the following to suppress the intended output > of the helper because the yield is already appending it: > > module FooHelper > def none_if_empty > yield > return '''' > end > end > > What is the proper way to write this helper in Rails 3? > > Thanks, > Avram > -- > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hi Luke, We''re having this problem with Rails 3. Helpers what worked in Rails 2 are basically this: def helper(&block) block.call # Returns the entire partial up to the helper call twice end Adding return '''' to the end helps, but as Avram said, this is only the behavior I''d expect if the helper was called with "<%= %>". In our testing, calling with and without the = made no difference. Best, Milan On Sep 5, 1:12 pm, Luke Cowell <lcow...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I haven''t tested this in rails3, but this is what I do to capture the > output of a block in rails 2. > > #my_helper.rb > if block_given? > block_text = capture(&block) > end > > #view_file.html.erb > <% my_helper do %> > ##anything output here is captured and returned to block_text > <% end %> > > Hope that helps. I wasn''t sure if your question was rails3 specific or > if you just happened to be working with rails 3. > > Luke > > On Sep 3, 10:38 am, Avram Dorfman <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > > > > > > > > > Hello, > > > I''m having a hard time understanding how you are supposed to write > > helpers that take blocks now in Rails 3. > > > According to 7.4.2 here: > > > http://edgeguides.rubyonrails.org/3_0_release_notes.html > > > you are supposed to write your helpers to return their result as text, > > rather than append to the accumulating content. > > > How do I execute the code in the block that has been passed to the > > helper without it appending to the content? My instincts, which are > > clearly wrong, are to just use "yield" like I would in any other method > > that takes a block. However, within the context of a helper method. > > "yield" seems to have a side effect of appending to the accumulating > > content. So, I have to do the following to suppress the intended output > > of the helper because the yield is already appending it: > > > module FooHelper > > def none_if_empty > > yield > > return '''' > > end > > end > > > What is the proper way to write this helper in Rails 3? > > > Thanks, > > Avram > > -- > > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
If you look at the compiled template, any <% %> blocks are compiled to something like this: _buf << ''<div ...'' Hence if you had the following (silly) ERB: <% foo do |b| %> <li <%= b %> > <% end %> the block portion would get compiled to: _buf << ''<li ''; _buf << ( b ) _buf << '' >'' Hence, if you yield to this block, the return value of the yield will be _buf. _buf contains the portion of the template that has been rendered so far. And if the yield is the last statement, then the implicit return value will be _buf. So far so good. Now you''re wondering, hey, the output should not be in the template unless you''re using <%= %>, right? This should be true, but in the interest of backwards compatibility, Rails 3 has append_if_string= - this appends the return value of <% %> blocks if the value is a string (so old form_for''s, etc. don''t break). _buf is conveniently a string as well, and gets added to the page. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.