[英]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.