views:

126

answers:

1

I have is an application can be accessed from 2 different URLs and looks/behaves slightly different depending on which URL you use to access it. I'm able to do this by storing a value in the session object and basing some decisions off of it such as which path views get loaded from.

This sort of approach seemed like it would work until I needed to have different URL's sent out in emails. In a class that inherits from ActionMailer, I need to set the default_url_options[:host] based on the value of a session variable. Rails throws the following error when I call session from anywhere within the mailer:

undefined local variable or method `session' for ApplicationMailer:Class

The less-than-desirable way to handle this is to pass the session variable into my mailer calls. I'd rather not do this as it doesn't seem very DRY and would require changes to much of my code.

+2  A: 

Whether you can shoehorn a reference to the session into the mailer or not, I think you've already hit upon the correct solution. Passing in the context you want to use would be preferable for a couple of reasons.

  1. The mailer probably shouldn't know about the session in the first place.
  2. Assume that someday you have to send a lot of mail, and batch process it. You'll be right back to where you are now -- having to pass in your context.
jdl