简体   繁体   中英

Adapt bootstrap to simple_form

I'd like to adapt bootstrap code

<div class="row">
                <div class="form-group col-lg-6">
                  <label>Nick</label>
                  <input type="text" class="form-control">
                </div>
                <div class="form-group col-lg-6">
                  <label>Email</label>
                  <input type="email" class="form-control">
                </div>
</div>

using simple_form.

I tried something like

<%= simple_form_for User.new do |f| %>
  <%= f.input :nick, label: "Nick", class: 'form-group col-lg-6' %>
  <%= f.input :email, label: "Email", class: 'form-group col-lg-6'%>
<% end %>

But without success. Could someone help me solve it?

Simpleform requires some configuration to play nicely with bootstrap.

Create an intializer and configure simpleform to work with bootstrap:

# File here config/initializers/simple_form_bootstrap.rb
SimpleForm.setup do |config|
  config.wrappers :vertical_input_group, tag: 'div', class: 'form-group', error_class: 'has-error' do |b|
    b.use :html5
    b.use :placeholder
    b.use :label, class: 'control-label'

    b.wrapper tag: 'div' do |ba|
      ba.wrapper tag: 'div', class: 'input-group col-sm-12' do |append|
        append.use :input, class: 'form-control'
      end
      ba.use :error, wrap_with: { tag: 'span', class: 'help-block' }
      ba.use :hint,  wrap_with: { tag: 'p', class: 'help-block' }
    end
  end

  config.wrappers :horizontal_input_group, tag: 'div', class: 'form-group', error_class: 'has-error' do |b|
    b.use :html5
    b.use :placeholder
    b.use :label, class: 'col-sm-3 control-label'

    b.wrapper tag: 'div', class: 'col-sm-9' do |ba|
      ba.wrapper tag: 'div', class: 'input-group col-sm-12' do |append|
        append.use :input, class: 'form-control'
      end
      ba.use :error, wrap_with: { tag: 'span', class: 'help-block' }
      ba.use :hint,  wrap_with: { tag: 'p', class: 'help-block' }
    end
  end
end

More information can be found here: https://github.com/plataformatec/simple_form/wiki/How-to-use-Bootstrap-3-input-group-in-Simple-Form

You shouldn't say User.new directly into view,do this in its controller, in your case User controller,say something like @user = User.new

and in your views

<%= simple_form_for @user, html: { multipart: true } do |f| %>
  <%= f.input :nick, label: "Nick", class: 'form-group col-lg-6' %> #i presume :nick is your tables name,instead of saying :name.
  <%= f.input :email, label: "Email", class: 'form-group col-lg-6'%>
<% end %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM