Tuesday, January 22, 2008

Rails Routes Hell

I have spent the last day in the seventh level of Rails hell (or something like that... it's been a while since I've read my Dante). Quite simply, I have been trying to have routes send a url in the form /cgi-bin/query?command=5 to a different controller/action based on the command parameter.

To be honest, I'd prefer to just remove the asinine code in the first place, but given that I don't have time to rewrite a fairly large client, it would be a great deal easier if I could just do a simple url rewrite.

Well, since I did work a solution out, I'll post it here, but good God (what's with my theological allusions today?) - if you have a better way, please tell me.




module ActionController
module Routing

class Route

TESTABLE_REQUEST_METHODS = [:command]

def recognition_conditions
result = ["(match = #{Regexp.new(recognition_pattern).inspect}.match(path))"]
conditions.each do method, value
if TESTABLE_REQUEST_METHODS.include? method
result << method =""> request.method,
:command => request.query_parameters['command']
}
end
end
end
end

and then in my controller:
map.connect 'cgi/query', :action => 'homepage', :controller => 'home', :conditions => { :command => /1/ }


This works, but isn't there are a good way of doing this without have to extend Rails internals? It just seems like there should be an easier way. I read Jamis articles, and while I learnt a great deal about the routes system, I still didn't get how to do any better that this.

http://weblog.jamisbuck.org/2006/10/4/under-the-hood-route-recognition-in-rails

and his routing trick plugin...
http://www.workingwithrails.com/railsplugin/4789-routing-tricks.

No comments: