简体   繁体   English

Ruby on Rails Form 添加带有文本框的“其他”选项到 Select 下拉列表

[英]Ruby on Rails Form Add an "Other" Option with Text Box to Select Dropdown

I have a ruby form that includes the following code我有一个 ruby 表单,其中包含以下代码

        <%= form.label(:emitter_type, class: "form-label") %>
        <%= form.select(:emitter_type, BoilerPlant.emitter_type , include_blank: true) %>

        <% if boiler_plant.emitter_type != "Other" %>
        <div id="emitter_other", style="display:none;">
          <% else %>
          <div id="emitter_other">
            <% end %>


          <%= form.label(:emitter_type_other, class: "form-label") %>
          <%= form.text_field(:emitter_type_other , autocomplete: "off", autocapitalize: "on", class: "form-input") %>

          </div>

BoilerPlant.emitter_type Is the following call to a function in my model that produces the array of options I use in my dropdown: BoilerPlant.emitter_type是对我的 model 中的 function 的以下调用,它生成我在下拉列表中使用的选项数组:

  def self.emitter_type
    ["Baseboard", "Fan Coil", "Heat Pump", "Other"]
  end

I use javascript to make the text field emitter_type_other appear when "Other" is selected.我使用javascript来使“其他”选中“其他”时出现文本字段emitter_type_other Below is the javascript I use to set the value of my select field to the value I type in the text field (ignore the odd naming conventions).下面是我用来将 select 字段的值设置为我在文本字段中键入的值的 javascript(忽略奇怪的命名约定)。

var toggleRevertValues = document.getElementById("plant_submit_button");
var deleteEmitterType = document.getElementById("boiler_plant_emitter_type");
var deleteEmitterOther = document.getElementById("boiler_plant_emitter_type_other");

toggleRevertValues.addEventListener("click", function(e) { 

 if ( deleteEmitterType.value == "Other" ) {
                deleteEmitterType.value = deleteEmitterOther.value;
            }
}

The dropdown populates with a value if I type in the exact name of one of the options that already exists in my array (such as "Baseboard").如果我输入我的数组中已存在的选项之一的确切名称(例如“底板”),下拉列表将填充一个值。 However, if I type in a new option, it shows up as null in the database, and my dropdown comes up as blank on an invalid submit.但是,如果我输入一个新选项,它在数据库中显示为 null,并且我的下拉列表在无效提交时显示为空白。 Is there any way to change my code so it allows a new value for the select?有什么方法可以更改我的代码,以便它允许 select 的新值?

In order to add a new emitter_type when user selects "Other" and types a new emitter type is to create a couple of new models: EmitterType' and 'BoilerEmitterType .为了在用户选择“其他”并键入新的发射器类型时添加新的发射器类型,需要创建几个新模型: EmitterType' and 'BoilerEmitterType The second is just a join table.第二个只是一个连接表。

class Boiler < ActiveRecord::Base
  has_many :boiler_emitter_types, dependent: :destroy
  has_many :emitter_types, through: :boiler_emitter_types
end

class BoilerEmitterType < ActiveRecord::Base
  belongs_to :boiler
  belongs_to :emitter_type
end

class EmitterType
  has_many :boiler_emitter_types, dependent: :destroy
  has_many :boilers, through: :boiler_emitter_types
end

Then in response to the form submission, you either assign an existing EmitterType to the Boiler instance, or create a new EmitterType and assign it to the Boiler instance.然后,为了响应表单提交,您可以将现有的EmitterType分配给Boiler实例,或者创建一个新的EmitterType并将其分配给Boiler实例。

ActiveRecord has a nice method that will do this for you: ActiveRecord 有一个很好的方法可以为你做到这一点:

# boilers_controller.rb
def update
  @boiler = Boiler.find(boiler_params(:id))
  @emitter_type = EmitterType.find_or_create_by(name: boiler_params(:emitter_type_name)
  @boiler.emitter_types << @emitter_type
  if @boiler.save
    # do something
  else
    # do something else
  end
end

private
def boiler_params
# create the 'strong parameters' here
end

note, we're looking for the emitter type by name, not by id, which would be the typical response from a select option.请注意,我们正在按名称而不是 ID 查找发射器类型,这将是 select 选项的典型响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在下拉列表中添加“其他”作为输入文本选项 - Add “other” as a input text option to dropdown list 如何在选择选项下拉菜单中添加输入文本 - How to add input text in select option dropdown 请帮助我.. RE:*FORM FIELDS* 我想隐藏()我的“类型=文本框”,直到在下拉选项中选择其他选项 - PLEASE assist me.. RE: *FORM FIELDS* I want to Hide() my "type=text box" until the Other option is chosen in the dropdown option JavaScript / jQuery:选择列表/下拉值值时添加文本框是“其他” - Javascript/JQuery: Add text box when select list/dropdown value is "other" 选择其他选择下拉选项 - Select the option of other select dropdown 用户可以通过在文本框中键入下拉列表来添加选项 - User is able to add option to dropdown list by typing it in a text box 如果其他选择更改,AngularJS将删除并添加选择框选项 - AngularJS remove & add select box option if other selection changes 如果其他选择更改,请删除并添加选择框选项 - remove & add select box option if other selection changes 使用表单中的文本填充表单的选择框 <option> CasperJS的标签 - Filling a select box of a form using the text inside the <option> tags with CasperJS 更改选择框选项文本 - Change select box option text
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM