【问题标题】:Rails Best_in_place ajax:success not being recognizedRails Best_in_place ajax:成功未被识别
【发布时间】:2015-05-18 22:17:34
【问题描述】:

我的 Rails 应用程序包含一个类似 Excel 的可编辑表格,始终使用 best_in_place 字段。它们在页面加载时工作得很好,在用户编辑 best_in_place 字段后,我在ajax:success 上调用咖啡脚本函数没有问题。

由于这是一个类似 Excel 的表格,因此用户可以单击链接以向表格中添加新的占位符行。我的 create.js.erb 文件在不重新加载页面的情况下添加了行。新行有几个具有 best_in_place 功能的表格单元格,更新它们会更新数据库,到目前为止一切顺利。

问题在于,当用户随后在 new 行之一中编辑 best_in_place 字段时,ajax:success 不会触发,因此我的咖啡脚本函数不会启动。

我已确认新表格单元格与现有表格单元格具有完全相同的类和数据属性(在类名上读取ajax:success)。

问题 - 在这种情况下,如果 ajax:success 未被识别,我该如何启动我的咖啡脚本功能?

里程碑控制器,创建和更新操作

def create
@payment_schedule = PaymentSchedule.find(params[:id])
build_default_milestone # this builds a placeholder milestone
if @milestone.save
  increment_milestone_positions # this relates to a sortable table
  respond_to do |format|
    format.js
    format.html { redirect_to :back }
  end
else
  respond_to do |format|
    format.js
    format.html { redirect_to :back }
  end
end
end

def update
@milestone = Milestone.find(params[:id])

respond_to do |format|
  if @milestone.update(milestone_params)
    update_milestone_amounts
    format.html { redirect_to(:back) }
    format.json { respond_with_bip(@milestone) }
  else
    format.html { redirect_to(:back) }
    format.json { respond_with_bip(:back) }
  end
end
end

创建.js.erb

$('#milestone-body').prepend('<%= escape_javascript(render partial: 'row_add', locals: { milestone: @milestone }) %>');

咖啡脚本功能

update-percent 是所讨论的 best_in_place 字段的类名。此函数对现有行完美触发,而不是新行。

$('.update-percent').on 'ajax:success', (event, data, status, xhr) ->
  ...

关于如何解决这个问题的任何建议?

【问题讨论】:

  • “类似 Excel 的可编辑表格” = 电子表格
  • 你可以检查 jQuerys ajaxComplete() fn 和字母检查它是否成功。 Docs

标签: ruby-on-rails ajax coffeescript best-in-place


【解决方案1】:

问题已解决。问题不在于 ajax:success 没有触发,而是(原因仍然未知)我必须在表格主体级别而不是有问题的表格单元格上阅读 ajax:success。显然这是一个比较常见的问题,当在 DOM 最初加载后向 DOM 添加新元素时,您需要阅读 ajax:success 更高的 DOM。

感谢提供意见的人。

【讨论】:

    【解决方案2】:

    您使用的是 turbo 链接吗?我认为最好的方式绑定到准备好的文档,如果您使用的是涡轮链接,这会显得很时髦。如果是这样,请将其从您的 application.html.erb 和 application.js 文件中删除,然后重试。

    【讨论】:

    • 从 Gemfile、application.html.erb 和 application.js 中删除了 turbolinks...不幸的是仍然无法识别 ajax:success。你是对的,它通过$(document).ready(function() { jQuery(".best_in_place").best_in_place(); }) 绑定到准备好的文档,这是在我的 application.js 文件中。
    猜你喜欢
    • 1970-01-01
    • 2013-01-19
    • 2019-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-23
    • 2014-01-12
    • 2012-02-28
    相关资源
    最近更新 更多