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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容