Rails: Routing

Guide Targets:

  • How to interpret the code in config/routes.rb?
    • http-verb + url to match specified controller and action.
  • How to construct routes using resourceful or match way?
    • use resourceful style routes is recommend in Rails.
    • resources, single resource, nested resources, namespace, etc.
  • How to declare routes parameters which passed onto controller action?
    • params are from url, post body and routing.
  • How to auto create path and urls using route helpers?
    • auto generate _path and _url helpers.
  • Advanced Techniques: constrains and mounting Rack endpoints.

My Notes

Helpers

  • play in console with app
  • app.magazine_ad_path(magazine, ad)
  • app.magazine_ad_url(magazine, ad)
  • app.url_for [magazine, ad] # call _url
  • app.root_path

Resourceful Routes

  • resources :photos
  • resource :profile
  • resources :photos, :books

Options

  • as specify routes name
  • path specify routes url path
  • module specify controller module
  • namespace both change in name, path and module
  • shallow for nested resource, only nested index, new, create

你可能感兴趣的:(Rails: Routing)