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|
"Logout"
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|
"Logout"
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.