Jquery Rails升级到3.1-将ajax处理从“render:update”更改为响应

Jquery Rails升级到3.1-将ajax处理从“render:update”更改为响应,jquery,ruby-on-rails,ajax,ruby-on-rails-3.1,Jquery,Ruby On Rails,Ajax,Ruby On Rails 3.1,我正在将一个旧的rails应用程序升级到3.1。该应用程序运行良好,但我需要更新一些ajax功能。如果有什么不同,我会使用jquery和coffeescript 所有现有的ajax功能都是使用render:updates编写的。乙二醇 render :update do |page| page.remove "financial_tran_offset_#{params[:ftof_id]}" page.replace_html 'details', :partial => 'de

我正在将一个旧的rails应用程序升级到3.1。该应用程序运行良好,但我需要更新一些ajax功能。如果有什么不同,我会使用jquery和coffeescript

所有现有的ajax功能都是使用render:updates编写的。乙二醇

render :update do |page|
  page.remove "financial_tran_offset_#{params[:ftof_id]}"
  page.replace_html 'details', :partial => 'details', :locals => {:obj => @fire}
end
我认为这样做的新的首选方式是使用response_?块来处理js,但我不确定处理这些不同情况的最佳方法

对于第一种情况page.remove,我认为对于资产管道,我应该在/app/assets/javascripts/中有一个外部js来处理javascript端,例如page.remove,但我不确定如何将参数传递给js文件。我猜你可以这样做:

respond_to do |format|  
  format.js {:render :js => {:ftof => params[:ftof_id]}
end
但我不确定你怎么能从js文件中找到这个。这是向js传递信息的正确方式吗?还是我应该使用另一种方法

对于第二种情况,page.replace_html我认为它已被弃用/从3.1中删除。我再次怀疑这应该使用app/assets/javascript目录中的js文件,但不确定我应该如何渲染部分内容并将此信息传递到js中


感谢在此区域中的所有指针=

将jQuery与js.erb视图结合使用,并对块进行响应

不管这个动作是什么,为了举例,我们都会说FoosController更新:

render :update do |page|
  page.remove "financial_tran_offset_#{params[:ftof_id]}"
  page.replace_html 'details', :partial => 'details', :locals => {:obj => @fire}
end
将成为:

respond_to do |format|
  format.html
  format.js     # <- this is what we're after
end
erb将解析update.js.erb,呈现为js,并通过Ajax和eval'd发送回客户端

您可以将任何需要的内容传递给这些JS模板。毕竟,它们是雇员再培训局的文件。与HTML/ERb视图一样使用和实例变量。如果在JS/ERb视图中调用render,请使用escape_javascript对其进行包装,以获得正确渲染的HTML

render:update为原型调用旧的JavaScriptGenerator辅助方法。转换为jQuery非常简单,因为它们都做相同的事情:选择一个DOM元素并对其进行操作

这是一个很好的小平移表,具有典型的操作。从控制器中删除Prototype helper方法,并将其jQuery或JS对应项放在相应的JS/ERb视图中:

       Prototype                            jQuery/Javascript
     in controller                    in JS/ERb view (xxxxxx.js.erb)
       ---------                            -----------------
page.alert "message"                 alert('message');
page.hide "id"                       $('#id').hide();
page.insert_html \
         :top,    "id", "content"    $('#id').prepend('content');
         :bottom, "id", "content"    $('#id').append('content');
         :before, "id", "content"    $('#id').before('content');
         :after,  "id", "content"    $('#id').after('content');
page.redirect_to "url"               window.location.href = 'url';
page.reload                          window.location.reload();
page.remove "id"                     $('#id').remove();
page.replace "id", "content"         $('#id').replaceWith('content');
page.replace_html "id", "content"    $('#id').html('content');
page.show "id"                       $('#id').show();
page.toggle "id"                     $('#id').toggle();

不要忘记每一行上的分号

酷。用coffeescript可以做到这一点吗?或者你在update.js.erb中只限于使用纯js吗?如果你真的想使用coffeescript,你也许可以,但我可以告诉你,动态视图中的支持是参差不齐的。我个人更喜欢直接的JS。用于简单jQuery一行程序的CoffeeScript有点过头了。$'添加“内容”;添加到$'id'。附加“content”。保存一组括号的开销是不值得的。JQuery非常紧凑和简单。还是坚持下去吧。谢谢你的详细回答!有助于消除原型使用
       Prototype                            jQuery/Javascript
     in controller                    in JS/ERb view (xxxxxx.js.erb)
       ---------                            -----------------
page.alert "message"                 alert('message');
page.hide "id"                       $('#id').hide();
page.insert_html \
         :top,    "id", "content"    $('#id').prepend('content');
         :bottom, "id", "content"    $('#id').append('content');
         :before, "id", "content"    $('#id').before('content');
         :after,  "id", "content"    $('#id').after('content');
page.redirect_to "url"               window.location.href = 'url';
page.reload                          window.location.reload();
page.remove "id"                     $('#id').remove();
page.replace "id", "content"         $('#id').replaceWith('content');
page.replace_html "id", "content"    $('#id').html('content');
page.show "id"                       $('#id').show();
page.toggle "id"                     $('#id').toggle();