[Nitro] Error Handling and Reloading

Matthew B Gardner weather at speakeasy.net
Sun Dec 16 18:38:50 EST 2007

Hello --

> If the same action displays and handles the form I'd just store the
> values in instance variables:
> #controller
> def foo
>   @text, @bla, @bar = %w(text bla bar).map {|w| request[w]}
>   ....
> end
> <!--template-->
> <input type="text" name="text" value="#{@text}" size="45" />
> <input type="text" name="bla" value="#{@bla}" size="45" />
> This requires extra logic to render the right template depending on if
> everything processed fine or not.
> If you're using a different action you'll have to redirect back to the
> form if there's an error, but in that case the user's browser makes a
> new request and the data from the previous request is gone, so you'll
> have to store it in the flash.
> def handle_form
>   if (something_not_ok)
>     flash_error "something's not ok"
>     flash[:form_data] = %w(text bla bar).map {|w| request[w]}
>     redirect :show_form #(or redirect_referrer)
>   end
> end
> def show_form
>     flash[:form_data] = NullClass.new #to prevent NoMethodError
>   ...
> end
> <input type="text" name="text" value="#{flash[:form_data]['text']}"
> size="45" /> <input type="text" name="bla"
> value="#{flash[:form_data]['bla']}" size="45" />
> This is all off the top of my head so the syntax might be a bit off.
> There are two different form helpers in Raw and there could very well be
> some functionality in there to do this kind of stuff. I just don't have
> the time right now to look at it in detail.
> (ab)

This isn't working for me because, I think, the flash object is getting 
cleared too early...here's the code:

def handle_hfile
    @h = request.assign(HelpFile.new)
    if @h.body.empty?
      flash_error "Something is amiss!"
      flash[:form_data] ||= {}
      %w(keywords).map {|w| flash[:form_data][w] = request[w]}
      redirect_referer # :helpfile

def helpfile
    puts flash[:form_data] #=> nil
    flash[:form_data] = NullClass.new if flash[:form_data].nil?

When #handle_hfile redirects to #helpfile, flash is nil -- I assumed that 
flash would keep its data until the next request. I don't know if this is an 
error on the part of Nitro or my code -- does anything stand out as 

Thanks for all the help thus far,

More information about the Nitro-general mailing list