# Embedding ruby code in a \[flash :notice\]

**URL:** <https://www.ruby-forum.com/t/embedding-ruby-code-in-a-flash-notice/96217>\
**Category:** Rails\
**Created:** [June 27, 2006, 1:01pm UTC](https://www.ruby-forum.com/t/embedding-ruby-code-in-a-flash-notice/96217 "2006-06-27T13:01:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [June 27, 2006, 1:01pm UTC](https://www.ruby-forum.com/t/embedding-ruby-code-in-a-flash-notice/96217/1 "2006-06-27T13:01:06Z")

</div>

The ruby code in this isn’t evaluated…

flash[:notice] = “Your last recorded entry was \<%= @in\_out.time\_in %\>  
  
You are currently marked as ‘In’, you probably want to check  
‘Out’”

Is there a way?

Craig

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [June 27, 2006, 1:26pm UTC](https://www.ruby-forum.com/t/embedding-ruby-code-in-a-flash-notice/96217/2 "2006-06-27T13:26:24Z")

</div>

Craig W. wrote:

> The ruby code in this isn’t evaluated…
> 
> flash[:notice] = “Your last recorded entry was \<%= @in\_out.time\_in %\>  
>   
> You are currently marked as ‘In’, you probably want to check  
> ‘Out’”
> 
> Is there a way?

I think you already know this 🙂

#{@in\_out.time\_in}

rather than

\<%= @in\_out.time\_in %\>

regards

Justin

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [June 30, 2006, 9:15pm UTC](https://www.ruby-forum.com/t/embedding-ruby-code-in-a-flash-notice/96217/3 "2006-06-30T21:15:42Z")

</div>

On Tue, 2006-06-27 at 12:25 +0100, Justin F. wrote:

> #{@in\_out.time\_in}
> 
> rather than
> 
> \<%= @in\_out.time\_in %\>

* * *

I have read AWDWR several times, Ruby4Rails once thoroughly (too little  
of that info has stuck with me unfortunately), Chad’s Recipes, Cody’s  
RJS Templates for Rails and perhaps I knew it somehow somewhere but  
postgres is far more reliable for data retrieval than my brain.

Thanks - I got it now…

Craig

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [July 1, 2006, 3:17am UTC](https://www.ruby-forum.com/t/embedding-ruby-code-in-a-flash-notice/96217/4 "2006-07-01T03:17:58Z")

</div>

Craig W. wrote:

> > #{@in\_out.time\_in}
> > 
> > rather than
> > 
> > \<%= @in\_out.time\_in %\>
> 
> * * *
> 
> I have read AWDWR several times, Ruby4Rails once thoroughly (too little  
> of that info has stuck with me unfortunately), Chad’s Recipes, Cody’s  
> RJS Templates for Rails and perhaps I knew it somehow somewhere but  
> postgres is far more reliable for data retrieval than my brain.

Well, if you are like me you will only remember a lot of facts if you  
know the underlying principles that tie them together.

There are two key factors here:

1. The flash is populated on one HTTP request, usually for display on  
the next (after a redirect). Your @in\_out instance variable contains the  
value that you want to insert into the message on the first HTTP  
request, when you are adding the string to the flash, but won’t be there  
after the redirect, when the page containing the message is rendered.

2. When the page is rendered, the message held in the flash is inserted  
into the page content by ERB - but the message itself isn’t treated as  
an ERB template. So ERB tags like \<%= x %\> in the flash message won’t be  
evaulated.

Either of these factors would be enough to stop your code from doing  
what you wanted.

Using #{x} in the double-quoted string causes the value to be inserted  
into the message before the string is placed in the flash.

Hope that helps

Justin

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [July 1, 2006, 3:54am UTC](https://www.ruby-forum.com/t/embedding-ruby-code-in-a-flash-notice/96217/5 "2006-07-01T03:54:44Z")

</div>

On Sat, 2006-07-01 at 02:15 +0100, Justin F. wrote:

> > > I think you already know this 🙂  
> > > postgres is far more reliable for data retrieval than my brain.  
> > > after the redirect, when the page containing the message is rendered.  
> > > into the message before the string is placed in the flash.
> 
> Hope that helps

* * *

it was terrific help but a couple of clarifications…I couldn’t use the  
item within the double quoted strings…it was blank

thus I ended up breaking the strings and the instance variable and  
combining them again, this worked…

```
    flash[:notice] = "You are trying to record an entry for" +
      @in_out.time_out.strftime("%m/%d/%Y @ %H:%M") +
      " <br /> which is earlier than your last 'Time In' entry shown

```

below.  
  
 It is assumed that this is not what you intended - try  
again"

Some of the ruby usage comes really difficult for me - probably because  
I am not a programmer but I am doing the best impersonation I can  
muster.

Thanks

Craig

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [July 1, 2006, 1:04pm UTC](https://www.ruby-forum.com/t/embedding-ruby-code-in-a-flash-notice/96217/6 "2006-07-01T13:04:56Z")

</div>

Craig W. wrote:

> > > > > Is there a way?  
> > > > > RJS Templates for Rails and perhaps I knew it somehow somewhere but  
> > > > > after the redirect, when the page containing the message is rendered.  
> > > > > into the message before the string is placed in the flash.  
> > > > > @in\_out.time\_out.strftime(“%m/%d/%Y @ %H:%M”) +  
> > > > > "   
> > > > > which is earlier than your last ‘Time In’ entry shown  
> > > > > below.  
> > > > >   
> > > > > It is assumed that this is not what you intended - try  
> > > > > again"

Good to see that you have it working. I’m puzzled by interpolation  
(using #{…}) not working for you - I tried a trivial example,  
interpolating Time.now into a flash message, and it worked for me.

There’s another option, which would make your code a bit cleaner - write  
your message text as a format string, and use the % operator to weave  
the value into it. This lets you declare the message format string as a  
constant, up at the top of your controller file, removing all that  
clutter from the code that uses it. Like this:

ERR\_OUT\_BEFORE\_IN = "You are trying to record an entry for %s " +  
“  
 which is earlier than your last ‘Time In’ entry…”

…

```
 time_out = @in_out.time_out.strftime("%m/%d/%Y @ %H:%M")
 flash[:notice] = ERR_OUT_BEFORE_IN % time_out

```

You can read about format strings here:  
[http://corelib.rubyonrails.org/classes/Kernel.html#M002081](http://corelib.rubyonrails.org/classes/Kernel.html#M002081)

You might be using strftime(“%m/%d/%Y @ %H:%M”) at many points in your  
code. If you decided to change the format, you would have to change all  
those places. If you made a helper method, and used it consistently,  
then the format would only be defined in one place.

To have a helper method that can be used in all controllers and views,  
define it in application.rb and declare it as a helper\_method, e.g.:

class ApplicationController \< ActionController::Base

helper\_method :format\_date\_and\_time

def format\_date\_and\_time(time)  
time.strftime(“%m/%d/%Y @ %H:%M”)  
end

end

(If you defined the helper in application\_helper.rb, it would be  
available to all views. If you defined it in application.rb, it would be  
inherited by all controllers. Adding the helper\_method declaration also  
makes it available to all views.)

regards

Justin

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [July 1, 2006, 5:26pm UTC](https://www.ruby-forum.com/t/embedding-ruby-code-in-a-flash-notice/96217/7 "2006-07-01T17:26:36Z")

</div>

On Sat, 2006-07-01 at 12:02 +0100, Justin F. wrote:

> Craig W. wrote:

> > ```
> > flash[:notice] = "You are trying to record an entry for" + 
> > @in_out.time_out.strftime("%m/%d/%Y @ %H:%M") + 
> > " <br /> which is earlier than your last 'Time In' entry shown
> > 
> > ```
> > 
> > below.  
> >   
> > It is assumed that this is not what you intended - try  
> > again"

* * *

## Thanks so much Justin for hanging in there with me as this is extremely valuable information that you have imparted upon me

> Good to see that you have it working. I’m puzzled by interpolation  
> (using #{…}) not working for you - I tried a trivial example,  
> interpolating Time.now into a flash message, and it worked for me.

* * *

## It didn’t error for me but simply left a blank but perhaps that was because I hadn’t fully ‘stringified’ it - I’m gonna go back to it with your excellent suggestion below.

> ```
> time_out = @in_out.time_out.strftime("%m/%d/%Y @ %H:%M")
> flash[:notice] = ERR_OUT_BEFORE_IN % time_out
> 
> ```
> 
> You can read about format strings here:  
> [http://corelib.rubyonrails.org/classes/Kernel.html#M002081](http://corelib.rubyonrails.org/classes/Kernel.html#M002081)

* * *

## I love this - this will improve a lot of things and dry up code

> helper\_method :format\_date\_and\_time  
> makes it available to all views.)

* * *

this is one of those things that I keep meaning to get a round tuit - my  
previous coding, I ended up going back and drying up the code after I  
got the ‘module’ completely finished which is indicative that I don’t  
conceptualize the dry methods very well and yes, I very much should be  
doing this. Despite reading and re-reading AWDWR and Ruby4Rails books,  
these concepts have come slowly and sometimes not at all but it’s funny  
how when you wrote the above, the light bulb went off. I have managed to  
add a few formatting shortcuts previously in application.rb but I can  
see that there is more work to do and finally - now can actually  
articulate where and why.

Thanks again

Craig
