4

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.

4

1 回答 1

3

无论您是否可以将会话引用硬塞到邮件中,我认为您已经找到了正确的解决方案。出于几个原因,传递您想要使用的上下文会更好。

  1. 发件人可能一开始就不应该知道会话。
  2. 假设有一天您必须发送大量邮件,并对其进行批处理。您将立即回到您现在所在的位置 - 必须在您的上下文中传递。
于 2009-09-23T18:31:00.547 回答