ruby – Rails 3-在弹出窗口中的ajax调用上部分渲染

我有一个带有remote => true的按钮,它按以下方式调用弹出窗口(
jquery弹出窗口,而不是真实窗口):

$modal = $('#modal')
$modal_close = $modal.find('.close')
$modal_container = $('#modal-container')
$task_select_div = $('.activity_task_add')

# Handle modal links with the data-remote attribute
$('a[data-remote]').on 'ajax:success',  (xhr, data, status) ->
  $modal
    .html(data)
    .prepend($modal_close)
    .css('top', $(window).scrollTop() + 150)
    .show()

#This is the callback that is not being executed.
$('form[data-remote]').on 'ajax:success',  (xhr, data, status) ->
  alert(data)
  $modal_container.hide()
  $modal.hide()
  $task_select_div.html(data)

在该弹出窗口中,我在此表单的提交按钮中有另一个带有remote_tag的表单,我调用并在底部具有以下代码的操作:

respond_to do |format|
    if @task.save
       format.html { redirect_to @task, notice: 'Task was successfully created.' }
       format.json { render json: @task, status: :created, location: @task }
       format.js {render :partial => 'tasks', :locals => {:tasks => current_user.department.tasks}}
    else
     format.html { render action: "new" }
     format.json { render json: @task.errors, status: :unprocessable_entity }
    end
end

它执行format.js,控制台显示“Rendered tasks / _tasks.html.erb(5.8ms)”,但ajax调用的回调不起作用.

$('form[data-remote]').on 'ajax:success',  (xhr, data, status) ->
  alert(data)

我需要收到一个ajax:success事件才能隐藏Popup.
有帮助吗?

最佳答案 删除此行:

format.js {render :partial => 'tasks', :locals => {:tasks => current_user.department.tasks}}

更新你的js回调:

$('form[data-remote]').on 'ajax:success',  (xhr, data, status) ->
  alert("hello world")
  $modal_container.hide()
  $modal.hide()
  $task_select_div.html(<%= escape_javascript(render :partial => 'tasks', :locals => {:tasks => current_user.department.tasks} ) %>)
点赞