试图遵循投票的答案,但它对我不起作用。所以我计划了一个可行的解决方案。这是我所做的。
1.) 我首先在我的资产 javascript 中添加了 //= require jquery-ui
2.) 我创建了一个表单来编辑我的笔记。这里使用了 ID 名称。如果您注意到我在表单标签上的 URL 是一个空字符串。你会在我的 javascript 上看到。
<%# this is the form that is used to update the notes %>
<div id="dialog" title="Update Notes">
<%= form_tag('', method: "put", class: 'comment_form', id: 'workorder_form') do %>
<div>
<%= text_area_tag(:message, '', class: "form-control workorder_notes_text_area") %>
</div>
<div class="mt-5 text-right">
<%= submit_tag("Submit", type:"submit", class:"btn btn-primary", id: 'workorder_notes_button') %>
</div>
<% end %>
</div>
3) 对于列出的每个注释,我都会获取注释 ID 并将其附加到注释消息中
<%# id attribute is also used to get the %>
<td class="text-center" id="note_message_id_<%=note.id%>">
<%= note.message %>
</td>
4) 对于编辑按钮,我使用 data 属性添加了一些数据,例如笔记的 ID。此外,classname edit-workorder 被用来监听每个按钮的点击事件
<button data-notesid="<%= note.id%>" class="btn btn-success edit-workorder">
<span class="icon icon-pencil"></span>
</button>
JAVASCRIPT 部分
5) 监听每个编辑按钮的点击事件
$('.edit-workorder').click(function(e) {}
the next javascript are inside the click event function
6) 这会抓取按钮数据属性中的数据。请参阅第 4 条。
const notesId = $(this).data('notesid');
7) 这将抓取当前消息并对其进行修剪。
// this will return the current notes and removes spaces
const notesMsg = $(`td#note_message_id_${notesId}`)
.text()
.trim();
8) 抓取对话框。
// dialog modal
// this comes from _workordernotes.html.erb
const dialogUI = $('#dialog');
9) 这将附加到 form_tag,因此 URL 将是动态的。由于笔记的 ID,我需要这个。
$('#workorder_form').attr('action',`${workorderId}/workordernotes/${notesId}`);
10) 这是对话部分。 close 方法很重要,因为当您第一次打开模式并关闭它时。操作 URL 不会更改,因为状态被保留。您需要销毁元素或将它们置于初始状态(空字符串状态)。
// dialog API
// https://api.jqueryui.com/dialog/
dialogUI.dialog({
modal: true,
draggable: false,
height: 400,
width: 600,
resizable: false,
// show: { effect: 'slideDown', duration: 1000 },
// on close dailog should return from its normal state or else
// action attribute wont change
close: function(e, ui) {
dialogUI.dialog('destroy');
}
});
11) 最后,这会将当前消息附加到表单的文本区域。
$('textarea#message').val(notesMsg);
workordernotes_controller.rb RoR
12) 这是我的更新方法。这还没有完成。我只需要在这里创建一个 try-catch 块然后就完成了。但在它的所有工作中。
def update
Spree::Workordernote.find(params[:id]).update(message: params[:message])
redirect_to admin_workorder_path(params[:workorder_id])
end
完整的 JAVASCRIPT 代码
$(document).ready(function() {
$('#dialog').hide();
const workorderId = $('tbody#table_body').data('workorderid');
$('.edit-workorder').click(function(e) {
e.preventDefault();
// returns the workordernotes ID
// this comes from _workordernotes.html.erb button
const notesId = $(this).data('notesid');
// this will return the current notes and removes spaces
const notesMsg = $(`td#note_message_id_${notesId}`)
.text()
.trim();
// dialog modal
// this comes from _workordernotes.html.erb
const dialogUI = $('#dialog');
// this should be called before dialog or else action attr wont attached
$('#workorder_form').attr(
'action',
`${workorderId}/workordernotes/${notesId}`
);
// dialog API
// https://api.jqueryui.com/dialog/
dialogUI.dialog({
modal: true,
draggable: false,
height: 400,
width: 600,
resizable: false,
// show: { effect: 'slideDown', duration: 1000 },
// on close dailog should return from its normal state or else
// action attribute wont change
close: function(e, ui) {
dialogUI.dialog('destroy');
}
});
// attached the old message on text area
$('textarea#message').val(notesMsg);
});
});