James L.
James L.

Reputation: 14515

Specifying a wrapper in simple_fields_for

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:

Upvotes: 2

Views: 1349

Answers (2)

eloyesp
eloyesp

Reputation: 3275

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.

Upvotes: 2

max
max

Reputation: 101811

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

Upvotes: 2

Related Questions