Rails的资源嵌套

[b][align=center]嵌套的资源[/align][/b][size=medium][/size]
当适用嵌套的资源的时候,REST的开发会变得更加有趣。在这个章节,你会更加明白简洁的URL的URL的重要性,也会对REST的理念有更清晰的理解。

嵌套的资源,也就是所说的父—子关系的资源。在Rails中,也就是一种model的关系:1对多关系。在我们这个 ontrack 的例子项目中,就好像projects 和 iterations 的关系一样。嵌套的REST controller 仍然负责处理某一个资源的操作,但是对于一个“子”controller来说,它还必须获得“父”资源的信息。

听起来很复杂,不过阅读完这个章节,你很快就会完全明白的。

根据Rails 的REST方式,Rails 将资源的这种主—从关系反映到URL里,并且保持了URL简洁这一重要的特性。在这个ontrack例子里,我们会通过两个资源 project 和 iteration来描述这一点。

首先,我们创建 iteration 这个资源,并且创建 iterations 这个表。

> ruby script/generate scaffold_resource iteration name:string \
start:date end:date project_id:integer
> rake db:migrate

Projects 和 Iterations 是“1 对多”的关系,所以我们要修改一下model:

Listing 1.4: ontrack/app/models/project.rb
class Project < ActiveRecord::Base
has_many :iterations
end


Listing 1.5: ontrack/app/models/iteration.rb
class Iteration < ActiveRecord::Base
belongs_to :project
end

除了创建了 model, controller 和 view, 生成器同时在 config/routes.rb 里,创建了一个路由的定义项:

map.resources :iterations


这个路由项和资源project 的非常类似,不过我们别忘了 iteration 和 project的关系。但是很明显,这个路由项并没有考虑这一点。例如,new_iteration_path方法生成了一个URL “/iterations/new”,并没有包含这样一个重要的信息:这个iteration 应该属于哪个 project?所以,我们应该意识到,如果没有一个“父”资源,那么一个“子”资源是没有任何意义的!

Rails 会把这种主—从的关系反映到URL里,所以,我们需要修改一下默认生成的路由项:

map.resources :projects do |projects|
projects.resources :iterations
end

现在这个路由项成为了一个嵌套的资源了,而且你要操作iteration这个资源,就必须基于project 这个资源之上。与之相对应的URL应该是下面这个样子:

/project/:project_id/iterations
/project/:project_id/iterations/:id

例如,如果我输入了这个URL
http://localhost:3000/projects/1/iterations


将会调用 IterationController 的 index 方法,在这个方法里,也可以通过所提交的参数 :project_id 来得到资源project。值得注意的是,URL关联一个资源的这个特性,其实就是等同于下面的关系:

/projects/1/iterations <=> Project.find(1).iterations


嵌套的URL仍然是简洁的URL――URL中仍然只表明的资源,而没有action。简单的说,如果一个资源使用2个REST风格的URL构成,那么它就是一个嵌套的资源。下面这个调用 show action 的URL能让我们清晰的了解这一点:
http://localhost:3000/projects/1/iterations/1


嵌套资源在controller 的代码

新生成的IterationController 并不知道它现在已经要处理嵌套的资源了――这意味着每个方法,都至少应该得到“父”资源project。所以,现在的index方法,仍然是显示全部的 iterations,尽管URL已经表明应该显示的是某一个project下的全部iterations:

Listing 1.6: ontrack/app/controllers/iterations controller.rb
def index
@iterations = Iteration.find(:all)
respond_to do |format|
format.html # index.rhtml
format.xml { render :xml => @iterations.to_xml }
end
end


我们必须重新写index方法,以保证我们只拿某一个project下的iterations。

Listing 1.7: ontrack/app/controllers/iterations controller.rb
def index
project = Project.find(params[:project_id])
@iterations = project.iterations.find(:all)
...
end

我们必须要让controller里全部的方法都能工作在 以/projects/:project_id 为前缀的URL上。这就意味着,我们不仅要修改 index方法,create, update 等等方法也必须进行修改。下面的章节我们会逐步介绍。

在 “path” 和 “url” helper 方法里使用参数
在 config/routes.rb 里新增加的资源,不仅仅只是增加了一个新的路由的定义,同时也自动地增加了新的helper 方法。正如定义的路由那样,新的helper方法需要一个 project-id作为参数。例如 通过 “iterations_path”这个helper 方法,来得到某一个project 下的全部的iterations。Helper 方法的名字并不是以嵌套的方式命名的,所不同的只是传递的参数不一样。对于嵌套式的资源来说,“子”资源的helper 方法的参数,通常都是“父”资源的资源id,在这个例子里就是 project的id。

下面作为例子,我们就来创建一个链接,这个链接可以显示一个project下的全部 iterations。

link_to "Iterations", iterations_path(project)
=>
Iterations

其中 iterations_path 的参数“project”就是一个资源的对象。为了更好的理解这个方法的作用,我们把它放到一个页面里来看看:Listing 1.8: ontrack/app/views
/projects/index.rhtml
...
<% for project in @projects %>

<%=h project.name %>
<%=h project.desc %>
<%= link_to "Iterations", iterations_path(project) %>
<%= link_to "Show", project_path(project) %>
<%= link_to "Edit", edit_project_path(project) %>
<%= link_to "Destroy", project_path(project),
:confirm => "Are you sure?", :method => :delete %>

<% end %>

...
那么如果我们传递给 iterations_path 错误的参数会怎么样呢?那将会导致所有的功能都实效,而且页面的显示也会不正常。例如下面这个现实全部iterations 的页面:

Listing 1.9: ontrack/app/views/iterations/index.rhtml
...
<% for iteration in @iterations %>

<%=h iteration.name %>
<%=h iteration.start %>
<%=h iteration.end %>
<%= link_to "Show", iteration_path(iteration) %>
<%= link_to "Edit", edit_iteration_path(iteration) %>
<%= link_to "Destroy", iteration_path(iteration),
:confirm => "Are you sure?", :method => :delete %>

<% end %>

...
我们看到,第一个参数现在都是 iteration对象。这就导致所有的方法都失效了---原因很明显,因为在 /config/routes.rb 里,我们定义的是第一个参数应该是 project id, 而不是 iteration id。如果想要这个页面显示正常,需要作如下修改:

Listing 1.10: ontrack/app/views/projects/index.rhtml
...
<% for iteration in @iterations %>

<%=h iteration.name %>
<%=h iteration.start %>
<%=h iteration.end %>
<%= link_to "Show", iteration_path(iteration.project,
iteration) %>
<%= link_to "Edit", edit_iteration_path(iteration.project,
iteration) %>
20 1 RESTful Rails
<%= link_to "Destroy", iteration_path(iteration.project,
iteration), :confirm => "Are you sure?",
:method => :delete %>

<% end %>
...

为了让参数的顺序正确,我们还可以用另一种显示指定参数的方式:
iteration_path(:project_id => iteration.project, :id => iteration)
如果您觉得用对象作为参数不够清晰,那么就可以考虑一下这个方式。

增加新的Iteration
我们仍然是在当前的例子中增加这个功能。为了实现这个功能,我们只需要简单的修改一下 ProjectController 的 index.rhtml :
Listing 1.11: ontrack/app/views/projects/index.rhtml
...
<% for project in @projects %>

<%=h project.name %>
<%=h project.desc %>
<%= link_to "Iterations", iterations_path(project) %>
<%= link_to "Show", project_path(project) %>
<%= link_to "Edit", edit_project_path(project) %>
<%= link_to "Destroy", project_path(project),
:confirm => "Are you sure?", :method => :delete %>
<%= link_to "New Iteration", new_iteration_path(project)
%>

<% end %>
...

这里我们使用了 “new_iteration_path”这个helper 方法,并且把project 这个对象作为参数传了进去。这个 helper 方法会生成如下的html语句:

link_to "New Iteration", new_iteration_path(project)
=>
New iteration


如果您点击这个链接,那么就会调用 IterationController 的 new 方法,在这个方法里,你就可以得到 project id ( 在这个例子里就是“1”)。这样,用于创建新的iteration的form 就可以使用这个project id 了:

Listing 1.12: ontrack/app/views/iterations/new.rhtml
<% form_for(:iteration,
:url => iterations_path(params[:project_id])) do |f| %>
...
<% end %>
=>


这个“params[:project_id]”其实也可以省略,Rails 会自动处理这个变量,也就是说,上面的代码,和下面的是等效的:form_for(:iteration, :url => iterations_path)因为我们之前在 /config/routes.rb 里定义了路由,这样就确保 使用post方式提交 “/projects/1/iterations”链接时,一定会调用IterationController 的 create 方法。
下面,我们要修改一下 IterationController里的create 方法,以保证我们创建的iteration 是基于某一个project 之上的:

Listing 1.13: ontrack/app/controllers/iterations controller.rb
1 def create
2 @iteration = Iteration.new(params[:iteration])
3 @iteration.project = Project.find(params[:project_id])
4
5 respond_to do |format|
6 if @iteration.save
7 flash[:notice] = "Iteration was successfully created."
8 format.html { redirect_to iteration_url(@iteration.project,
9 @iteration) }
10 format.xml { head :created, :location =>
11 iteration_url(@iteration.project, @iteration) }
12 else
13 format.html { render :action => "new" }
14 format.xml { render :xml => @iteration.errors.to_xml }
15 end
16 end
17 end


在第“3”行,我们使用了“project_id”这个参数,在第“8”行和第“11”行,我们使用了 “url”helper 方法。
下面我们还需要修改一些显示,编辑iteration的链接—因为我们必须把iteration和project 关联在一起。

Listing 1.14: ontrack/app/views/iterations/show.rhtml
...
<%= link_to "Edit", edit_iteration_path(@iteration.project, @iteration)
%>
<%= link_to "Back", iterations_path(@iteration.project) %>


编辑 Iteration
为了能够编辑 iteration,至少需要修改2个地方。1〉在视图中的form_for 方法的参数,目前的参数只有 iteration 一个,还需要增加 projectid。

form_for(:iteration,
:url => iteration_path(@iteration),
:html => { :method => :put }) do |f|

需要修改成:

form_for(:iteration,
:url => iteration_path(params[:project_id], @iteration),
:html => { :method => :put }) do |f|


我们还需要修改 update 方法,修改的目的是一样的。

Listing 1.15: ontrack/app/controllers/iterations controller.rb
1 def update
2 @iteration = Iteration.find(params[:id])
3
4 respond_to do |format|
5 if @iteration.update_attributes(params[:iteration])
6 flash[:notice] = "Iteration was successfully updated."
7 format.html { redirect_to iteration_url(@iteration) }
8 format.xml { head :ok }
9 else
10 format.html { render :action => "edit" }
11 format.xml { render :xml => @iteration.errors.to_xml }
12 end
13 end
14 end


第“7”行需要修改成:
format.html { redirect_to iteration_url(@iteration.project,@iteration) }


到目前为止,新增加的资源 Iteration的操作大部分都可以正常工作了,但是还有一些细节的地方我们没有处理。下一回将讲一下如何自定义Action。


--转自women and men([url]http://www.hezubbs.cn/html/ruby/200901/yingyongRailsjinxingRESTkaifa-wu-_366.html[/url])
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值