What exactly does map.connect mean?

This is a beginner's question. I am going through Agile Web Development with Rails, First Edition. I am in chapter 16 titled "Action Controllers and Rails" section 16.3 titled "Routing Requests" on page 291. I have run into a conceptual mental block. I don't understand what EXACTLY is the function of map.connect method in the following code:

ActionController::Routing::Routes.draw do |map|    map.connect ':controller/:action/:id' end

I have just spent a couple of hours googling to look-up the references, but everyone of those assume that you KNOW what "map.connect" means. If someone can also point out how to go about looking up this information in Rails API or any other suitable source, I will really appreciate it. In other words, help me learn how to fish....

Thanks in advance.

Bharat

Hi --

This is a beginner's question. I am going through Agile Web Development with Rails, First Edition. I am in chapter 16 titled "Action Controllers and Rails" section 16.3 titled "Routing Requests" on page 291. I have run into a conceptual mental block. I don't understand what EXACTLY is the function of map.connect method in the following code:

ActionController::Routing::Routes.draw do |map|   map.connect ':controller/:action/:id' end

I have just spent a couple of hours googling to look-up the references, but everyone of those assume that you KNOW what "map.connect" means. If someone can also point out how to go about looking up this information in Rails API or any other suitable source, I will really appreciate it. In other words, help me learn how to fish....

In Chapter 17 of "Ruby for Rails" I offer some techniques for how to fish through the source code -- including the almighty grep, as well as some less brute-force approaches :slight_smile: It's not always easy, but it's usually very interesting and educational.

The code you're seeing in routes.rb uses techniques defined in routing.rb, in the ActionController library. The draw method yields a Mapper object; "map" is that object. You then call the method "connect" on the Mapper object. connect has the effect of registering information about a given route with the routing system. In other words, you're specifying routing rules and handing them off to a Mapper object, which then stores them in its own data structures which it later consults when it needs to do routing.

David

David's answer is (obviously) correct - he wrote the book on the subject. But if you looking for a beginners take on it (mine), try this:

map.connect lets you tell Rails how you want to handle incoming requests. The code "map.connect ':controller/:action/:id'" says I want the first part of the URL to be the name of the controller followed by a "/", then the action followed by a "/", then the id.

The ":" sort of says "store this in a variable called" (more accurately, store this in the params hash with the following key). You can actually put any key in there, but these are the conventions.

Thanks gentlemen for your responses.

Andrew, it certainly helps to get someone's perspective who is new to rails as I am. I appreciate your response. I backtracked through the book and re-read the first ten pages of this chapter and things are a lot clearer now.

David, I have your book and have gone through it cover-to-cover actually repeating a few chapters twice. I have gotten used to writing snippets of test programs in "irb" as you do throughout your book. Actually, I was able to locate the code in routes.rb file, but could not understand it. As you are explaining above,

"the draw method yields a Mapper object; "map" is that object. ....."

The Mapper object that you are mentioning here would be the class Route right? which is defined in the the same source file "routes.rb"?

Coming back to irb. Is there any easy way of testing the URL to Params hash transformation from irb? Or do I have to do it the hard way and insert logging statements in the config/routes.rb file?

What I am trying to do is to test various mappings of URLs and corresponding rails transforms into the Params hash as stated in the Agile Web Development Book..."

Thanks again for your time. Bharat

I have been trying to follow-up on David's reply above and googled quite a bit on Routes.rb. I found Jamis Buck's blog related to this topic at the URL below:

http://weblog.jamisbuck.org/2006/10/2/under-the-hood-rails-routing-dsl

This is very informative. One thing that puzzles me is that both David and Jamis refer to Mapper object and I could not find a mapper object in the file Routing.rb. As a matter of fact Jamis specifically discusses the draw method in his article as below:

The RouteSet#draw method is where the DSL magic all begins. If you look in the routing.rb code (around line 1113), it's only three lines long, but what a significant three lines those are!

def draw   clear!   yield Mapper.new(self)   named_routes.install end

I don't have that draw method in my Routing.rb file much less the Mapper object! As a matter of fact, my Routing.rb file is only 716 lines long! I think that I Yum installed Ruby and then gem installed Rails on my machine following rather simple instructions given for Fedora Core 5 (my operating system). Everything has worked OK thus far! I have followed David's book from start to finish and have worked through Agile Web Development First Edition sample application. If someone can shed some light on this, I will really appreciate it. Note that the path for the Routing.rb on my machine is as below: /usr/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller Thanks. Bharat

Hello All, I think that I can answer my own question (did require some work and money). I have Chad Fowler's excellent book titled 'Rails Recipes' where he gives a recipe to trap the routing by created in routes.rb. It is a very easy yet quite effective way of testing the routs. This is recipe number 36 on page 153 of his book and is called "Make Your URLs Meaningful (and Pretty)." Regards, Bharat

Hi --