Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the Rails router Journey work?

Looking at the Readme of the Journey Router (which is the Rails 4.0 router), it is rather empty. I was wondering how the router works conceptually, and if this 'abstract' grammer is catching the idea, of the router as mini-programming-language:

ROUTE := GET|POST|PUT|DELETE   path
path := (static_path) | (dynamic_path) 

So, how does the Rails 4.0 Router work?

like image 779
poseid Avatar asked May 29 '13 20:05

poseid


People also ask

How does the rails router work?

The Rails router recognizes URLs and dispatches them to a controller's action, or to a Rack application. It can also generate paths and URLs, avoiding the need to hardcode strings in your views. When your Rails application receives an incoming request for:

How to create RESTful routes for articles using rails router?

The Rails Router verifies if there is an entry matching the requested URL. We just need to configure the routes for this line: This will create RESTful routes for articles. If we run bundle exec rake routes, it will show the list of paths created. The HTTP verb can be GET, POST, PATCH, PUT, or DELETE.

What is a resourceful route in rails?

In Rails, a resourceful route provides a mapping between HTTP verbs and URLs to controller actions. By convention, each action also maps to a specific CRUD operation in a database. A single entry in the routing file, such as: creates seven different routes in your application, all mapping to the Photos controller:

How do I test a custom route in rails?

Routes should be included in your testing strategy (just like the rest of your application). Rails offers three built-in assertions designed to make testing routes simpler: assert_generates asserts that a particular set of options generate a particular path and can be used with default routes or custom routes. For example:


1 Answers

If you want to understand Rails routing, the best place to start is Rails Routing from the Outside In in the edge guide.

After that, you'll have more luck looking at ActionDispatch::Routing's docs.

Note: The following relates to Journey v1.0.4, which was the latest at time of writing.

Journey itself is the Rails routing engine introduced in Rails 3.2. In the release notes, it is described with a single statement: "Route recognition also got a bunch faster thanks to the new Journey engine." That's not a lot of info specifically about Journey, of course, but Rails doesn't describe every implementation detail; that would take forever! :) Journey's gemspec also describes itself with the statement: "Journey is a router. It routes requests."

You could look at the api docs, but in v1.0.4, it has extremely sparse documentation (Journey::Router::Utils.normalize_path(path) is documented) other than the code itself and maybe its open and closed issues. You could take a look at the other S.O. posts with tag journey.

Some of the code is somewhat self-descriptive just via method names, etc. like in Journey::Routes. Journey's tests are also a great way to see how it works.

Some notes on the code itself:

  • It monkey-patches Hash in pre-1.9 versions of Ruby to add a keep_if method.
  • "gtg" stands for "generalized transition graph" (see here)
  • Funniest code is here (Easter egg: rails c then Journey::Path::Pattern.new(9))

Journey's visualizer might be interesting to play around with, also (note visualizer method in Journey::GTG::TransitionTable). Sample visualization here, and online demo here for now.

like image 184
Gary S. Weaver Avatar answered Sep 22 '22 01:09

Gary S. Weaver