rails4 concerns

原创文章,转载请注明来源并保留原文链接

最近把rails4拉下来跑了一下,发现在controllersmodels下多了一个新的文件夹concerns

原来concerns里面放的就是之前写rails库经常用到的ActiveSupport::Concern,解决的问题是models或者controllers太臃肿了(当业务量庞大的时候),不过看起来只是代码的抽离。

相关链接:

f6bbc3f582

put-chubby-models-on-a-diet-with-concerns

顺带提一下,路由里面也加了一个concern

https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/routing/mapper.rb#L1629

concern :commentable do
  resources :comments
end

concern :image_attachable do
  resources :images, only: :index
end

# These concerns are used in Resources routing:

resources :messages, concerns: [:commentable, :image_attachable]

# or in a scope or namespace:

namespace :posts do
  concerns :commentable
end

One thought on “rails4 concerns

  1. 你很精通技术,不错,向你学习。
    有些机会想和你聊聊,
    方便的话加我QQ:624449684

Leave a Reply

Your email address will not be published. Required fields are marked *