3. REST, Resources, and Rails - 3.8 Routing Concerns

3.8 Routing Concerns

One of the fundamental principles Rails developers follow is Don’t Repeat Yourself (DRY). Even though thisis the case, the config/routes.rb file can be prone to having repetition in the form of nested routes that areshared across multiple resources.

resources :auctions do
  resources :bids
  resources :comments
  resources :image_attachments, only: :index
end

resources :bids do 
  resources :comments
end

To eliminate some code duplication and to encapsulate shared behavior across routes, Rails 4 introduces the
routing method concern.

concern :commentable do 
  resources :comments
end

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

To add a routing concern to a RESTful route, pass the concern to the :concerns option.

# The :concerns option can accept one or more routing concerns.
resources :auctions, concerns: [:commentable, :image_attachable] do 
  resources :bids
end

resources :bids, concerns::commentable
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 偶然在网易上听到这首歌,本来在提笔练字的我,突然想写点什么东西,许是歌词太有感触吧,说出来都感觉自己有点沧桑的感觉...
    沫宝贝wy阅读 3,863评论 0 0
  • 人们总是戏谑自己,“过个春节胖三斤”。有些人还不止,可能五斤、十斤。 朋友们都在微信圈里晒过年期间的生活百态,体重...
    木皮叔阅读 3,211评论 0 4
  • 蓓蕾的心事,舞曳蕊的绽放你是天边多情的云么诺言抹不去散了聚,聚了散的愁绪迷惘在空气里,随风一声叹息 缱绻的梦镜,醉...
    蓝蓝的子夜爱在阳光阅读 3,130评论 48 70
  • AsyncTask这个类大家用以前用的比较多,具体怎么用就不再去详细讲解了,谷歌前前后后修改过几次AsyncTas...
    Nicole被占用阅读 2,875评论 0 0

友情链接更多精彩内容