使用simple_form构建自定义配置

时间:2014-09-11 04:48:30

标签: ruby-on-rails ruby simple-form

我使用rails default form_for构建了以下表单:

= form_for @customer do |f|
  .form-group
    .col-sm-3
      = f.label :first_name, "Vorname", class: "control-label"
    .col-sm-3
      = f.text_field :first_name, placeholder: "Ihr Vorname", class: 'form-control'

现在我想为输入构建simple_form配置,并且label具有相同的类,而simple_form_for看起来与form_for相同。这段代码给了我以下html:

          <form accept-charset="UTF-8" action="/portal/sessions" class="new_customer" id="new_customer" method="post"><div style="display:none"><input name="utf8" type="hidden" value="&#x2713;" /><input name="authenticity_token" type="hidden" value="LeRWJSnYDYvzeW5+KMm9BczbtfHXX6yI6Pg4xLjH2SE=" /></div>    <div class='row'>
              <div class='col-md-12'>
                <div class='form-group'>
                  <div class='col-sm-3'>
                    <label class="control-label" for="customer_first_name">Vorname</label>
                  </div>
                  <div class='col-sm-3'>
                    <input class="form-control" id="customer_first_name" name="customer[first_name]" placeholder="Ihr Vorname" type="text" />
                  </div>
                </div>
              </div>
            </div>
            <input name="commit" type="submit" value="Create Customer" />
        </form>

我试图自己编写simple_form配置,但它无法正常工作:

config.wrappers :bootstrap, error_class: 'error' do |b|
    b.use :html5
    b.use :placeholder
    b.use :label, class: 'control-label'
    b.wrapper tag: 'div', class: 'form-group' do |ba|
      ba.use :input, class: 'form-controlc'
      ba.use :error, wrap_with: { tag: 'span', class: 'help-inline' }
      ba.use :hint,  wrap_with: { tag: 'p', class: 'help-block' }
    end
  end

有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

答案是:

SimpleForm.setup do |config|
  config.wrappers :bootstrap, tag: 'div', class: 'control-group', error_class: 'error' do |b|
    b.use :html5
    b.use :placeholder
    b.use :label
    b.wrapper tag: 'div', class: 'controls' do |ba|
      ba.use :input
      ba.use :error, wrap_with: { tag: 'span', class: 'help-inline' }
      ba.use :hint,  wrap_with: { tag: 'p', class: 'help-block' }
    end
  end
 config.default_wrapper = :bootstrap
  config.label_class = 'control-label'
  config.input_class = 'form-control'
end