我有模型Approval
,Approver
。他們之間的聯繫是Approval
有很多Approvers
。這裏我使用嵌套的形式。創建一個Approval Process
以及他們的Approvers
。在這裏,當我添加新的批准者編輯窗體其工作良好。當我改變現有的價值觀,它在編輯形式正確改變。但是當我通過單擊刪除鏈接刪除這些嵌套的字段字段正在消失。到這裏好。當我點擊更新時刪除字段後,字段不會被刪除。嵌套表單字段不刪除當我點擊刪除鏈接,但更新字段和添加新字段正在工作(在軌道上的紅寶石)
<%= nested_form_for(@approval , html: {:class => 'form-inline'}) do |f| %>
<div class="col-md-7">
<div class="field">
<%= f.label :name, "Assign a name to this approval process." %></br>
<%= f.text_field :name,:class => 'form-control' %></br></br>
</div>
<div class="field">
<%= f.label :for, "This approval process will be be applicable to which claim form?" %>
<%=f.select :for, ["Regularize Attendance", "Leave Request", "Daily Expense Claim ", "Travel Expense Claim"], {:prompt=>"Select One"}, {:class => 'form-control' } %></br></br>
</div>
<div class="field">
<%= f.label :sequential, "Does this process involve sequential or non-sequential approvals?" %>
<%=f.select :sequential, ["Sequential", "Non-Sequential"], {:prompt=>"Select One"}, {:class => 'form-control' } %>
<br><br>
</div>
<%= f.fields_for :approvers do |approver| %>
<div class="field">
<%= approver.label :approver_type,"Approver" %>
<%= approver.select :approver_type, ["Role", "Specific User"], {}, {:class => 'form-control role_select', :onchange => "$.fn.myFunc(this);" } %></br></br>
</div>
<div class="field">
<%= approver.label :approver_value,"Role" %>
<%= approver.select :approver_value, [ "HR Manager", "Reporting Manager"], {}, {:class => 'form-control appr_select' } %>
<%= approver.text_field :approver_value, style: 'display:none',:class => 'form-control inpt_specific_user',:disabled => "true" %>
<%= approver.link_to_remove "Remove", :class=>"" %> </br></br>
</div>
<% end %>
<%= f.link_to_add "Add Approvers", :approvers,:class=>"" %></br>
</div>
<div class="col-md-7">
</br>
<%= f.submit "Save", :class=>"btn btn-primary" %>
</div>
<br>
<% end %>
這裏默認approver_type
字段將顯示爲角色和相應的IAM顯示approver_value
字段作爲下拉(通過隱藏approver_value
文本框)。當用戶改變approver_type
到特定的用戶,然後IAM顯示approver_value
字段作爲文本框輸入具體的人的名字(通過隱藏approver_value
場下拉列表)。對於這一點,這是我寫的JavaScript:
$.fn.myFunc = function (button) {
alert("in actual");
var $nextDiv = $(button).closest('div.field') // get closest parent field
.next('div.field') // get next parent field
$nextDiv.find('label') // find label
.text(button.value);
if(button.value == 'Role'){
$nextDiv.find('select').show();
$nextDiv.find('select').removeAttr("disabled")
$nextDiv.find('input').hide();
$nextDiv.find('input').attr("disabled", "disabled");
}
if (button.value =="Specific User"){
$nextDiv.find('input').show();
$nextDiv.find('input').removeAttr("disabled")
$nextDiv.find('select').hide();
$nextDiv.find('select').attr("disabled", "disabled");
}
}
$(document).ready(function() {
$("select.role_select").each(function(){
var $this = $(this);
var $nextDiv = $this.closest('div.field') // get closest parent field
.next('div.field') // get next parent field
alert("YYYYYYYYYYYYYYYYYYYYYYYY");
alert($this);
alert($this.val());
if($this.val() == 'Role'){
$nextDiv.find('select').show();
$nextDiv.find('select').removeAttr("disabled")
$nextDiv.find('input').hide();
$nextDiv.find('input').attr("disabled", "disabled");
}
if ($this.val() =="Specific User"){
$nextDiv.find('input').show();
$nextDiv.find('input').removeAttr("disabled")
$nextDiv.find('select').hide();
$nextDiv.find('select').attr("disabled", "disabled");
}
});
});
和當我刪除Approver_type
下拉值爲"specific user"
的字段時,刪除工作正常。
這是在終端的輸出:
Started PATCH "/approvals/fYtVHpl_3zqTskQtnmedWw%253D%253D" for 127.0.0.1 at 2015-09-02 12:24:09 +0530
Processing by ApprovalsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"/lQfQRUhE8jmhSitAMkDx+bWMKLZilP29PQXGI82YaY=", "approval"=>{"name"=>"attendance", "for"=>"Regularize Attendance", "sequential"=>"Non-Sequential", "approvers_attributes"=>{"0"=>{"approver_type"=>"Role", "approver_value"=>"Reporting Manager", "id"=>"13"}, "1"=>{"approver_type"=>"Specific User", "approver_value"=>"john", "_destroy"=>"1", "id"=>"14"}}}, "commit"=>"Save", "id"=>"fYtVHpl_3zqTskQtnmedWw%3D%3D"}
但是當我刪除其具有Approver_type
下拉值作爲"role"
然後它不刪除的字段。
Started PATCH "/approvals/fYtVHpl_3zqTskQtnmedWw%253D%253D" for 127.0.0.1 at 2015-09-02 12:32:15 +0530
Processing by ApprovalsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"/lQfQRUhE8jmhSitAMkDx+bWMKLZilP29PQXGI82YaY=", "approval"=>{"name"=>"attendance", "for"=>"Regularize Attendance", "sequential"=>"Non-Sequential", "approvers_attributes"=>{"0"=>{"approver_type"=>"Role", "approver_value"=>"Reporting Manager", "id"=>"13"}}}, "commit"=>"Save", "id"=>"fYtVHpl_3zqTskQtnmedWw%3D%3D"}
當我刪除具有值作爲角色的審批者下拉字段時,參數中沒有發送destroy參數。
但在控制器中,我還將參數列入白名單。
def approval_params
# params.require(:approval).permit(:company_id, :name, :for, :sequential)
params.require(:approval).permit!
end
和嵌套形式生成編輯表單下面的標記:
<form accept-charset="UTF-8" action="/approvals/fYtVHpl_3zqTskQtnmedWw%253D%253D" class="form-inline" id="edit_approval_4" method="post"><div style="display:none"><input name="utf8" type="hidden" value="✓" /><input name="_method" type="hidden" value="patch" /><input name="authenticity_token" type="hidden" value="/lQfQRUhE8jmhSitAMkDx+bWMKLZilP29PQXGI82YaY=" /></div>
<div class="col-md-7">
<div class="field">
<label for="approval_name">Assign a name to this approval process.</label></br>
<input class="form-control" id="approval_name" name="approval[name]" type="text" value="attendance" /></br></br>
</div>
<div class="field">
<label for="approval_for">This approval process will be be applicable to which claim form?</label>
<select class="form-control" id="approval_for" name="approval[for]"><option selected="selected" value="Regularize Attendance">Regularize Attendance</option>
<option value="Leave Request">Leave Request</option>
<option value="Daily Expense Claim ">Daily Expense Claim </option>
<option value="Travel Expense Claim">Travel Expense Claim</option></select></br></br>
</div>
<div class="field">
<label for="approval_sequential">Does this process involve sequential or non-sequential approvals?</label>
<select class="form-control" id="approval_sequential" name="approval[sequential]"><option value="Sequential">Sequential</option>
<option selected="selected" value="Non-Sequential">Non-Sequential</option></select>
<br><br>
</div>
<div class="fields">
<div class="field">
<label for="approval_approvers_attributes_0_approver_type">Approver</label>
<select class="form-control role_select" id="approval_approvers_attributes_0_approver_type" name="approval[approvers_attributes][0][approver_type]" onchange="$.fn.myFunc(this);"><option selected="selected" value="Role">Role</option>
<option value="Specific User">Specific User</option></select></br></br>
</div>
<div class="field">
<label for="approval_approvers_attributes_0_approver_value">Role</label>
<select class="form-control appr_select" id="approval_approvers_attributes_0_approver_value" name="approval[approvers_attributes][0][approver_value]"><option value="HR Manager">HR Manager</option>
<option selected="selected" value="Reporting Manager">Reporting Manager</option></select>
<input class="form-control inpt_specific_user" disabled="disabled" id="approval_approvers_attributes_0_approver_value" name="approval[approvers_attributes][0][approver_value]" style="display:none" type="text" value="Reporting Manager" />
<input id="approval_approvers_attributes_0__destroy" name="approval[approvers_attributes][0][_destroy]" type="hidden" value="false" /><a class=" remove_nested_fields" data-association="approvers" href="javascript:void(0)">Remove</a> </br></br>
</div>
<input id="approval_approvers_attributes_0_id" name="approval[approvers_attributes][0][id]" type="hidden" value="13" /></div><div class="fields">
<div class="field">
<label for="approval_approvers_attributes_1_approver_type">Approver</label>
<select class="form-control role_select" id="approval_approvers_attributes_1_approver_type" name="approval[approvers_attributes][1][approver_type]" onchange="$.fn.myFunc(this);"><option value="Role">Role</option>
<option selected="selected" value="Specific User">Specific User</option></select></br></br>
</div>
<div class="field">
<label for="approval_approvers_attributes_1_approver_value">Role</label>
<select class="form-control appr_select" id="approval_approvers_attributes_1_approver_value" name="approval[approvers_attributes][1][approver_value]"><option value="HR Manager">HR Manager</option>
<option value="Reporting Manager">Reporting Manager</option></select>
<input class="form-control inpt_specific_user" disabled="disabled" id="approval_approvers_attributes_1_approver_value" name="approval[approvers_attributes][1][approver_value]" style="display:none" type="text" value="john" />
<input id="approval_approvers_attributes_1__destroy" name="approval[approvers_attributes][1][_destroy]" type="hidden" value="false" /><a class=" remove_nested_fields" data-association="approvers" href="javascript:void(0)">Remove</a> </br></br>
</div>
<input id="approval_approvers_attributes_1_id" name="approval[approvers_attributes][1][id]" type="hidden" value="14" /></div> <a class=" add_nested_fields" data-association="approvers" data-blueprint-id="approvers_fields_blueprint" href="javascript:void(0)">Add Approvers</a></br>
</div>
<div class="col-md-7">
</br>
<input class="btn btn-primary" name="commit" type="submit" value="Save" />
</div>
<br>
<div data-blueprint="<div class="fields">
<div class="field">
<label for="approval_approvers_attributes_new_approvers_approver_type">Approver</label>
<select class="form-control role_select" id="approval_approvers_attributes_new_approvers_approver_type" name="approval[approvers_attributes][new_approvers][approver_type]" onchange="$.fn.myFunc(this);"><option value="Role">Role</option>
<option value="Specific User">Specific User</option></select></br></br>
</div>
<div class="field">
<label for="approval_approvers_attributes_new_approvers_approver_value">Role</label>
<select class="form-control appr_select" id="approval_approvers_attributes_new_approvers_approver_value" name="approval[approvers_attributes][new_approvers][approver_value]"><option value="HR Manager"quot;>HR Manager</option>
<option value="Reporting Manager">Reporting Manager</option></select>
<input class="form-control inpt_specific_user" disabled="disabled" id="approval_approvers_attributes_new_approvers_approver_value" name="approval[approvers_attributes][new_approvers][approver_value]" style="display:none" type="text" />
<input id="approval_approvers_attributes_new_approvers__destroy" name="approval[approvers_attributes][new_approvers][_destroy]" type="hidden" value="false" /><a class=" remove_nested_fields" data-association="approvers" href="javascript:void(0)">Remove</a> </br></br>
</div>
</div>" id="approvers_fields_blueprint" style="display: none"></div></form>
我也試過這個。這也不起作用。將下拉菜單中的特定字段轉換爲文本字段,反之亦然? – John
我認爲link_to_remove方法有問題。爲什麼你要將下拉菜單轉換爲文本字段? –
當approver_type下拉值爲「特定用戶」時,link_to刪除正在工作。和相同的link_to刪除方法爲什麼它是無法刪除Approver_vaue下拉列表值時的角色? – John