simple_form中的包装器映射

时间:2014-07-19 09:14:45

标签: ruby-on-rails simple-form

在github上的 simple_form 示例repo中,有一个包含包装器信息的块。 https://github.com/rafaelfranca/simple_form-bootstrap/blob/master/app/views/examples/_horizontal_form_sf.html.erb

它工作正常,但似乎需要为每个表单添加许多额外的设置。 有没有办法添加名称此信息并将其添加为参数?

<%= simple_form_for @user_horizontal, url: create_horizontal_examples_url, as:     'user_horizontal', html: { class: 'form-horizontal' },
  wrapper: :horizontal_form,
  wrapper_mappings: {
    check_boxes: :horizontal_radio_and_checkboxes,
    radio_buttons: :horizontal_radio_and_checkboxes,
    file: :horizontal_file_input,
    boolean: :horizontal_boolean
  } do |f| %>
  <%= f.error_notification %>
  <%= f.input :email, placeholder: 'Email' %>

1 个答案:

答案 0 :(得分:15)

在视图助手处,添加:

def wrapped_form(resource, options = {}, &block)
  options[:html] = { class: 'form-horizontal'}
  options[:wrapper] = :horizontal_form
  options[:wrapper_mappings] = {
    check_boxes: :horizontal_radio_and_checkboxes,
    radio_buttons: :horizontal_radio_and_checkboxes,
    file: :horizontal_file_input,
    boolean: :horizontal_boolean
  }
  simple_form_for(resource, options, &block)
end

然后你就可以这样使用:

<%= wrapped_form @user_horizontal, url: create_horizontal_examples_url, as: 'user_horizontal' do |f| %>
  <%= f.error_notification %>
  <%= f.input :email, placeholder: 'Email' %>
<% end %>