Using routes in your model in Rails 3.0.x

I am trying to use routes in my model so that I can generate links in a spreadsheet, but I kept getting this error:
RuntimeError (Missing host to link to! Please provide :host parameter or set default_url_options[:host]):

So I started doing some digging and this is what I came up with.

To use routes in your model in Rails 3.0.x you can do this in your model:

class Example < ActiveRecord::Base
  include Rails.application.routes.url_helpers
  Rails.application.routes.default_url_options = ActionMailer::Base.default_url_options

This allows you to avoid duplication and allows you to set the default url options in your config environment like this:

ExampleApp::Aplication.configure do
  config.action_mailer.default_url_options = {:host => ""}

27 notes

  1. slaive-prog posted this