3

I have a mailer class that wants to get the remote user. I access it my request.env['REMOTE_USER'] but get request not defined error. i thought i were able to use request anywhere?

undefined local variable or method request for my mailer

usha
  • 28,973
  • 5
  • 72
  • 93
user1510412
  • 133
  • 2
  • 8
  • 2
    `i thought i were able to use request anywhere`. Totally wrong. You can only access it in controller, view and helpers. – usha Feb 06 '14 at 22:22

1 Answers1

2

Why don't you try loading the remote user in your controller action, and either storing in the db, or redis when you're compiling your mailer


Request

The problem, as stated by Vimsha, is you're not able to access request params outside of front-facing code

According to Rails, how do you access the raw request data at the absolute lowest level?, you can access the data directly from the middleware if you're working with a direct request (I.E your mailer is not delayed etc)


Structure

Further to this, I would also question why you're accessing request-specific data directly from your mailer

The modular (DRY) nature of Rails means you need to be able to re-use as many of the assets as possible, meaning you may wish to use the mailer in a totally unrelated action

I'd personally store the request data in the db (or redis if you only want it temporarily), and access it on demand

Community
  • 1
  • 1
Richard Peck
  • 76,116
  • 9
  • 93
  • 147