Some suggestions on the Ruby on Rails routing configuration _ruby topics

Source: Internet
Author: User
Tags comments ruby on rails

When you need to add one or more actions to a RESTful resource (do you really need it?) ), using the member and collection routing.

  # get
  ' subscriptions/:id/unsubscribe '
  resources:subscriptions

  # good
  resources:subscriptions do Get
   ' unsubscribe ', on:: member
  end

  # photos/search ' to ' resources
  :p hotos

  # good
  Resources:p Hotos does get
   ' search ', On:: Collection
  End

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

 Resources:subscriptions do
   -get
    ' unsubscribe '
    # More routing end
  -Resources

  : Photos do
   collection does get
    ' search '
    # more route
   End
  

Use nested routines to better express the relationship with the ActiveRecord model by (nested routes).

 Class Post < activerecord::base
   has_many:comments
  End

  class Comments < ActiveRecord::Base
   Belongs_to:p ost

  # routes.rb Resources
  :p OSTs does
   resources:comments
  end

Use namespace routing to group related behavior.

  Namespace:admin do
   # directs/admin/products/* to admin::P roductscontroller
   # (app/controllers/admin/ PRODUCTS_CONTROLLER.RB)
   :p roducts End
  

Do not use the crazy route left behind in the controller (Legacy Wild Controller route). This route allows the actions of each controller to be accessed through a GET request.

  # 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.