Some Suggestions on Ruby on Rails routing configuration, rubyrails

Source: Internet
Author: User

Some Suggestions on Ruby on Rails routing configuration, rubyrails

When you need to add one or more actions to a RESTful Resource (do you really need it ?), Use member and collection routes.

# Differential get 'subscriptions/: id/unsubscribe 'resources: subscriptions # Good resources: subscriptions do get 'unsubscribe', on: member end # differential get 'photos/search' resources: photos # Good resources: photos do get 'search', on: collection end

If you need to define multiple member/collection routes, use the block syntax alternative ).

Resources: subscriptions do member do get 'unsubscribe' # more routes end resources: photos do collection do get 'search' # more routes end

Use nested routes to better express the relationship with the ActiveRecord model.

 class Post < ActiveRecord::Base   has_many :comments  end  class Comments < ActiveRecord::Base   belongs_to :post  end  # routes.rb  resources :posts do   resources :comments  end

Use namespace routing to group related behaviors.

  namespace :admin do   # Directs /admin/products/* to Admin::ProductsController   # (app/controllers/admin/products_controller.rb)   resources :products  end

Do not use legacy wild controller route in the controller ). This route will allow each controller to access through GET requests.

# Very poor match ': controller (/: action (/: id (.: format )))'


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.