调整bootstrap到simple_form

时间:2015-05-23 10:53:51

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

我想改编自举代码

<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>

使用simple_form。

我试过像

这样的东西
<%= 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 %>

但没有成功。有人可以帮我解决吗?

2 个答案:

答案 0 :(得分:0)

Simpleform需要一些配置才能与bootstrap很好地配合。

创建一个初始化器并配置simpleform以使用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

可在此处找到更多信息:https://github.com/plataformatec/simple_form/wiki/How-to-use-Bootstrap-3-input-group-in-Simple-Form

答案 1 :(得分:0)

你不应该直接在视图中说User.new,在其控制器中执行此操作, 在您的情况下,用户控制器,例如@user = User.new

并在您的观看中

<%= 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 %>