Recent Blog Posts

  • Inbox Zero is Hard for Me
    By Johanna Rothman - Tuesday Jan, 6
    This year, after I archived my last year’s inbox, I decided my email problem was getting worse, not better. “I’m Johanna Rothman, and I have a problem collecting email in my inbox.&#... more »
  • Career Survey
    By Jared Richardson - Sunday Jan, 4
    The upcoming Career 2.0 book is in high gear, but we'd like to include more than our experiences. Over on the Career 2.0 blog we've posted a few questions about your career. Best moves, worst experien... more »
  • Tactics vs. Strategy (SOA & The Tarpit of Irrelevancy)
    By Neal Ford - Friday Jan, 2
    This is the first in a series of blog posts where I discuss what I see wrong with SOA (Service Oriented Architecture) in the way that it's being sold by vendors. The first installment is about how the... more »
  • Collaborating with Other Writers
    By Johanna Rothman - Friday Jan, 2
    Merlin, via 43 Folders Clips has a video of Eric Idle, on John Cleese’s Approach to Writing. Aside from John Cleese’s specificity, Idle talks about how he had trouble finding collaborators until... more »
  • Happy New Year
    By Johanna Rothman - Thursday Jan, 1
    Everyone, I thank you for reading and commenting. I hope you have a healthy and happy 2009. ... more »

Using routes inside Radius tags

Posted by: Matthew Bass on 11/10/2008

I’m in the middle of developing a Radiant extension for Terralien. Radius is the tagging language for Radiant, and today I figured out how to use a route from inside a custom tag class.

A typical Radius tag class might look like this:

module AwesomeTags
  include Radiant::Taggable
 
  tag "session:logout" do |tag|
    "<a href='/session/logout'>Logout</a>"
  end
end

You can see that the hard-coded URL isn’t exactly DRY. I’ve already defined a route for it. Why should I have to hard code it here? The solution:

module AwesomeTags
  include Radiant::Taggable
  include ActionController::UrlWriter  
  default_url_options[:host] = REGISTRY[:host]
 
  tag "session:logout" do |tag|
    "<a href='#{logout_url}'>Logout</a>"
  end
end

Note that UrlWriter needs to know the host name to base its URLs off of. The host name gets set using the registry pattern. It will be different depending on whether the app is running in development or production mode.

The same method can be used to reference routes from inside ActiveRecord models.


be the first to rate this blog


About Matthew Bass

Matthew Bass is an independent software developer, entrepreneur, speaker, and writer. He has over ten years of experience across a diverse set of technologies and has worked at places like SAS Institute, the world's largest privately held software company. An agilist from the very beginning, he continues evangelizing and experimenting with pair programming, test-first and behavior-driven development, and continuous integration. Matthew has spoken at several regional and national software conferences and regularly writes for publications like InfoQ.