Forum: Ruby on Rails Can't override routing code

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
4a551074ddba4460f95d011c47190d0e?d=identicon&s=25 Henrik N (Guest)
on 2006-04-23 17:18
I want to change some of Rails' routing code so that I can make routes
subdomain-sensitive.

The code at http://wiki.rubyonrails.com/rails/pages/RoutingSubdomains
works fine when I edit the file (in my case,
C:\Program\Ruby\lib\ruby\gems\1.8\gems\actionpack-1.12.1\lib\action_controller\routing.rb)
directly.

Changing that file seems very fragile, so I figured I would override the
method in question in environment.rb instead. However, it seems my
routing code is never used for routing...

I added this to the end of environment.rb and rebooted the server:

  class ActionController::Routing::RouteSet
  	def recognize(request)
  		"some text for testing"
  	end
  end

Now, if I do this in some view, I get the output "some text for
testing":

  DEBUG: <%=ActionController::Routing::RouteSet.new.recognize(:foo)%>

but the new RouteSet#recognize method is obviously not being used for
actual routing, since the routing keeps on working.

If I change the method directly in routing.rb to simply return "some
text for testing", routing breaks completely.

Again, could anyone please tell me what I'm doing wrong, and how to fix
it?

I suppose a workaround would be to include Rails in the lib directory
and modify that, but that's still a lot uglier than just modifying that
one method.
4a551074ddba4460f95d011c47190d0e?d=identicon&s=25 Henrik N (Guest)
on 2006-04-23 17:38
Henrik N wrote:
> I want to change some of Rails' routing code so that I can make routes
> subdomain-sensitive.
> ...

Solved it myself. I simply needed to include
        alias :recognize! :recognize
in my modification, since routing apparently uses recognize! rather than
recognize internally, and since overriding the method apparently means
any aliases need overriding as well.
This topic is locked and can not be replied to.