狀態是一個下拉串列,如果它是一個功能,則具有值(新的、開始的、完成的);如果它是一個錯誤,則為(新的、開始的和已解決的)。我不知道我該怎么做
模型
Class Task
enum typeOf: %i[feature bug]
enum status_is: %i[New started completed resolved]
end
erb.html
<%= form_for :task, url: project_tasks_path, :html => {:multipart => true, :cla
<%= f.hidden_field :project %>
Title: <%=f.text_field :title, class:"form-control" %><br>
Description: <%=f.text_field :description, class:"form-control" %><br>
<label for="deadline">Deadline</label><input type="datetime-local" id="deadline"
name="deadline" ><br>
Screen Shot: <%=f.file_field :screen_shot, multiple: true, class:"form-control" %>
<div class="field">
<%= f.label :typeOf, class:"form-control" %><br />
<%= f.select(:typeOf, Task.typeOfs.keys.map {|role| [role.titleize,role]}) %>
</div>
<div class="field">
<%= f.label :status_is, class:"form-control" %><br />
<%= f.select(:status_is, Task.status_is.keys.map {|role| [role.titleize,role]}) %>
</div>
<%= f.submit "Add" %>
<%end %>
uj5u.com熱心網友回復:
我會使用兩個單獨的選擇框,每個選擇框都有預期的情況:
<div class="field" data-target="bug">
<%= f.label :status_is, class:"form-control" %><br />
<%= f.select(:status_is, (Task.status_is.keys - ['completed']).map {|role| [role.titleize,role]}) %>
</div>
<div class="field" data-target="feature">
<%= f.label :status_is, class:"form-control" %><br />
<%= f.select(:status_is, (Task.status_is.keys - ['resolved']).map {|role| [role.titleize,role]}) %>
</div>
然后你使用一些 javascript 來切換它們的輸入/輸出:
function toggleSelects() {
var value = $('#task_typeOf')[0].value;
$('[data-target=bug]').toggle(value === 'bug');
$('[data-target=bug] select').attr('disable', value !== 'bug');
$('[data-target=feature]').toggle(value === 'feature');
$('[data-target=feature] select').attr('disable', value !== 'feature');
}
// execute every time the type of task is changed
$('#task_typeOf').on('change', () => toggleSelects());
// also execute at document load
$(toggleSelects);
當然,您可以使用不同的策略或包裝器來隱藏欄位并禁用選擇,但您必須兩者都做(無論如何都會提交隱藏的選擇)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/416203.html
標籤:
