Rails 1.2 REST + Adobe Spry

本文介绍了如何将Adobe的Spry框架与Rails结合使用来创建动态网页应用。通过具体的步骤和示例,展示了如何实现RESTful接口并利用Spry进行Ajax数据加载及交互。
[url=http://jerryinside.iteye.com/]jerryinside[/url]介绍了一个[url=http://www.iteye.com/topic/37180]adobe的ajax框架[/url],和当初见到rails一样,我几乎是看它一眼就爱上了。它是一个轻量级的ajax框架,以XML作为协议,目前功能还比较单一,不过几乎完全就是我所期待的。

Rails生成XML轻而易举,Rails 1.2的REST也是一项另人心动的功能,而Spry也是以资源作为操作对象,这2者结合会发生什么?我试着学习Spry的内涵,并结合Rails做点测试性应用,以确定是否值得迁移稍大点的项目到它上面。

首先建立一个项目:
[code]
rails test -d progresql
[/code]
修改配置,让它正确连接到数据库,建立相应数据库。

进入项目文件夹,安装rails 1.2
[code]
rake rails:freeze:edge TAG=rel_1-2-0_RC1
[/code]

既然要测试rest,就用scaffold_resource吧:
[code]
script/generate scaffold_resource post title:string body:text created_at:time
script/generate scaffold_resource comment post_id:integer body:text created_at:time
[/code]
它自动在config/routes.rb里面添加了2行:
[code]
map.resources :posts
map.resources :comments
[/code]
我的post和comment是一对多关联的,所以这里做点修改:
[code]
map.resources :posts do |post|
post.resources :comments
end
[/code]
并且修改Post和Comment这2个model类:
[code]
class Post < ActiveRecord::Base
has_many :comments
end

class Comment < ActiveRecord::Base
belongs_to :post
end
[/code]
因为上面已经指定了几个字段,所以它为我们生成了数据库模式,把它导入数据库:
[code]
rake db:migrate
[/code]

修改app/views/posts/index.rhtml:
[code]
<h1>Listing posts</h1>

<table>
<tr>
<th>Title</th>
<th>Body</th>
<th>Created at</th>
<th>Comments</th> # <======
</tr>

<% for post in @posts %>
<tr>
<td><%=h post.title %></td>
<td><%=h post.body %></td>
<td><%=h post.created_at %></td>
<td><%= link_to 'Comments', comments_path(post) %></td> # <======
<td><%= link_to 'Show', post_path(post) %></td>
<td><%= link_to 'Edit', edit_post_path(post) %></td>
<td><%= link_to 'Destroy', post_path(post), :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>

<br />

<%= link_to 'New post', new_post_path %>
[/code]
打标记的2行是我添加的。

接下来在CommentController里添加点代码:
[code]
before_filter :find_post
...
#最后面
protected
def find_post
@post = Post.find(params[:post_id])
end
[/code]

查找所有的comment_path,添加一个参数@post,比如comment_path(comment),就改成comment_path(@post, comment),包括所有的rhtml文件,我不知道有没有更方便的做法,不过我没找到命令行生成时指定这个参数。

查找所有的Comment.find(...),修改为@post.comments.find(...)。create方法修改成这样:
[code]
def create
@comment = @post.comments.create(params[:comment]) # <=====

respond_to do |format|
unless @comment.new_record? # <=====
flash[:notice] = 'Comment was successfully created.'
format.html { redirect_to comment_path(@post, @comment) }
format.xml { head :created, :location => comment_path(@post, @comment) }
else
format.html { render :action => "new" }
format.xml { render :xml => @comment.errors.to_xml }
end
end
end
[/code]
标记的2行是我修改的。修改就完成了,下面测试一下。

启动服务器,打开 http://localhost:3000/posts,点击“New Post”添加几条记录。

接着点击每条记录后的Comments链接,各添加几条评论。

这才刚完成服务端代码。下面是视图处理,这里不打算演示太多功能(实际上我也没研究出多少 :oops:)。

把Spry的几个脚本文件拷到public/javascripts下。

打开app/views/layout/posts.rhtml,添加2行:
[code]
<%= javascript_include_tag 'xpath' %>
<%= javascript_include_tag 'SpryData' %>
[/code]
这个演示只需要这2个文件。

把app/views/posts/index.rhtml备份一下,然后打开修改成这样:
[code]
<h1>Listing posts</h1>

<script language="javascript">
var dsPosts = new Spry.Data.XMLDataSet("posts.xml", "posts/post")
var dsComments = new Spry.Data.XMLDataSet("posts/{dsPosts::id}/comments.xml", "comments/comment")
</script>

<table spry:region="dsPosts">
<tr>
<th>Index</th>
<th onclick="dsPosts.sort('title', 'toggle')">Title</th>
<th>Body</th>
<th onclick="dsPosts.sort('created-at', 'toggle')">Created at</th>
</tr>
<tr spry:repeat="dsPosts" onclick="dsPosts.setCurrentRow('{ds_RowID}')">
<td>{ds_RowNumberPlus1}</td>
<td>{title}</td>
<td>{body}</td>
<td>{created-at}</td>
</tr>
</table>

<hr />
Comments:
<div spry:region="dsComments">
<div spry:repeat="dsComments">
{body}<br />
Comment At: {created-at}
<hr />
</div>
</div>
[/code]
这短短一点代码完成了什么?

1、ajax抓取post数据
2、点击post记录行时,下面的Comments区域将动态请求服务器,提取属于该post的comments,Cool!它也会自动判断数据是否请求过,如果已经请求过则不会再请求。
3、点击表头中的"Title"和"Created At"这2个字段,表格会排序,Cool!

暂时只研究了这么些。
内容概要:本文系统梳理了2025年数学前沿领域的研究动态与发展趋势,涵盖代数几何、数论、微分几何、拓扑学、偏微分方程、数学物理等多个核心方向,并介绍了当前国际数学研究的三大主流趋势:代数几何与数论、分析与偏微分方程、几何拓扑与表示论。文中重点报道了青年数学家王虹成功证明三维挂谷猜想的重大突破,以及韦东奕在偏微分方程与几何分析方面的研究成果,展现了中国数学界的崛起态势。同时,文档还涉及数学基础研究、应用数学、数学教育、期刊评价体系及国际数学强国格局等内容,引用大量视频、文章和权威资源,呈现数学学科的全貌与发展前景。; 适合人群:具备一定数学基础的本科生、研究生及科研工作者,关注数学前沿发展的教师、科技爱好者以及从事人工智能、物理、工程等相关领域并需数学支撑的专业人士。; 使用场景及目标:①了解2025年数学领域的重要突破与研究热点,如挂谷猜想的证明、朗兰兹纲领、拓扑数据分析等;②把握数学各分支的前沿方向与交叉应用,服务于科研选题、学术规划或跨学科研究;③获取权威学习资源与经典文献推荐,辅助数学学习与教学实践。; 阅读建议:此文档为信息聚合型资料,建议结合所列视频、书籍和论文深入拓展学习,重点关注核心突破案例(如王虹、韦东奕)与主流研究方向的演进脉络,宜以批判性思维梳理知识体系,避免碎片化阅读。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值