美文网首页
3. REST, Resources, and Rails -

3. REST, Resources, and Rails -

作者: 介可能是只乌龟 | 来源:发表于2016-04-03 23:48 被阅读0次

    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
    

    相关文章

      网友评论

          本文标题:3. REST, Resources, and Rails -

          本文链接:https://www.haomeiwen.com/subject/fvmulttx.html