Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can a partial detect if it's being rendered by a mailer?

I have a partial that is being shared between a few different views, and a mailer template. This partial should attempt to use the user's session to store some state information if possible.

Determining if the session exists seems to be a bit of a problem. Within the partial, calling defined?(session) always seems to yield true during a mail render (is this a bug?), but attempting to access "session" in any way yields an "undefined method" exception.

As of now, I'm having my mailer use a @for_mailer instance variable to signal this partial to render differently, but this doesn't seem very elegant. Is there some simple way for the partial to figure out whether or not it's being rendered by a mailer, as opposed to being rendered in the context of a web request?

like image 742
jmcnevin Avatar asked Apr 05 '11 17:04

jmcnevin


1 Answers

I would also create two partials for this but here is an alternative solution as well.

Assuming that it is coming from a different controller and action, you could check the params[:controller] and params[:action].

If you end up doing this more than a few times, you will probably end up with more code than just rewriting the partial. What do you want to be different between the two presentations?

like image 115
ezis Avatar answered Oct 21 '22 03:10

ezis