Rails 3:简单的 AJAXy 页面更新?
我不敢相信我为了这个简单的任务花了四个小时,但我做到了。
在 Rails 2.3 中,我可以使用以下简单代码替换页面的一部分:
render :update do |page| page.replace_html "div_id", :partial => “新内容”,... 在
Rails 3 中,Ryan Bates 让我编写全新的 javascript 函数,从 Prototype(rails 默认)切换到 jQuery,否则就不享受生活了。其他课程也同样简单。
我缺少什么?现在我们如何替换
?I can't believe I've been looking four hours for this simple task, but I have.
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",...
end
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.
What am I missing? How do we replace a <div>
these days?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
谢谢,伙计们。官方的答案似乎是,是的,团队认为简单是优秀的敌人,因此将其变得更加复杂。
第一个关键是创建一个 .js.erb 文件,该文件名为调用 ajax 更新的方法。因此,如果索引方法处理更新,请将原始 JavaScript 放入
index.js.erb
中。这位于视图文件夹中。其次,在index.js.erb中工作的代码是
然后进行调用,在控制器方法的respond_to块中添加:
最后,调用视图有:
顺便说一句,据说所有旧页面都使用
page.replace 将起作用。插件下载页面表明它在 Rails 3 的最新版本中出现问题并且尚未修复。此外,如果您使用它,各种博主会来到您家并更换您。
Thanks, guys. The official answer seems to be that, yes, the team felt simple is the enemy of good and made it more complicated.
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.Second, the code that worked in index.js.erb was
Then to make the call, add in the respond_to block of the controller method, add:
Finally, the calling view has:
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.整个 RJS 内容使 javascript 内联,并使 dom 非常引人注目。此外,通过避免内联 javascript,您可以通过在浏览器中压缩和缓存这些文件来开辟其他可能的优化 javascript 的方法。这就是为什么 RJS 超出了 Rails 3 范围的原因。花一天时间使用 jQuery 或 Prototype 应该能让你熟悉这些小东西,并且从长远来看将有助于项目。
The whole RJS stuff makes the javascript inline and makes the dom very obtrusive. Also, by avoiding inline javascript you could open up other possible ways of optimizing you javascript by compressing and caching those files in browser. Thats the reason why RJS is getting out of scope from rails 3. A little bit of getting around with jQuery or Prototype for a day should get you on gears with these kind of small stuff and will help the project on long run.
你那里还有 jQuery 吗?我会在任何一天推荐它而不是 Prototype...
如果它仍然存在,您可以在 Javascript 中使用以下内容:
这通过 AJAX 获取部分,然后将其转储到 ID 为 div_id 的 div 中。
希望这有帮助!
Do you still have jQuery in there? I'd recommend it over Prototype any day...
If it's still there you can just use the following in your Javascript:
This gets the partial via AJAX and just dumps it into the div with id div_id.
Hope this helps!
我什至不确定您是否需要进行 AJAX 调用来加载该部分。我相信在 js.erb 文件中,对 render(:partial => object_or_path) 的调用只会返回一个包含所有 html 的字符串,您可以将其包装在 jQuery 对象中并附加。例子:
I'm not even sure you need to make an AJAX call to load that partial. I believe that in a js.erb file, a call to render(:partial => object_or_path) will just return a string with all the html, which you can wrap in a jQuery object and append. Example:
据我所知,沿着与上面答案相同的路线,您可以在模板中执行类似的操作:
在控制器中,执行以下操作:
这样您就可以让控制器“执行”客户端 JS,就像渲染一样:update 过去常做的事。这相当于在 Rails 2 中执行以下操作:
有什么理由不建议这种方法吗?
As far as I know, along the same line as the answer above, you can do something like this in your template:
And in controller, do this:
This way you can have controller "execute" client side JS much the same as as render :update used to do. This is equivalent to doing the following in Rails 2:
Is there any reason why this approach is not advisable?
试试这个:
Try this: