weixin_33695082 2013-01-31 22:37 采纳率: 0%
浏览 24

Rails 3中的Ajax成功事件

Rails newbe here. I've got links that do ajax query to server:

<%= link_to g.name, g, :class => 'link link-to-gallery', :remote => true %>

JS view that loads data from partial:

$('#content').html('<%= escape_javascript render("layouts/galleries/show") %>');

And controller that operates with models and renders html and js:

class GalleriesController < ApplicationController
  def show
    @galleries = Gallery.all
    @gallery_to_show = Gallery.find(params[:id])
    respond_to do |format|
      format.html
      format.js
    end
  end
end

Ajax works fine, but I try to put callback after ajax completed:

jQuery(function() {
  $(".link-to-gallery").bind("ajax:complete", function() {
    alert(1)
  });
});

and alert never shows. What am I doing wrong? Rails version is 3.2.11, Ruby version is 1.9.3p194

Update: When I'm using this code

jQuery(function() {
  $(".link-to-gallery")
    .on("ajax:before", function () {
      alert("before")
    })
    .on("ajax:complete", function() {
      alert("complete")
    });
});

"before" alert fires but only once. If I press again I gain nothing. But ajax still works.

  • 写回答

2条回答 默认 最新

  • 零零乙 2013-01-31 22:46
    关注

    I think the problem is with your controller method that is not responding the async call with a success status message, 200.

    Try this

    def show
      @galleries = Gallery.all
      @gallery_to_show = Gallery.find(params[:id])
      respond_to do |format|
        format.html { :status => 200 }
        format.js { :status => 200 }
      end
    end
    

    If that doesn't work, try this.

    def show
      @galleries = Gallery.all
      @gallery_to_show = Gallery.find(params[:id])
      respond_to do |format|
        format.html { :status => :ok }
        format.js { :status => :ok }
      end
    end
    
    评论

报告相同问题?

悬赏问题

  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式