I have been trying my hand at custom form builder methods in Rails. My goal is similar to formtastic's f.inputs
method. Here is my code so far:
class Builder < ActionView::Helpers::FormBuilder
def fieldset(name)
@template.content_tag :fieldset, :class => 'inputs' do
@template.content_tag :div, :class => 'legend' do
@template.content_tag :span, name
end
@template.content_tag :ol do
yield
end
end
end
def input_field(name, classes='', *args)
@template.content_tag :li, class: "input #{classes}" do
label(name) + text_field(name)
end
end
end
So then I can build forms in HAML like so:
= f.fieldset "General Info" do
= f.input_field :name
= f.input_field :slug, 'last'
The problem is the rendered HTML drops the legend DIV.
<fieldset class="inputs">
<ol>
<li class="input ">
<label for="content_instance_name">Name</label>
<input id="content_instance_name" name="content_instance[name]" size="30" type="text" value="Home Page">
</li>
<li class="input last">
<label for="content_instance_slug">Slug</label>
<input id="content_instance_slug" name="content_instance[slug]" size="30" type="text" value="home-page">
</li>
</ol>
</fieldset>
Seems like I'm missing something.
Thanks in advance.
SOLUTION
Here is the adjusted fieldset method:
def fieldset(name)
@template.content_tag :fieldset, :class => 'inputs' do
legend = @template.content_tag :div, @template.content_tag(:span, name), :class => "legend"
legend += @template.content_tag :ol do
yield
end
end
end
Only the return value of the block is rendered. Add a +
so both your content_tag
calls are returned. Similar question here.