开发者

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

开发者 https://www.devze.com 2023-02-22 11:45 出处:网络
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.

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 t开发者_高级运维he 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?


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号