写点什么

Rails: 用 Resource_controller 插件给 Controllers 减肥

  • 2008-02-04
  • 本文字数:2561 字

    阅读完需:约 8 分钟

许多Rails大师提倡的一个导向原则是“丰满的模型,精瘦的控制器”。 Rails 控制器只应该包含一些在模型和视图之间周转用的代码。事务逻辑属于模型,而不属于控制器或视图。在如今支持 REST的 Rails 2.0 中,控制器变得非常相象,都包括七个基本的动作(索引、获取、创建、更新、删除、新增和编辑),且具有非常相似的实现方式。在 Rails 2.0 中,可以简单地用 script/generate scaffold standard 来建立控制器框架,即生成了一段具有 REST 特性的控制器代码,如下所示:

class StandardsController < ApplicationController

GET /standards

GET /standards.xml

def index
@standards = Standard.find(:all) respond_to do <span>|</span>format<span>|</span>

format.html # index.html.erb
format.xml { render :xml => @standards }
end
end # GET /standards/1

GET /standards/1.xml

def show
@standard = Standard.find(params[:id]) respond_to do <span>|</span>format<span>|</span>

format.html # show.html.erb
format.xml { render :xml => @standard }
end
end # GET /standards/new

GET /standards/new.xml

def new
@standard = Standard.new respond_to do <span>|</span>format<span>|</span>

format.html # new.html.erb
format.xml { render :xml => @standard }
end
end # GET /standards/1/edit

def edit
@standard = Standard.find(params[:id])
end # POST /standards

POST /standards.xml

def create
@standard = Standard.new(params[:standard]) respond_to do <span>|</span>format<span>|</span>

if @standard.save
flash[:notice] = ‘Standard was successfully created.’
format.html { redirect_to(@standard) }
format.xml { render :xml => @standard, :status => :created, :location => @standard }
else
format.html { render :action => “new” }
format.xml { render :xml => @standard.errors, :status => :unprocessable_entity }
end
end
end # PUT /standards/1

PUT /standards/1.xml

def update
@standard = Standard.find(params[:id]) respond_to do <span>|</span>format<span>|</span>

if @standard.update_attributes(params[:standard])
flash[:notice] = ‘Standard was successfully updated.’
format.html { redirect_to(@standard) }
format.xml { head :ok }
else
format.html { render :action => “edit” }
format.xml { render :xml => @standard.errors, :status => :unprocessable_entity }
end
end
end # DELETE /standards/1

DELETE /standards/1.xml

def destroy
@standard = Standard.find(params[:id])
@standard.destroy respond_to do <span>|</span>format<span>|</span>

format.html { redirect_to(standards_url) }
format.xml { head :ok }
end
end
end

除了特别的名字以外,所有自动生成的控制器代码都是这样的。

使用自动生成的控制器非常简单。在许多情况下,很少或者不需要对生成的代码做任何改变,尤其是当你把“精瘦的控制器”这个理念铭记于心时。

另一方面,Ruby/Rails 还有一条理念,就是 “不要重复自己(DRY)”。 如果存在几乎重复的代码,即便不是你自己写的,也是违背DRY 原则的。

输入: resource_controller。James Golick 贡献了一个新的 rails 插件,称为 resource_controller ,它可以实现与上面同样的控制器,代码如下:

class StandardsController < ApplicationController<br></br>resource_controller<br></br>end然而,这里仍有一个小小的瑕疵。 它没有提供标准的 xml 响应能力,但可以用一小段代码来实现:

class StandardsController < ApplicationController
resource_controller index.wants.xml { render :xml => @standards }

[new, show].each do <span>|</span>action<span>|</span>
action.wants.xml { render :xml => @standard })
end
create.wants.xml { render :xml => @standard, :status => :created, :location => @standard }
[update, destroy].each do <span>|</span>action<span>|</span>
action.wants.xml { head :ok }
end
[create_fails, update_fails].each do <span>|</span>action<span>|</span>
action.wants.xml { render :xml => @standard.errors, :status => :unprocessable_entity }
end
end

有了这个插件,写控制器代码如同写模型代码一样,只需加上像resource_controller这样的声明的类方法,以及action.wants之类的“回调”。这个插件自动为控制器的每个方法分配实例变量。在上面的代码中,给 index 方法分配了 @standards ,给其他方法分配了 @standard。

Rails 有一些公用的模式强迫改变控制器代码。其中包括嵌套资源。很容易在 config/routes.rb 中设置路由:

map.resources :organization, :has_many => :standards但是,一旦你这样做了,你就需要更改控制器来获取和使用上层资源,并在每个动作中正确使用。resource_controller 插件简化了这些。在如上面那样更改路由后,你只需添加一个声明来调用我们的控制器:

class StandardsController < ApplicationController<br></br>resource_controller<br></br>belongs_to :organization<br></br>endbelongs_to 声明允许嵌套资源使用控制器。现在,当一个控制器动作通过嵌套资源 URL 被访问时,例如 /organization/1234 /standards,控制器会自动创建一个名为 @organization 的实例变量,适当地设置好,并使用 standards 所关联地父对象来查找和 建立 Standard 模型的实例。

注意, 同样的控制器也工作在非嵌套的 URL 下,因此我们可以在 routes.rb 中做另一个映射,来允许在 organization 之外访问 starnards:

map.resources :standard<br></br>map.resources :organization, :has_many :standards这样 resource 控制器就会自动地工作在这两种上下文中了。

这个插件也处理命名空间控制器、多态嵌套资源(与 ActiveRecord 多态关联相似和相关)和其他一些奇妙地东西。你也可以获得 URL 以及工作在请求的 URL 上下文中的路径辅助函数。

看来 Resource_controller 是个有用的插件,毫无疑问随着它的成熟,会变得越来越好。细节见 James Golicks 的博客。另外还有 Fabio Akita 制作的一段屏幕录像,演示了这个插件的实际使用情况。

查看英文原文: Rails: Resource_controller Plugin Puts Controllers on a Diet

2008-02-04 20:591506
用户头像

发布了 33 篇内容, 共 62481 次阅读, 收获喜欢 0 次。

关注

评论

发布
暂无评论
发现更多内容

『腾讯网』德国飞艇平台[手机乐乎]

天马行空

德国飞艇平台

长短租用户体验

张老蔫

28天写作

Android C++系列:Linux线程(二)线程原语

轻口味

android 28天写作 12月日更

极客时间【架构实战营】第二期 毕业设计

Geek_91606e

架构实战营

『腾讯网』1分彩计划软件下载[手机乐乎]

天马行空

1分彩计划软件下载

毕业总结

guangbao

35 K8S之kubeconfig配置文件

穿过生命散发芬芳

k8s 28天写作 12月日更

Nacos 本地单机版部署步骤和使用 – Spring Cloud 系列教程

程序员泥瓦匠

nacos

在 JavaScript 中创建私有成员

devpoint

JavaScript typescript ES2020 ES6 12月日更

『腾讯网』飞艇技巧图片图解【重点推荐】[手机乐乎]

天马行空

飞艇技巧图片图解

自定义规则删除过期文件(linux)

liuzhen007

28天写作 12月日更

毕业设计:电商秒杀系统架构设计

紫云

庞氏骗局:购物就能赚钱

石云升

28天写作 电信诈骗 12月日更 庞氏骗局

创业研发团的组织建设-硬件开发流程

wood

创业 敏捷 28天写作 硬件开发

对研发管理的一些思考

hackstoic

研发管理 规范

『腾讯网』幸运彩票是真是假[手机乐乎]

天马行空

幸运彩票是真是假

架构实战营毕业总结

紫云

架构实战营

模块五作业

Geek_1d37ea

架构实战营

模块五学习总结

Geek_1d37ea

架构实战营

hw6-拆分电商系统为微服务

WWH

架构实战营

模块六作业

沐风

存储引擎漫话

ElvinYang

B-tree LSM-Tree Hash Index MySQL InnoDB

『腾讯网』腾讯分分彩 官网平台[手机乐乎]

天马行空

腾讯分分彩 官网平台

Springboot异步任务执行及监控

xcbeyond

springboot 异步任务 28天写作 12月日更

再来一篇!看jdk源码大师亲自操刀编写的集合源码!

看点代码再上班

Java 架构 软件技术

浅谈指标体系建设(一)

圣迪

DDD领域驱动设计落地实践:六步拆解DDD

慕枫技术笔记

架构 后端 签约计划第二季

JetPack-Paging3-入门

Changing Lin

12月日更

Golang Gin 框架之模型绑定(八)

liuzhen007

28天写作 12月日更

现代软件架构师的10个技巧

俞凡

架构 微服务 云原生

天长地久->曾经拥有

mtfelix

28天写作

Rails: 用Resource_controller插件给Controllers减肥_Ruby_Rick DeNatale_InfoQ精选文章