Accessing Rails helper method with_output_buffer

Hi all,

I cannot figure out how to correctly use the with_output_buffer helper
method (located in ActionView::Helpers::CaptureHelper) inside a custom
FormBuilder.

Below follows a simplified example.

I’m trying to achieve the following behavior in an ERB as suggested in
the form_for helper docs in the Rails source:

==========
<%= form_for @object, :builder => MyFormBuilder do |my_form| %>
<%= my_form.my_dummy_div_wrapper do %>
<%= … %>
<%= end %>
<% end %>

Here’s the helper:

module MyHelper
class MyFormBuilder < ActionView::Helpers::FormBuilder
def my_dummy_div_wrapper(&block)
output = “

”.html_safe
  # here I should use with_output_buffer to correctly get the

block’s output
output.safe_concat(with_output_buffer &block)

  output.safe_concat "</div>"

  output
end

end
end

Inside the MyFormBuilder methods, there’s no with_output_buffer method
defined so I cannot use it (and a bunch of other Rails helpers; cannot
render a partial, etc…).

I already spent two days studying the Rails source, tried lots of
things, no luck so far.

I don’t think what I’m trying to accomplish is wrong: I’m not calling
view helper methods from models etc.

Any help a life saver!

Thank you,
Martin

On Sep 13, 11:00am, Gogov [email protected] wrote:

Inside the MyFormBuilder methods, there’s no with_output_buffer method
defined so I cannot use it (and a bunch of other Rails helpers; cannot
render a partial, etc…).

You need to use builder’s @template ivar - this is the view that was
being rendered so has all the helpers you need (if you look at how a
form builder’s radio_button, check_box etc. methods are implemented,
they just call the corresponding method on @template)

Fred

Thank you very much, Frederick!

I’m into my day 5 digging into Rails rendering internals (writing a
plugin) but I didn’t think of that.
This part of Rails seems like it needs some tidying…
More questions arise but I’ll try to answer them myself first.

Thank you!
Martin

On Sep 13, 1:15pm, Frederick C. [email protected]

I´m trying to run Hobo 1.3.0.pre31, with Ruby 1.9.2 and Rails 3.1.0, on
Ubunto 10.04.
So far, everytime I tried to start a new Hobo project, it failed at the
time of the initial migration with the message “rake aborted!”, followed
by:
"AssociationCollection is not a class

/usr/local/lib/ruby/gems/1.9.1/gems/hobo-1.3.0.pre31/lib/hobo/extensions/active_record/associations/collection.rb:3:in
<module:Associations>': AssociationCollection is not a class (TypeError) from /usr/local/lib/ruby/gems/1.9.1/gems/hobo-1.3.0.pre31/lib/hobo/extensions/active_record/associations/collection.rb:2:inmodule:ActiveRecord
from
/usr/local/lib/ruby/gems/1.9.1/gems/hobo-1.3.0.pre31/lib/hobo/extensions/active_record/associations/collection.rb:1:in
<top (required)>' from /usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:240:inrequire’
from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:240:in
block in require' from /usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:223:inblock in load_dependency’
from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:640:in
new_constants_in' from /usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:223:inload_dependency’
from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:240:in
require' from /usr/local/lib/ruby/gems/1.9.1/gems/hobo-1.3.0.pre31/lib/hobo/engine.rb:27:inblock in class:Engine
from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.1.0/lib/active_support/lazy_load_hooks.rb:36:in
instance_eval' from /usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.1.0/lib/active_support/lazy_load_hooks.rb:36:inexecute_hook’
from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.1.0/lib/active_support/lazy_load_hooks.rb:43:in
block in run_load_hooks' from /usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.1.0/lib/active_support/lazy_load_hooks.rb:42:ineach’
from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.1.0/lib/active_support/lazy_load_hooks.rb:42:in
run_load_hooks' from /usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/base.rb:2184:in<top (required)>’
from
/usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/migration.rb:633:in
initialize' from /usr/local/lib/ruby/gems/1.9.1/gems/hobo_fields-1.3.0.pre31/lib/generators/hobo/migration/migration_generator.rb:94:innew’
from
/usr/local/lib/ruby/gems/1.9.1/gems/hobo_fields-1.3.0.pre31/lib/generators/hobo/migration/migration_generator.rb:94:in
migrations_pending?' from /usr/local/lib/ruby/gems/1.9.1/gems/hobo_fields-1.3.0.pre31/lib/generators/hobo/migration/migration_generator.rb:47:inmigrate’
from
/usr/local/lib/ruby/gems/1.9.1/gems/thor-0.14.6/lib/thor/task.rb:22:in
run' from /usr/local/lib/ruby/gems/1.9.1/gems/thor-0.14.6/lib/thor/invocation.rb:118:ininvoke_task’
from
/usr/local/lib/ruby/gems/1.9.1/gems/thor-0.14.6/lib/thor/invocation.rb:124:in
block in invoke_all' from /usr/local/lib/ruby/gems/1.9.1/gems/thor-0.14.6/lib/thor/invocation.rb:124:ineach’
from
/usr/local/lib/ruby/gems/1.9.1/gems/thor-0.14.6/lib/thor/invocation.rb:124:in
map' from /usr/local/lib/ruby/gems/1.9.1/gems/thor-0.14.6/lib/thor/invocation.rb:124:ininvoke_all’
from
/usr/local/lib/ruby/gems/1.9.1/gems/thor-0.14.6/lib/thor/group.rb:226:in
dispatch' from /usr/local/lib/ruby/gems/1.9.1/gems/thor-0.14.6/lib/thor/invocation.rb:109:ininvoke’
from
/usr/local/lib/ruby/gems/1.9.1/gems/hobo-1.3.0.pre31/lib/generators/hobo/setup_wizard/setup_wizard_generator.rb:196:in
generate_migration' from /usr/local/lib/ruby/gems/1.9.1/gems/thor-0.14.6/lib/thor/task.rb:22:inrun’
from
/usr/local/lib/ruby/gems/1.9.1/gems/thor-0.14.6/lib/thor/invocation.rb:118:in
invoke_task' from /usr/local/lib/ruby/gems/1.9.1/gems/thor-0.14.6/lib/thor/invocation.rb:124:inblock in invoke_all’
from
/usr/local/lib/ruby/gems/1.9.1/gems/thor-0.14.6/lib/thor/invocation.rb:124:in
each' from /usr/local/lib/ruby/gems/1.9.1/gems/thor-0.14.6/lib/thor/invocation.rb:124:inmap’
from
/usr/local/lib/ruby/gems/1.9.1/gems/thor-0.14.6/lib/thor/invocation.rb:124:in
invoke_all' from /usr/local/lib/ruby/gems/1.9.1/gems/thor-0.14.6/lib/thor/group.rb:226:indispatch’
from
/usr/local/lib/ruby/gems/1.9.1/gems/thor-0.14.6/lib/thor/base.rb:389:in
start' from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.1.0/lib/rails/generators.rb:168:ininvoke’
from
/usr/local/lib/ruby/gems/1.9.1/gems/railties-3.1.0/lib/rails/commands/generate.rb:12:in
<top (required)>' from /usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:240:inrequire’
from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:240:in
block in require' from /usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:223:inblock in load_dependency’
from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:640:in
new_constants_in' from /usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:223:inload_dependency’
from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:240:in
require' from /usr/local/lib/ruby/gems/1.9.1/gems/railties-3.1.0/lib/rails/commands.rb:28:in<top (required)>’
from script/rails:6:in require' from script/rails:6:in

Please reply help me