InheritedResources 简介 2

对于下面的这几行,比较难理解,有空再说吧。

Our first attempt to do this would be:

class ProjectsController < InheritedResources::Base
  def update
    update! do |format|
      unless @project.errors.empty? # failure
        format.html { redirect_to project_url(@project) }
      end
    end
 end
end

Looks too verbose, right? We can actually do:

class ProjectsController < InheritedResources::Base
  def update
    update! do |success, failure|
      failure.html { redirect_to project_url(@project) }
    end
  end
end

还有这个 坑人 的 聪明 的重定向, 在这下面的代码中CUD
发起 create , update , destroy ... 等请求(或对就 Post, Get )会重定向到 belongs_to 所对应的 资源

ass ButtonsConntroller < InheritedResources::Base belongs_to :window actions :all, :except => [:show, :index] end

对就 model 中的一些 关联 一样,我们在 controller 中也可以用 belongs_to 和 Nested belongs to 如

class TasksController < InheritedResources::Base
  belongs_to :project
end

class CommentsController < InheritedResources::Base
  nested_belongs_to :project, :task
end

还有的东西很坑人,需要的自己看吧。

(内容为原创,仅为便于自己使用时查看。)

posted @ 2012-02-24 19:13  kelby  阅读(205)  评论(0编辑  收藏  举报