Rails 3:简单的AJAXy页面更新? [英] Rails 3: Simple AJAXy Page updates?

查看:67
本文介绍了Rails 3:简单的AJAXy页面更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不敢相信我已经花了四个小时来完成这个简单的任务,但是我已经找到了。

I can't believe I've been looking four hours for this simple task, but I have.

在Rails 2.3中,我可以替换a使用以下简单代码的页面:

In Rails 2.3, I could replace one section of a page with this simple code:

render:update do | page |
page.replace_html div_id,:partial => new_content,...
结尾

render :update do |page| page.replace_html "div_id", :partial => "new_content",... end

在Rails 3中,Ryan Bates有我编写全新的javascript函数,从Prototype(默认为rails)切换为jQuery,否则将无法享受生活。其他tutes不再简单明了。

In Rails 3, Ryan Bates has me writing entire new javascript functions, switching from Prototype (rails default) to jQuery, and otherwise not enjoying life. The other tutes are no more straightforward.

我缺少什么?这些天我们如何替换< div>

What am I missing? How do we replace a <div> these days?

推荐答案

谢谢你们。官方的答案似乎是,是的,团队认为简单是好事的敌人,并使事情变得更加复杂。

Thanks, guys. The official answer seems to be that, yes, the team felt simple is the enemy of good and made it more complicated.

第一个关键是创建一个.js。调用ajax更新的方法的erb文件NAMED。因此,如果index方法处理更新,请将原始javascript放在 index.js.erb 中。

The first key is to create a .js.erb file NAMED for the method CALLING the ajax update. So if the index method handles the update, put the raw javascript in index.js.erb. This goes in the views folder.

第二,在index.js.erb中工作的代码是

Second, the code that worked in index.js.erb was

m = $('list_users');    
m.innerHTML = "<%= escape_javascript(render :partial => "reload_users") %>";

然后进行调用,在控制器方法的response_to块中添加:

Then to make the call, add in the respond_to block of the controller method, add:

format.js

最后,调用视图具有:

<%= link_to "Update User List", @reload_users_path, :remote => true %>

顺便说一句,据说所有旧页面都使用 page.replace 将在您安装插件时起作用。插件下载页面表明它在Rails 3的最新版本中损坏,并且尚未修复。此外,如果您使用它,各种各样的博客作者也会来您家,并为您切换桦木。

By the way, supposedly all the old pages using page.replace will work if you install a plugin. The plugin download page suggests that it broke in the last releases of Rails 3 and has not been fixed. Also, various bloggers will come to your home and birch-switch you if you use it.

这篇关于Rails 3:简单的AJAXy页面更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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