gpt4 book ai didi

ruby-on-rails - simple_form 中的封装映射

转载 作者:行者123 更新时间:2023-12-04 13:23:02 27 4
gpt4 key购买 nike

simple_form github 上的示例 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' %>

最佳答案

在 View 助手中,添加:

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 %>

关于ruby-on-rails - simple_form 中的封装映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24838716/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com