tôi luôn luôn sử dụng giấy gói mà Mẫu Simple Bootstrap sử dụng.
Một ví dụ từ wrapper mặc định của nó:
SimpleForm.setup do |config|
config.error_notification_class = 'alert alert-danger'
config.button_class = 'btn btn-default'
config.boolean_label_class = nil
# :vertica_from wrapper
config.wrappers :vertical_form, tag: 'div', class: 'form-group row', 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: 'control-label'
b.use :input, class: 'form-control'
b.use :error, wrap_with: { tag: 'span', class: 'help-block' }
b.use :hint, wrap_with: { tag: 'p', class: 'help-block' }
end
# Rest omitted
# Change default simple form wrapper settings
config.default_wrapper = :vertical_form
config.wrapper_mappings = {
check_boxes: :vertical_radio_and_checkboxes,
radio_buttons: :vertical_radio_and_checkboxes,
file: :vertical_file_input,
boolean: :vertical_boolean,
datetime: :multi_select,
date: :multi_select,
time: :multi_select
}
end
Khi bạn tạo ra một hình thức đơn giản
<%= form_for(resource, as: resource_name, url: unlock_path(resource_name), html: { method: :post }) do |f| %>
Nó sẽ theo mặc định (vì các thiết lập) để sử dụng :vertical_form
Bây giờ nếu bạn muốn thiết lập trình bao bọc tùy chỉnh, hãy làm theo ví dụ trên, tạo lớp tùy chỉnh của bạn và đặt nó dưới config/initializers
.Đây là một wrapper dụ tùy chỉnh Tôi thêm rằng Bootstrap thiết lập ở trên:
config.wrappers :horizontal_file_input, tag: 'div', class: 'form-group row', error_class: 'has-error' do |b|
b.use :html5
b.use :placeholder
b.optional :maxlength
b.optional :readonly
b.wrapper tag: 'div', class: 'col-md-6' do |bb|
bb.use :label, class: 'col-sm-5 control-label'
bb.wrapper tag: 'div', class: 'col-sm-7' do |bbb|
bbb.use :input
bbb.use :hint, wrap_with: { tag: 'p', class: 'help-block' }
end
end
b.wrapper tag: 'div', class: 'col-md-3 side-validation' do |bc|
bc.use :error, wrap_with: { tag: 'span', class: 'help-block' }
end
end
Sau đó, để sử dụng wrapper này, hãy tìm đầu vào nơi bạn muốn áp dụng wrapper tùy chỉnh, và làm điều này:
<%= f.input :resume, as: :attachment_preview, wrapper: :horizontal_file_input %>
Boom ! Nó sẽ được hiển thị với cài đặt tùy chỉnh của bạn! Ngoài ra, bạn có thể đặt trình bao bọc thành biểu mẫu để thay đổi trình bao bọc mặc định cho tất cả các đầu vào của nó. Vì vậy, nếu bạn làm:
<%= simple_form_for(@staff, as: :staff,
url: staffs_path,
method: "post",
wrapper: :horizontal_form) do |f| %>
Sau đó, tất cả các lĩnh vực đầu vào của nó sẽ mặc định sử dụng :horizontal_form
wrapper (mà cũng là một đơn giản Mẫu Bootstrap wrapper)
Hope this helps.
bạn có muốn hình ảnh nội tuyến không? – Chandrakant
chỉ đơn giản, muốn một hình ảnh bên cạnh trường văn bản ... –
là có thể tiêm mẫu html? –