【发布时间】:2015-02-01 20:06:07
【问题描述】:
我有一个大型 simple_form 表单,其中的字段需要启用或禁用,具体取决于表单的部分加载位置。
我的问题是:如何使用 simple_form 帮助器/包装器快速禁用每个表单输入?
Simple Form's documentation 解释了如何使用disabled: true 禁用单个输入字段:
<%= simple_form_for @user do |f| %>
<%= f.input :username, disabled: true %>
<%= f.button :submit %>
<% end %>
但文档不太清楚我如何通过 simple_form 帮助程序禁用整个表单,而无需在实际上每个表单输入上重复disabled: true。
我尝试将 disabled: true 和 readonly: true 传递给 simple_form 的 :wrapper_mappings 选项,但这不起作用。
示例代码:
我通过部分加载表单来定义 simple_form 显示变量。这有效:
#user/show.html.erb:
<%= render partial: 'shared/form', locals: {questionnaire: @questionnaire, readonly_state: true, disabled_state: true, bootstrap_form_class: 'form-horizontal'} %>
但是,readonly_state 和 disabled_state 不起作用,除非我将它们传递给每个表单输入:
# shared/_form.html.erb
<%= simple_form_for(@questionnaire, :html => {:class => bootstrap_form_class},
:wrapper_mappings => {check_boxes: :vertical_radio_and_checkboxes, file: :vertical_file_input,
boolean: :vertical_boolean }) do |f| %>
<%= f.input :username, disabled: disabled_state, hint: 'You cannot change your username.' %>
<%= f.input :email, disabled: disabled_state %>
<%= f.input :city, disabled: disabled_state %>
<%= f.input :country, disabled: disabled_state %>
. . .
<%= f.button :submit %>
<% end %>
您可以快速看到大型表格的重复性。
如何使用 DRY 代码在整个表单中快速切换 disable 和 readonly 表单属性?
【问题讨论】:
标签: html ruby-on-rails forms simple-form disabled-input