Tôi đang cố gắng tích hợp bootstrap 3 với simple_forms (từ chính).Hộp kiểm Bootstrap 3 + simple_forms
Ngay bây giờ, tôi có như sau:
config/initializers/simple_form.rb:
SimpleForm.setup do |config|
config.wrappers :default, class: :input,
hint_class: :field_with_hint, error_class: :field_with_errors do |b|
b.use :html5
b.use :placeholder
b.optional :maxlength
b.optional :pattern
b.optional :min_max
b.optional :readonly
b.use :label_input
b.use :hint, wrap_with: { tag: :span, class: :hint }
b.use :error, wrap_with: { tag: :span, class: :error }
end
config.default_wrapper = :default
config.boolean_style = :nested
config.button_class = 'btn'
end
config/initializers/simple_form_bootstrap.rb:
SimpleForm.setup do |config|
config.input_class = 'form-control'
config.wrappers :bootstrap, tag: 'div', class: 'form-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.wrappers :prepend, tag: 'div', class: "form-group", error_class: 'error' do |b|
b.use :html5
b.use :placeholder
b.use :label
b.wrapper tag: 'div', class: 'controls' do |input|
input.wrapper tag: 'div', class: 'input-prepend' do |prepend|
prepend.use :input
end
input.use :hint, wrap_with: { tag: 'span', class: 'help-block' }
input.use :error, wrap_with: { tag: 'span', class: 'help-inline' }
end
end
config.wrappers :append, tag: 'div', class: "form-group", error_class: 'error' do |b|
b.use :html5
b.use :placeholder
b.use :label
b.wrapper tag: 'div', class: 'controls' do |input|
input.wrapper tag: 'div', class: 'input-append' do |append|
append.use :input
end
input.use :hint, wrap_with: { tag: 'span', class: 'help-block' }
input.use :error, wrap_with: { tag: 'span', class: 'help-inline' }
end
end
config.default_wrapper = :bootstrap
end
app/views/devise/sessions/new.html.haml
%div.panel.panel-auth
%div.panel-heading
%h3.panel-title Sign in
%div.panel-body
= simple_form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f|
.form-inputs
= f.input :email, :required => false, :autofocus => true
= f.input :password, :required => false
= f.input :remember_me, :as => :boolean if devise_mapping.rememberable?
.form-actions
= f.button :submit, "Sign in"
%hr
= render "devise/shared/links"
Nhưng HTML được tạo là sai. Vâng, nó đúng theo BS2, nhưng sai với BS3. Ở đây là:
<div class="form-group boolean optional user_remember_me">
<label class="boolean optional control-label" for="user_remember_me">
Remember me
</label>
<div class="controls">
<input name="user[remember_me]" type="hidden" value="0">
<label class="checkbox">
<input class="boolean optional form-control" id="user_remember_me" name="user[remember_me]" type="checkbox" value="1">
</label>
</div>
</div>
Nhưng nó thực sự cần được điều gì đó như:
<div class="checkbox">
<label>
<input type="checkbox"> Check me out
</label>
</div>
Đây có thể là có thể khắc phục điều này hacking xung quanh hàm bao, nhưng tôi không thể làm cho nó làm việc. Ai đó có thể cho tôi một số lời khuyên về điều đó không?
Cheers
đã không làm việc. Nó vẫn thêm lớp 'kiểm soát hình thức', điều này làm mọi thứ lộn xộn. :/ – caarlos0
Nó đang thêm lớp "kiểm soát biểu mẫu"? Tôi không thấy điều đó trong bộ khởi tạo của bạn ... trừ khi bạn có nghĩa là "nhóm biểu mẫu"? Nếu không, cái gì khác có thể đang xảy ra (có lẽ là một đầu vào tùy chỉnh?). Hãy chắc chắn rằng bạn khởi động lại máy chủ của bạn sau khi thay đổi initializer simple_form_bootstrap.rb mặc dù hoặc thay đổi sẽ không có hiệu lực. Chỉ cần kiểm tra một lần nữa vào cuối của tôi và nó làm việc tốt với "wrapper:: checkbox" trong dòng đầu vào. – mwalsher
Hãy nhớ rằng bạn cũng đang sử dụng nhánh chính của simple_form (phiên bản 3.0.0) ... có thể có sự thay đổi so với phiên bản được rubygems tham chiếu (2.1.0). Hãy thử thay đổi tham chiếu gemfile của bạn thành: "gem 'simple_form', github: 'plataformatec/simple_form'" và chạy gem update – mwalsher