# Accessing Flash/Session in Radiant Tags?

**URL:** <https://www.ruby-forum.com/t/accessing-flash-session-in-radiant-tags/128744>\
**Category:** Radiant CMS\
**Created:** [February 11, 2008, 6:53am UTC](https://www.ruby-forum.com/t/accessing-flash-session-in-radiant-tags/128744 "2008-02-11T06:53:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![J\_Aaron\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e79b87/32.png) [@J\_Aaron\_F](https://www.ruby-forum.com/u/J_Aaron_F)\
**Post date:** [February 11, 2008, 6:53am UTC](https://www.ruby-forum.com/t/accessing-flash-session-in-radiant-tags/128744/1 "2008-02-11T06:53:45Z")

</div>

The scenerio is this:

The Comments extension has a controller that handles the comment form  
and then redirects the request back to the originating Page (with the  
comment now saved). The form requires an email address, but at the  
moment, when no email address is supplied, one just gets the classic  
ugly Rails error page (with stack).

The trouble is that because of the redirect, the CommentController  
cannot use the typical Rails validation code to display validation  
error messages. My thought was to try and fix this by stuffing the  
error message into the controller `flash` or the session. But there  
doesn’t appear to be any way to access flash from inside a tag. It’s  
not part of the tag.globals or tag.locals. Fixing this would require  
patching the SiteController.

So my questions are:

1. Is there any other way to access the flash or session data from  
a tag?

2. Is there a better way to send back form validation messages to a  
Page? (\*)

If not, I’ll send a patch to the SiteController to bind the flash to a  
the tag object.

(\*) – The Mailer extension gets around this problem by subclassing  
the Page object, but that seems like a less than ideal approach since  
at the moment, any type of Page can have comments, whereas subclassing  
removes that option.

Thanks!

–  
J Aaron F. [jadetower.com](http://jadetower.com) [US] +1 724-964-4515  
é¦®å‚‘ä» [cubiclemuses.com](http://cubiclemuses.com) [HK] +852 8123-7905

---

<div class="post-metadata">

**Author:** ![J\_Aaron\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e79b87/32.png) [@J\_Aaron\_F](https://www.ruby-forum.com/u/J_Aaron_F)\
**Post date:** [February 11, 2008, 11:06am UTC](https://www.ruby-forum.com/t/accessing-flash-session-in-radiant-tags/128744/2 "2008-02-11T11:06:23Z")

</div>

> 1. Is there any other way to access the flash or session data from  
> a tag?

It can be done, but it requires a bit of patching. You need to put the  
request and response objects in the globals for the tag to access, and  
bear in mind that you need to reenable to sessions for sitecontroller:

module SiteControllerExt  
def self.included(base)  
base.class\_eval {  
# This reenables the session for the SiteController -  
session :on does not work!  
session :disabled =\> false  
}  
end  
end

---

<div class="post-metadata">

**Author:** ![J\_Aaron\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e79b87/32.png) [@J\_Aaron\_F](https://www.ruby-forum.com/u/J_Aaron_F)\
**Post date:** [February 11, 2008, 11:11am UTC](https://www.ruby-forum.com/t/accessing-flash-session-in-radiant-tags/128744/3 "2008-02-11T11:11:40Z")

</div>

Casper F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> ```
> base.class_eval {
> # This reenables the session for the SiteController -  
> 
> ```
> 
> session :on does not work!  
> session :disabled =\> false  
> }  
> end  
> end

Thanks.

I thought about this, but it smelled bad to me, so I wanted to ask  
around first.

–  
J Aaron F. [jadetower.com](http://jadetower.com) [US] +1 724-964-4515  
é¦®å‚‘ä» [cubiclemuses.com](http://cubiclemuses.com) [HK] +852 8123-7905

---

<div class="post-metadata">

**Author:** ![J\_Aaron\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e79b87/32.png) [@J\_Aaron\_F](https://www.ruby-forum.com/u/J_Aaron_F)\
**Post date:** [February 11, 2008, 2:45pm UTC](https://www.ruby-forum.com/t/accessing-flash-session-in-radiant-tags/128744/4 "2008-02-11T14:45:53Z")

</div>

> So my questions are:
> 
> 1. Is there any other way to access the flash or session data from  
> a tag?
> 
> 2. Is there a better way to send back form validation messages to a  
> Page? (\*)

There are two options I can think of here for question #2, and I don’t  
think you need to worry about #1.

First, when validation fails, you could simply render the page as your  
output, using the special Radius tags you use to markup your form to  
output the validation errors.

Second, you could use AJAX to post the comment. Then if it fails, you  
can send back various JS commands that display the validation errors.

A third but potentially less desirable option is not to subclass Page  
like the Mailer extension does, but to modify the Page model directly.  
Alias-chain the process method and then check for your comment post,  
passing on to the original method if it wasn’t a comment.

For Redken, we did add sessions to SiteController; however, I would only  
do it if you are allowing visitor logins. It doesn’t seem worth it to  
enable sessions just for flash messages.

Sean
