繁体   English   中英

使引导程序适应simple_form

[英]Adapt bootstrap to 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 %>

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

Simpleform需要一些配置才能与Bootstrap完美配合。

创建一个初始化器并配置simpleform以使用引导程序:

# 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

您不应该直接在视图中说出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 %>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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