Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrapper mappings in simple_form

In the simple_form example repo on github, there is a block containing the wrapper information. https://github.com/rafaelfranca/simple_form-bootstrap/blob/master/app/views/examples/_horizontal_form_sf.html.erb

It works fine, but seems to be a lot of extra setup to add for every form. Is there some way to add name this information and add it as a parameter?

<%= simple_form_for @user_horizontal, url: create_horizontal_examples_url, as:     'user_horizontal', html: { class: 'form-horizontal' },
  wrapper: :horizontal_form,
  wrapper_mappings: {
    check_boxes: :horizontal_radio_and_checkboxes,
    radio_buttons: :horizontal_radio_and_checkboxes,
    file: :horizontal_file_input,
    boolean: :horizontal_boolean
  } do |f| %>
  <%= f.error_notification %>
  <%= f.input :email, placeholder: 'Email' %>
like image 639
port5432 Avatar asked Jul 19 '14 09:07

port5432


1 Answers

At a view helper, add:

def wrapped_form(resource, options = {}, &block)
  options[:html] = { class: 'form-horizontal'}
  options[:wrapper] = :horizontal_form
  options[:wrapper_mappings] = {
    check_boxes: :horizontal_radio_and_checkboxes,
    radio_buttons: :horizontal_radio_and_checkboxes,
    file: :horizontal_file_input,
    boolean: :horizontal_boolean
  }
  simple_form_for(resource, options, &block)
end

And then you can just use like this:

<%= wrapped_form @user_horizontal, url: create_horizontal_examples_url, as: 'user_horizontal' do |f| %>
  <%= f.error_notification %>
  <%= f.input :email, placeholder: 'Email' %>
<% end %>
like image 189
Maurício Linhares Avatar answered Sep 16 '22 12:09

Maurício Linhares