打印

版主能给一个完整的ror+AJAX的例子不?

版主能给一个完整的ror+AJAX的例子不?

版主能给一个完整的ror+AJAX的例子不?自己曾找到一些但都有错误没时间研究,希望能给一个简单完整的例子.

TOP

书上不都有吗?用link_to_remote之类的来做```

TOP

这种例子各类rails相关的书上应该都有,这里给出一个非常简单的。

功能:当点击页面上的一个按钮时,刷新给定区域的值为服务器的当前时间。

生成controller

ruby script\generate ajax index update

ajax_controller.rb
复制内容到剪贴板
代码:
class AjaxController < ApplicationController

  def index
  end

  def update
    render_text Time.new.to_s
  end
end
index.rhtml
复制内容到剪贴板
代码:
<%= javascript_include_tag :defaults%>

<div id="time">
</div>

<%= link_to_remote "refresh time",
                    :update => "time",
                    :url => {:action => "update"}
                    %>
当点击页面上的refresh time按钮时,就会刷新time的内容为服务器的当前时间
First they ignore you, then they laugh at you, then they fight you, then you win.

TOP

谢谢版主的帮助,我已照着例子调试通过,但我现在要处理的是:有一下拉列表框(select),当选中不同的值时下边一表格的数据显示与该选中项有关的数据,也就是在它的onChange事件中处发AJAX,这个应用link_to_remote,form_remote_tag,observers中的哪一个呢,还是有其它方法?
   另外我原来在PHP中调用AJAX文件(js文件)现在搬到ROR中都不行了,这是为什么呢?在PHP中用得好好的啊,而且很灵活可以用在任何事件中,在ROR中怎么不行,不用它自带的AJAX类不行吗?

TOP

不是很懂。
不用它自带的可以啊,你可以自己写JS文件的```

TOP

你完全可以不用ror提供的,而使用自己的ajax实现

那些tag不过是提供了一些便利而已
First they ignore you, then they laugh at you, then they fight you, then you win.

TOP

我原来在PHP中写的AJAX代码拿过来用有错误啊,正因此郁闷着哩.IE下老提示语法错误,FireFox却能通过,不知道怎么回事?

TOP

估计是浏览器不兼容

其实ror不过是对prototype等进行了包装,本质还是生成javascript调用prototype提供的Ajax相关的方法,所以你不用它也完全没有关系

但既然选择了ror,建议还是好好研究一下rails对ajax的实现支持

推荐看一下《Ajax on Rails》这本书
First they ignore you, then they laugh at you, then they fight you, then you win.

TOP

哦,这本书有电子版的没?中文的吗?
问题已搞定,就是用它自身的AJAX,用form_remote_tag,observers都可以,但用Observers占用资源,我就用from_remote_tag.
谢谢大伙的帮助啊

TOP

我这里有一个英文电子版,不过是2006年出版的

可以到网上搜一下下载
First they ignore you, then they laugh at you, then they fight you, then you win.

TOP