我正在尝试在Rails中的一个表单中设置一个"其他"选项,用户可以在该表单中输入自己的文本。任何帮助/想法都会很有帮助。
<%= 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以使其看起来更好。