Skip to content

Instantly share code, notes, and snippets.

@between40and2
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save between40and2/6393067b3ac195586278 to your computer and use it in GitHub Desktop.
Save between40and2/6393067b3ac195586278 to your computer and use it in GitHub Desktop.

Paths come with marketing and perception identity purpose, while controller-action is purely technical. Routes decouples these two parts to make it flexible and controller-action reusable.

Tips

Different paths may lead to same controller-action. That is where you need to apply :defaults options to differentiate the paths. controller-action can check params on the defaults. For example, both account creation form and account registration form can share the same accounts#new. :default can be set with usecase: creation or usecase: registration respectively.

:as option is encouraged to apply, so that in your controller or view you can use named path/url to refer to the link. This sets flexibility when you change paths frequently. This is especially handy for routes in engine/plugin, since you cannot predicate how a hosting app set path for engine itself when in use.

No such thing like redirect in route. You can just refer the path to controller-action you want it to be. Example: root 'cc\topics#index

There are more than one way to do it. Do not be confused with many handy shortcut in composing routes. Some means the same thing. Example:

match 'photos/:id' => 'family/photos#show'
match 'photos/:id', to: 'family/photos#show'
match 'photos/:id', module: 'family', controller: 'photos', action: 'show'

ActionDispatch::Routing::Mapper::Base

link: ActionDispatch::Routing::Mapper::Base

  • default_url_options(options)
  • default_url_options=(options)
  • has_named_route?(name)
  • match(path, options=nil)
  • mount(app, options = nil)
  • root(options = {})
  • with_default_scope(scope, &block)

Options

  • :as The name used to generate routing helpers.
  • :via Allowed HTTP verb(s) for route.

ActionDispatch::Routing::Mapper::HttpHelpers

link: ActionDispatch::Routing::Mapper::HttpHelpers

  • delete
  • get
  • patch
  • post
  • put

ActionDispatch::Routing::Mapper::Resources

link ActionDispatch::Routing::Mapper::Resources

  • collection
  • match
  • member
  • namespace
  • nested
  • new
  • resource
  • resources
  • resources_path_names
  • root
  • set_member_mappings_for_resource
  • shallow
  • shallow?
  • using_match_shorthand?
  • with_exclusive_scope
  • with_scope_level

ActionDispatch::Routing::Mapper::Scoping

link: ActionDispatch::Routing::Mapper::Scoping

ActionDispatch::Routing::Mapper::Concerns

link: ActionDispatch::Routing::Mapper::Concerns

  • concern(name, callable = nil, &block)
  • concerns(*args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment