如何升级:升级=>“div_id'从轨道2 remote_form_for的选项,Rails 3的? [英] How to upgrade the :update=>'div_id' option of remote_form_for from Rails 2 to Rails 3?

查看:145
本文介绍了如何升级:升级=>“div_id'从轨道2 remote_form_for的选项,Rails 3的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法弄清楚如何升级从Rails的2本code到Rails 3的:

I can't figure out how to upgrade this code from Rails 2 to Rails 3:

<% remote_form_for(item, :update => 'div_id') do |f| %>
... 

我尝试这样做:

I tried this:

<%= form_for :item, :remote => true, :url => { :controller => "items", :action => "create" }, :update => 'div_id' do |f| %>
...

它创建新的项目,但失败的更新内容&LT; D​​IV ID =div_id&GT;&LT; / DIV&GT; 标签。看来Rails 3中不再支持:更新的form_for 选项远程。任何建议?

It creates the new item but it fails in updating the content within <div id="div_id"></div> tags. It seems Rails 3 no longer supports the ":update" option for a remote form_for. Any suggestion?

推荐答案

您可以使用RJS,但是这被德precated太(有很好的理由)。简化的,最佳实践的方法来处理这​​个Rails中3+如下(假设jQuery的):

You could use RJS, but that's being deprecated too (and for good reason). The simplified, best-practices way to handle this in Rails 3+ is as follows (assuming jQuery):

# your_view.html.erb
<div id="receiver-id"></div>

<%= form_for :some_model, :remote => true, :id => 'form-id' do |f| %>
  ...
<% end %>


# application.js (or any other .js loaded on the page)
$(function(){
  $('#form-id').bind('ajax:success', function(xhr, data, status){
    $('#receiver-id').html(data);
  });
});

AJAX:成功挂钩被称为jQuery的-UJS(又名jQuery的护栏,又名Rails的UJS)远程链接/表单处理程序。 看到自己。有很多其他的回调/可钩子供您使用了。如果你想使这个更加灵活,你可以使用生活,而不是绑定,并绑定到一个类,决定了那里的输出中去(如边栏),然后所有的远程链接/表格与补充工具栏类将有自己的HTML响应去 DIV#侧边栏

The ajax:success hook gets called by the jquery-ujs (aka jquery-rails, aka rails-ujs) remote link/form handler. See for yourself. There are lots of other callbacks/hooks available for you to use, too. If you wanted to make this even more flexible, you could use live instead of bind, and bind to a class that dictates where the ouput goes (e.g. "sidebar") and then all remote links/forms with the sidebar class would have their HTML response go to div#sidebar.

这篇关于如何升级:升级=&GT;“div_id'从轨道2 remote_form_for的选项,Rails 3的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆