Embedding ruby code in a [flash :notice]

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

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 :slight_smile:

#{@in_out.time_in}

rather than

<%= @in_out.time_in %>

regards

Justin

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

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

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

I think you already know this :slight_smile:
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

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

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

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


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