There are, I believe, three separate implementations of #url_for: one
in ActionController::Base, one in ActionController::UrlWriter, and one
in ActionView::Helpers::UrlHelper. I've looked for the exact
incantation for when, and in what order, to include each of these
without success; also, they communicate to one another through
instance variable, which you have to have set up correctly.
Where exactly are you running into these problems? As unsatisfying as
this answer will be for you, I generally solve these types of problems
by only creating URLs in Rails-sanctioned objects (controllers and
views). It's possible to get the behavior working in other objects,
but it will make your head hurt.