使用Rails,jquery和best_in_place gem进行就地编辑

我有一个任务列表应用程序,其中任务按类别显示。

我在两个地方列出了类别。 我允许使用best_in_place gem就地编辑类别名称。 我工作得很好。 我的问题是,因为我在两个地方都有类别名称而且我只是在原地编辑了一个类别,我需要在表单提交后使用新名称更新未编辑的名称的另一个外观。 刷新/重新加载受影响类别名称的最佳方法是什么?

谢谢。

您需要使用回调。

使用gem的描述( https://github.com/bernat/best_in_place )我得到了这个:

<%= best_in_place @user, :name, :data => {:user_name => @user.name}, :classes => 'custom_class' %> $('.custom_class').bind("ajax:success", function(){ alert('Name updated for '+$(this).data('userName')); }); 

所以在你的情况下,你需要做这样的事情:

 $("#other_ocurrence_id").html($(this).data('userName')); 

如果你有这个代码(haml)

 = best_in_place @user, :name, :classes => 'name-edit' 

你确实需要一个回调。 但是,在回调处理程序的上下文中,’this’表示调用的元素,因此您可以这样做

 $('.name-edit').on 'ajax:success', (event, data, status, xhr) -> $('.some-other-widget').html this.innerHTML 

并以这种方式获取其他小部件的新值。 回调处理程序中的事件还将currentTarget作为属性,即启动ajax请求的小部件。 但是,我不认为这是必要的。