Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rails—为什么jQuery没有';远程提交表单后无法加载_Jquery_Ruby On Rails 3_Coffeescript_Ujs - Fatal编程技术网

Rails—为什么jQuery没有';远程提交表单后无法加载

Rails—为什么jQuery没有';远程提交表单后无法加载,jquery,ruby-on-rails-3,coffeescript,ujs,Jquery,Ruby On Rails 3,Coffeescript,Ujs,为了简单起见,我有一个表单可以更新部分。没什么特别的。在partial中,我有一个jQuery可排序列表。在更新表单之前,我可以毫无问题地拖动列表中的内容 然而,在我更新列表之后,就好像js没有被重新加载,我必须刷新整个页面才能重新启动 我已经尝试了所有的方法,甚至在我在部分中加入了一些js shizzle之后,借用了同样不起作用的 我相信这很简单,但我是一个ajax/jQuery新手,我真的很挣扎 视图: 部分“测试一” update.js.erb $("#test_one").html('&

为了简单起见,我有一个表单可以更新部分。没什么特别的。在partial中,我有一个jQuery可排序列表。在更新表单之前,我可以毫无问题地拖动列表中的内容

然而,在我更新列表之后,就好像js没有被重新加载,我必须刷新整个页面才能重新启动

我已经尝试了所有的方法,甚至在我在部分中加入了一些js shizzle之后,借用了同样不起作用的

我相信这很简单,但我是一个ajax/jQuery新手,我真的很挣扎

视图:

部分“测试一”

update.js.erb

$("#test_one").html('<%= escape_javascript render("test_two") %>');
最后是locations.js.coffee

jQuery ->
  $('#types').sortable(
      axis: 'y'
      update: ->
        $.post($(this).data('update-url'), $(this).sortable('serialize'))
      )

更新工作正常,我没有收到任何错误,我的控制台中也没有任何内容。我现在对此非常迷茫-更新记录后如何让js重新加载?

原因是,您的自定义js(关于可排序)已加载到文档就绪状态,并且不会在部分更新后再次触发

解决方案是:将自定义js包装到函数中。在文档准备就绪并进行更新时调用此函数

# locations.js.coffee
$ ->
  $(document).custom_js()

$.fn.custom_js ->
  $('#types').sortable(
    axis: 'y'
    update: ->
      $.post($(this).data('update-url'), $(this).sortable('serialize'))
    )

# update.js.erb
$("#test_one").html('<%= escape_javascript render("test_two") %>');
$(document).custom_js();
#locations.js.coffee
$ ->
$(文档).custom_js()
$.fn.custom_js->
$(“#类型”)。可排序(
轴:“y”
更新:->
$.post($(this.data('update-url'),$(this.sortable('serialize'))
)
#update.js.erb
$(“#测试一”).html(“”);
$(文档).custom_js();

关于自定义js功能的旁注:无需在此处包装所有自定义代码,只需与Ajax将要添加/更新的内容相关的部分。

您刚刚将我和周围其他人的情绪提高了十倍:)非常感谢。很简单,不是吗@simonmorley,我很高兴能帮上一点忙:)@BillyChan谢谢你,我只是在想-当我有一个循环,其中部分被渲染了一百次,这意味着
update.js.erb
中的代码也会被调用一百次-这可能会减慢应用程序的速度。只是想知道-有没有更有效的解决方案?谢谢。@user984621,我认为在您的情况下,上面的代码不能直接使用,需要一些修改。有几点:一,。我不认为渲染100多次会让人感觉很难看,特别是当你有一个旋转器出现时。2.如果真的需要,整个html循环可以在服务器端组成。
  def update
    @location = Location.find(params[:id])
    @types = @location.types.order('location_types.position').limit(2)
    respond_to do |format|
      if @location.update_attributes!(params[:location])
        flash[:notice] = 'Settings updated successfully'
        format.html { redirect_to edit_location_path }
        format.js
      else
        format.html { render action: "edit_access" }
        format.js { render action: "update_access_errors" }
      end
    end
  end
jQuery ->
  $('#types').sortable(
      axis: 'y'
      update: ->
        $.post($(this).data('update-url'), $(this).sortable('serialize'))
      )
# locations.js.coffee
$ ->
  $(document).custom_js()

$.fn.custom_js ->
  $('#types').sortable(
    axis: 'y'
    update: ->
      $.post($(this).data('update-url'), $(this).sortable('serialize'))
    )

# update.js.erb
$("#test_one").html('<%= escape_javascript render("test_two") %>');
$(document).custom_js();