2
votes

When using the Simple Form gem, is it possible to specify a wrapper and its HTML? I don't want to manually create a <div> via HTML, but want to learn if there are wrapper options to pass into simple_fields_for.

Given

<%= f.simple_fields_for :xxx, do |xxx| %>
  ...
<% end %>

I want the simple_fields_for to create a wrapper, <div class="xyz">...</div> around any code output inside it

Similar issues:

2
Do you want the DIV inside or outside the form?max
Inside the form. simple_fields_for is for a nested form, which is inside a parent form. I want the div to wrap this nested form.James L.
Sorry I though you meant simple_form.max

2 Answers

2
votes

The easiest way is probably just to create a method that wraps simple_fields_for.

# config/initializers/special_form_builder.rb
module SpecialFormBuilder
  def special_fields_for(record_name, record_object = nil, options = {}, &block)
    super.simple_fields_for(record_name, record_object = nil, options = {}) do
      content_tag :div, class: "xyz" do
        block.call
      end
    end
  end
end

module SimpleForm
  class FormBuilder
    include SpecialFormBuilder
  end
end
2
votes

I'm facing this issue and the given solution does not work, the problem is that the hidden id field is rendered outside of the wrapper element.

It seems that the problem is not simple_form, the problem is in rails within the FormHelper#fields_for_nested_model method.

Also, simple form accept a wrapper option on simple_form_for, but it defines the default wrapper used in the nested form inputs, not a wrapper for the form itself.

The only solution I see, is to use the option include_id: false and add the hidden field within the wrapper tag.