Rails 表单 - 'Other' 带有复选框/单选按钮的选项?
Rails forms - 'Other' option with checkboxes / radio buttons?
我正在尝试在 Rails 中的表单中添加一个 "Other" 选项,用户可以在其中输入自己的文本。任何帮助/想法都会很有帮助。
<%= simple_form_for(resource, as: resource_name, url:
registration_path(resource_name), html: { method: :put }) do |f| %>
<%= f.input :apples,
label: "How many apples?",
required: true,
as: :radio_buttons,
collection: [['0','0'], ['1', '1'], ['Other' , '**user input**']],
input_html: {class: "form-basic" } ,
item_wrapper_class: 'inline' %>
我建议创建一个名为 "other_option" 的单独字段,并在表单中创建一个数字字段输入。在您的单选按钮输入中,为选项 'Other' 提供一个类似于“-1”的值。然后在您的控制器操作中,检查此单选按钮输入的值。如果它是“-1”,则用用户在 "other_option" 字段中输入的值覆盖该值。您可能需要添加一些 js 验证和 css 以使其看起来更好。
我正在尝试在 Rails 中的表单中添加一个 "Other" 选项,用户可以在其中输入自己的文本。任何帮助/想法都会很有帮助。
<%= simple_form_for(resource, as: resource_name, url:
registration_path(resource_name), html: { method: :put }) do |f| %>
<%= f.input :apples,
label: "How many apples?",
required: true,
as: :radio_buttons,
collection: [['0','0'], ['1', '1'], ['Other' , '**user input**']],
input_html: {class: "form-basic" } ,
item_wrapper_class: 'inline' %>
我建议创建一个名为 "other_option" 的单独字段,并在表单中创建一个数字字段输入。在您的单选按钮输入中,为选项 'Other' 提供一个类似于“-1”的值。然后在您的控制器操作中,检查此单选按钮输入的值。如果它是“-1”,则用用户在 "other_option" 字段中输入的值覆盖该值。您可能需要添加一些 js 验证和 css 以使其看起来更好。