新手问题 simple_form 死活不横排

syutran · 2017年04月12日 · 最后由 hz_qiuyuanxin 回复于 2017年04月13日 · 1412 次阅读

simple_form 3.4 bootstrap 4.0.0 alpha6

<%= simple_form_for(@user, :html => { :class => 'form-horizontal' }) do |f| %>  

死活不横排,是不是我用的 bootstrap 版本太高了?

rails generate simple_form:install --bootstrap

安装 simple_form 时明明说好可以 html => { :class => 'form-horizontal' } 的~

所有的 helper 包括前端 gem,最终的目标都是生成 HTML,显示成什么样子是由最终生成的 HTML 是什么样子决定的

虽然这里可以说确实是你 bootstrap 版本高了,但这个结论不需要通过经验,通过页面 HTML 源码对照 Bootstrap 的文档就可以得出来

看 HTML 啊!

wrapper 定义了吗?

SimpleForm.setup do |config|
  config.wrappers :horizontal_form, tag: 'div', class: 'form-group', error_class: 'has-error' do |b|
    b.use :html5
    b.use :placeholder
    b.optional :maxlength
    b.optional :pattern
    b.optional :min_max
    b.optional :readonly
    b.use :label, class: 'col-sm-3 control-label'

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

楼主还是从 form_for 用起吧,步子不要迈得太大!

需要 登录 后方可回复, 如果你还没有账号请 注册新账号