Forum: Ruby on Rails Some help for form builder?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Fc582698581884352e745d1d4c64699d?d=identicon&s=25 Joshua Muheim (josh)
on 2007-04-01 22:27
Hi all

I'd like to group my fields on a form into visual separate areas. My
plan is to surround fields that belong together with fieldset/legend
tags:

<fieldset>
  <legend>Personal Information</legend>
  form.text_field :name
  form.text_field :email
</fieldset>

<fieldset>
  <legend>Additional Information</legend>
  form.text_field :xxx
  form.text_field :yyy
</fieldset>

I'd like to add a helper method to the form builder that creates this
fieldset/legend tag pair:

<% form.visual_group('Personal Information').do %>
  form.text_field :name
  form.text_field :email
<% end %>

<% form.visual_group('Additional Information').do %>
  form.text_field :xxx
  form.text_field :yyy
<% end %>

I've come so far with my poor Ruby knowledge:

def bla(caption)
  "<fieldset><legend>#{caption}</legend>#{yield}</fieldset>"
end

Sadly this does not really work - the fieldset/legend tags are not
written to the template...

How can I achieve this behavior? :-) Thanks
Josh
B0d6fda6fabb8cda27b5e6dde2dc2ac3?d=identicon&s=25 maner (Guest)
on 2007-04-02 10:51
Joshua Muheim wrote:
> ....
> I've come so far with my poor Ruby knowledge:
>
> def bla(caption)
>   "<fieldset><legend>#{caption}</legend>#{yield}</fieldset>"
> end
>
> Sadly this does not really work - the fieldset/legend tags are not
> written to the template...
>
> How can I achieve this behavior? :-) Thanks
> Josh

I haven't time to check what you misses, but for obtaining the same
result, I use the following helper method:

  def fieldset_tag(legend, options = {}, &block)
    concat(content_tag('fieldset', content_tag('legend',legend) +
capture(&block), options), block.binding)
  end

Regards
 Massimo
 http://www.addsw.it
Fc582698581884352e745d1d4c64699d?d=identicon&s=25 Joshua Muheim (josh)
on 2007-04-02 18:16
Thank you, but I get the following error:

undefined method `capture' for #<IncenseFormBuilder:0x2368898>
B0d6fda6fabb8cda27b5e6dde2dc2ac3?d=identicon&s=25 maner (Guest)
on 2007-04-03 10:05
Joshua Muheim wrote:
> Thank you, but I get the following error:
>
> undefined method `capture' for #<IncenseFormBuilder:0x2368898>

The method was intented to be used as

<% fieldset_tag('Personal Information') do %>
  <%= form.text_field :name %>
  <%= form.text_field :email %>
<% end %>

You can put code in app/helpers/application_helper.rb

If you, instead, want to include it into your formbuilder, then, try to
include all modules needed (like include
ActionView::Helpers::CaptureHelper, for capture method; see API)
Fc582698581884352e745d1d4c64699d?d=identicon&s=25 Joshua Muheim (josh)
on 2007-04-03 14:37
Thanks a lot for your help. :-)
This topic is locked and can not be replied to.