Factor out a url_for method and use it from link_to and form_tag

This should fix #739 and it just generally makes our `link_to` magic available on its own for plain URLs or for use in other helpers.

While I was at it I also rewrote the core of `link_to` (which I moved to `url_for`) to be easier to understand and more efficient.