In Rails 3 the only way to get for content captured with the content_for method in a view is to call yield from within a view. However, sometimes is it useful to get to this data from within a helper (for instance to provide a default value). Calling yield from within the helper method won''t work, and accessing the @_content_for variable directly is not encouraged. Proposed solution: add a captured_content_for method so helper methods can legitimately access captured content. Add to /action_pack/lib/ action_view/helpers/capture_helper.rb # Get content previously captured with +content_for+. This is the same as calling # +yield+ inside a template except that this method can be used inside helper methods. def captured_content_for(name) @_content_for(name) end Lighthouse ticket: https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/4537 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
For the benefit of those who did not check the lighouse ticket, this functionality is apparently already in Rails 3, by calling content_for without a block or second parameter. This is in fact very deliberate, as evidenced by the third line of code in the function as it stands when this was written, but just was not documented in the rdoc. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
Simply calling content_for(:name) should work :) Yehuda Katz Architect | Engine Yard (ph) 718.877.1325 On Wed, May 5, 2010 at 4:55 PM, Brian Durand <brian@embellishedvisions.com>wrote:> In Rails 3 the only way to get for content captured with the > content_for method in a view is to call yield from within a view. > However, sometimes is it useful to get to this data from within a > helper (for instance to provide a default value). Calling yield from > within the helper method won''t work, and accessing the @_content_for > variable directly is not encouraged. > > Proposed solution: add a captured_content_for method so helper methods > can legitimately access captured content. Add to /action_pack/lib/ > action_view/helpers/capture_helper.rb > > # Get content previously captured with +content_for+. This is the > same as calling > # +yield+ inside a template except that this method can be used > inside helper methods. > def captured_content_for(name) > @_content_for(name) > end > > Lighthouse ticket: > https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/4537 > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Core" group. > To post to this group, send email to rubyonrails-core@googlegroups.com. > To unsubscribe from this group, send email to > rubyonrails-core+unsubscribe@googlegroups.com<rubyonrails-core%2Bunsubscribe@googlegroups.com> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-core?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.