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

发布于 2024-12-28 20:56:39 字数 1456 浏览 3 评论 0原文

我有一个带有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 jobs/_tasks.html.erb (5.8ms)”,但 ajax 调用的回调不起作用。

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

我需要接收 ajax:success 事件才能隐藏弹出窗口。 有什么帮助吗?

I have a button with remote=>true that calls a popup (a jquery popup, not a real one) in the following way:

$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)

In that popup I have another form with remote_tag in the submit button of this form I call and action that has the following code at the bottom:

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

It executes format.js and the console says "Rendered tasks/_tasks.html.erb (5.8ms)" but the callback for the ajax call is not working.

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

I need to receive an ajax:success event in order to hide the Popup.
Any help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人事已非 2025-01-04 20:56:39

删除这一行:

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} ) %>)

Remove this line :

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

Update your js callback :

$('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} ) %>)
萌吟 2025-01-04 20:56:39

解决了。

改变了我的response_to do|格式|对于这个:

if request.xhr?
  task_list = render_to_string :partial => 'tasks', :locals => {:tasks => current_user.department.tasks}
  task_list =  task_list.html_safe.gsub(/\n/, '').gsub(/\t/, '').gsub(/\r/, '')
  render :json => {:html => task_list, :error => ''}
else
  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 }  
    else
      format.html { render action: "new" }
      format.json { render json: @task.errors, status: :unprocessable_entity }
    end
  end
end

我的 javascript 对于这个:

$('form[data-remote]').on 'ajax:success',  (xhr, data, status) ->
  $modal_container.hide()
  $modal.hide()
  $task_select_div.html(data.html)

你觉得这个解决方案怎么样?有什么缺点吗?

Solved it.

Changed my respond_to do |format| for this:

if request.xhr?
  task_list = render_to_string :partial => 'tasks', :locals => {:tasks => current_user.department.tasks}
  task_list =  task_list.html_safe.gsub(/\n/, '').gsub(/\t/, '').gsub(/\r/, '')
  render :json => {:html => task_list, :error => ''}
else
  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 }  
    else
      format.html { render action: "new" }
      format.json { render json: @task.errors, status: :unprocessable_entity }
    end
  end
end

And my javascript for this:

$('form[data-remote]').on 'ajax:success',  (xhr, data, status) ->
  $modal_container.hide()
  $modal.hide()
  $task_select_div.html(data.html)

What do you think of this solution? Any drawbacks?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文