我有一个简单的应用程序,它使用带有模式的Ajax在常规表视图中添加和更新记录。
我添加了新记录,只需将新的表行/记录追加到表的底部,但在更新现有记录后,找出如何显示更改是有困难的。以下是一些代码:
update.js.erb
console.log('Updated');
$('#dialog-form').dialog('close');
$('#dialog-form').remove();
$('table').update record somehow?通常我会做这样的事
$(this).closest('tr').remove();
$(this).closest('tr').append('whatever');但是这是行不通的,因为update按钮是在一个模态中,而不是在实际的表中。
作为参考,下面是我如何添加一个新记录:
create.js.erb
console.log('Created');
$('#dialog-form').dialog('close');
$('#dialog-form').remove();
$('table').append('<%= escape_javascript(render(@exercise)) %>');
$('tr').last('tr').effect('highlight');发布于 2013-09-11 14:49:52
你可以这样做:
# index.html.erb
<table>
<tbody>
<% @exercises.each do |exercise| %>
<tr id="exercise_<%= exercise.id %>">
<td><%= exercise.name %></td>
# etc...
</tr>
<% end %>
</tbody>
</table>
# update action in controller:
def update
@exercise = Exercise.where(id: params[:id]).first
@exercise.update_attributes(params[:whatever])
# etc ...
# update.js.erb
console.log('Updated');
$('#dialog-form').dialog('close');
$('#dialog-form').remove();
$('table tr#exercise_<%= @exercise.id %>').replaceWith('<%= escape_javascript(render(@exercise)) %>');希望这能有所帮助!你可以随便问关于这个的任何问题
https://stackoverflow.com/questions/18744197
复制相似问题