对下面两个插件有依赖,分别为 HasScope 和 Responders
他们三者安装都很简单,直接 gem install 或者 bundle 就行。
对于 controller 中,常见的 flash 提示如 flash[:success] , flash[:failure]
我们可以用 Responders 聪明的我们减少重复造轮子
InheritedResources.flash_keys = [ :success, :failure ]
对于 scaffold 生成的 7 个方法,你是否已烂记于心? 那么让我们用一句代码 ‘屏蔽’ 它。 把这
class ProjectsController < ApplicationController::Base
end
改成这
class ProjectsController < InheritedResources::Base
end
当然了,对于常见的 respond_to 还可以照久使用,例如加上
respond_to :html, :xml, :json
和自带的 respond_to 不同的是你还可以 用可选参数(option)的形式指定所要作用于的 Action , 例如
class ProjectsController < InheritedResources::Base
respond_to :html, :xml, :json
respond_to :js, :only => :create
respond_to :iphone, :except => [ :edit, :update ]
end
当然了,我们有时并不需要 7 个方法,而只是其中的 一部分, 这也很简单,只需用 actions 方法指定就行了。
class ProjectsController < InheritedResources::Base
actions :index, :show, :new, :create
end
或者这样
class ProjectsController < InheritedResources::Base
actions :all, :except => [ :edit, :update, :destroy ]
end
在你的视图里,还会自动有这几个 辅助 方法(虽然我们可能永远也不用!)
resource #=> @project,对应其它 action (除 index 外)
collection #=> @projects,对应 index
resource_class #=> Project, (不常用)
当然了,我们对于这么 ‘抽象’ 的重构还不习惯,那么我们不必 ‘冒险’ 继承于
class ProjectsController < InheritedResources::Base
而只是普通的 调用‘
class AccountsController < ApplicationController
inherit_resources # 对,就是这一行代码,一样的神奇 !
end’
上面提到的,看起来很简单(实际上也是)。下面来点 ‘高难度的’
当我们的 controller 名 和 实际 想要的 资源(Resources)不一样时, InheritedResources 不会聪明的 转换, 这就需要我们来告诉它。
class AccountsController < InheritedResources::Base
defaults :resource_class => User, :collection_name => 'users', :instance_name => 'user'
end
对于一些 特殊 的情况,如 管理员(admin) 才能’访问‘的页面,在生成 controller 时我们可能会用 rails g controller admin::people 等等类似的语句 这时我们就要给 资源(resources )加上 前缀(prefix)
class Administrators::PeopleController < InheritedResources::Base
defaults :route_prefix => 'admin'
end
还有下面这两个定制,不太可能常用,我们就不说了,这个
class ProjectsController < InheritedResources::Base
protected
def collection
@projects ||= end_of_association_chain.paginate(:page => params[:page])
end
end
还有这个
class ProjectsController < InheritedResources::Base
protected
def begin_of_association_chain
@current_user
end
end
InheritedResources 默认作用于那 ’经典‘的 7 个方法,所以其内容也是 7 个方法里的。有时我们需要自己的,而不是默认的。但又想用 InheritedResources 的方便, 我们可以 ‘重写’ 需要 改变 的部分, 如:
class ProjectsController < InheritedResources::Base
def destroy
super do |format|
format.html { redirect_to root_url }
end
end
end
对于上面的 super, 如果你不喜欢,不放心? 怎么办, 我们来翻翻 ‘源代码’
module InheritedResources
# Holds all default actions for InheritedResouces.
module Actions
# GET /resources
def index(options={}, &block)
respond_with(*(with_chain(collection) << options), &block)
end
alias :index! :index
# GET /resources/1
def show(options={}, &block)
respond_with(*(with_chain(resource) << options), &block)
end
alias :show! :show
# GET /resources/new
def new(options={}, &block)
respond_with(*(with_chain(build_resource) << options), &block)
end
alias :new! :new
# GET /resources/1/edit
def edit(options={}, &block)
respond_with(*(with_chain(resource) << options), &block)
end
alias :edit! :edit
# POST /resources
def create(options={}, &block)
object = build_resource
if create_resource(object)
options[:location] ||= smart_resource_url
end
respond_with_dual_blocks(object, options, &block)
end
alias :create! :create
# PUT /resources/1
def update(options={}, &block)
object = resource
if update_resource(object, resource_params)
options[:location] ||= smart_resource_url
end
respond_with_dual_blocks(object, options, &block)
end
alias :update! :update
# DELETE /resources/1
def destroy(options={}, &block)
object = resource
options[:location] ||= smart_collection_url
destroy_resource(object)
respond_with_dual_blocks(object, options, &block)
end
alias :destroy! :destroy
# Make aliases protected
protected :index!, :show!, :new!, :create!, :edit!, :update!, :destroy!
end
end
从上面可以看到,作者为 7 个方法都 起了别名,这就好办多了。把上面的 super 改掉
class ProjectsController < InheritedResources::Base
def destroy
destroy! do |format|
format.html { redirect_to root_url }
end
end
end
想想 7 个方法中的 那 3 个另类(非 GET, POST请求) create,update, destroy 在完成方法后我们常常要 重定向(redirect_to ), 这样的话,为什么不 “简单一点?!(理解起来真的挺难的)” 不太推荐使用,但不使用,又不能体现其 ‘高明’
class ProjectsController < InheritedResources::Base
def destroy
destroy!{ root_url }
end
end
上面提到过 flash 默认的提示 :notice and :alert 如果你不喜欢,同样可以定制,就像平常一样写就行了
class ProjectsController < InheritedResources::Base
def create
create!(:notice => "Dude! Nice job creating that project.")
end
end
You can still pass the block to change the redirect, as mentioned above:
你同样可能这样写 注意 小括号()和 花括号{}
class ProjectsController < InheritedResources::Base
def create
create!(:notice => "Dude! Nice job creating that project.") { root_url }
end
end
对于 7 个方法中的任何 部分,你都可以定制,然后再 用 super 。。。这会 ’重写‘ InheritedResources 但再次调用时不用担心会被 ‘写回来’
class ProjectsController < InheritedResources::Base
def create
@project = Project.new(params[:project])
@project.something_special!
create! # 注意这行 !
end
end