我目前有一个创建clinicians
的表单。在此表单中,我希望获得clinician_type
的下拉列表,用户可以在physician
,nurse
和other
之间进行选择。根据用户的选择,表单将适应允许他们为Speciality
选择physician
或为Occupation
选择nurse
。
我尝试实施23459948的答案,并将其调整为有三个选项,但我不知道javascript,我无法让它工作。
每个Clinician
belongs_to
:occupation
:speciality
Occupation
和Speciality
以及has_many
和:clinicians
。
我在表单中实现此代码的代码如下所示:
<%= form_for @clinician do |form| %>
Type of new clinician: <select id="clinician_type">
<option value="physician">Physician</option>
<option value="nurse">Nurse</option>
<option value="other">Other</option>
</select><br />
<span id="webname">
<div class="form-group">
<%= form.label :speciality_id %>
<%= form.collection_select :speciality_id, Speciality.all, :id, :name, { include_blank: true }, class: "form-control" %>
</div>
</span>
<script>
document.getElementById("clinician_type").onchange = function () {
if (document.getElementById("clinician_type").options[document.getElementById("clinician_type").selectedIndex].value == "physician")
document.getElementById("webname").style.display = "block";
else
document.getElementById("webname").style.display = "none";
}
</script>
<span id="webname2">
<div class="form-group">
<%= form.label :occupation_id %>
<%= form.collection_select :occupation_id, Occupation.all, :id, :name, { include_blank: true }, class: "form-control" %>
</div>
</span>
<script>
document.getElementById("clinician_type").onchange = function () {
if (document.getElementById("clinician_type").options[document.getElementById("clinician_type").selectedIndex].value == "nurse")
document.getElementById("webname2").style.display = "block";
else
document.getElementById("webname2").style.display = "none";
}
</script>
后面跟着physicians
和nurses
的更多选项相同。
目前Speciality
显示所有选项,Occupation
仅在选择nurse
时出现(按照预期)。
我真的很感激任何建议,根据下拉选择制作适合的表格。我已尝试实施17525724的答案,但还没有让它发挥作用。