Forum: Nitro Error Handling and Reloading

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
5f65ee21e577b094753cd7d854dc39e4?d=identicon&s=25 Matthew B Gardner (Guest)
on 2007-12-16 10:02
(Received via mailing list)
Hello --
  I've been googling and looking through the rdocs for some time now,
and I
can't find Nitro's answer to this issue: If someone enters an empty
field on
Form, reload the page without losing the entered data from the other
fields
and print the appropriate error message. As far as the reloading
(without
losing the entered data) goes, I'm really not sure how to do that.
However, I
found a couple references to error handling, but it's not working as I
expect
it to from those examples. To keep it simple, here's what I'm trying to
do:

template...
<form>
  ...
  #{flash[:err]}
  <input type="text" name="body" size="45" />
  ...
</form>

controller...
def form_foo
  obj = request.assign(TestObj.new)
  if obj.body,empty?
    flash[:err] = "No blank entries!"
    # reload the page with the entered data...
  ...
end

I've tested the error handling a couple different ways, and I can't get
it to
print. Hopefully my question is clear enough, but please let me know if
it's
not.

Thanks for any help,
Matt
Bd10252dc68b39171cc9bf5f74bcb9f1?d=identicon&s=25 Arne Brasseur (Guest)
on 2007-12-16 10:50
(Received via mailing list)
Matthew B Gardner schreef:
> <form>
>     flash[:err] = "No blank entries!"
>     # reload the page with the entered data...
>   ...
> end
>
> I've tested the error handling a couple different ways, and I can't get it to
> print. Hopefully my question is clear enough, but please let me know if it's
> not.
>
> Thanks for any help,
> Mat
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)

--
Ein Fuchs muß tun, was ein Fuchs tun muß
arne@arnebrasseur.net
Bd10252dc68b39171cc9bf5f74bcb9f1?d=identicon&s=25 Arne Brasseur (Guest)
on 2007-12-16 10:52
(Received via mailing list)
Arne Brasseur schreef:
> def handle_form
>   if (something_not_ok)
>     flash_error "something's not ok"
>     flash[:form_data] = %w(text bla bar).map {|w| request[w]}
>
That  should be

flash[:form_data] ||= {}
%w(text bla bar).map {|w| flash[:form_data][w] = request[w]}

(ab)

--
Ein Fuchs muß tun, was ein Fuchs tun muß
arne@arnebrasseur.net
5f65ee21e577b094753cd7d854dc39e4?d=identicon&s=25 Matthew B Gardner (Guest)
on 2007-12-17 00:39
(Received via mailing list)
Hello --

>
>
>   ...
> 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
    else
  ...
end

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

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
incorrect?

Thanks for all the help thus far,
-Matt
Bd10252dc68b39171cc9bf5f74bcb9f1?d=identicon&s=25 Arne Brasseur (Guest)
on 2007-12-17 06:18
(Received via mailing list)
Matthew B Gardner schreef:
>     else
> error on the part of Nitro or my code -- does anything stand out as
> incorrect?
>
> Thanks for all the help thus far,
> -Mat
This looks good to me, but let me guess... I had the same problem using
Firefox and a url http://localhost:9000, when I changed the URL to
http://127.0.0.1:9000 it worked. This could be the same problem. For
some reason in the first case the cookie isn't sent back from the
browser. I tried Konqueror and there both URL's worked. Could that be
it?

(ab)

--
Ein Fuchs muß tun, was ein Fuchs tun muß
arne@arnebrasseur.net
5f65ee21e577b094753cd7d854dc39e4?d=identicon&s=25 Matthew B Gardner (Guest)
on 2007-12-17 06:49
(Received via mailing list)
Hello --

> This looks good to me, but let me guess... I had the same problem using
> Firefox and a url http://localhost:9000, when I changed the URL to
> http://127.0.0.1:9000 it worked. This could be the same problem. For
> some reason in the first case the cookie isn't sent back from the
> browser. I tried Konqueror and there both URL's worked. Could that be it?
>
> (ab)

Excellent guess, that was it! :)

Thank you very much, again!
-Matt
7a4e995e378ef66de0ceaea5e1381ee1?d=identicon&s=25 George Moschovitis (Guest)
on 2007-12-17 19:41
(Received via mailing list)
>
> > This looks good to me, but let me guess... I had the same problem using
> > Firefox and a url http://localhost:9000, when I changed the URL to
> > http://127.0.0.1:9000 it worked. This could be the same problem. For
> > some reason in the first case the cookie isn't sent back from the
> > browser. I tried Konqueror and there both URL's worked. Could that be
> it?
>

Does anyone have any idea WHY this happens?

-g.
Bd10252dc68b39171cc9bf5f74bcb9f1?d=identicon&s=25 Arne Brasseur (Guest)
on 2007-12-18 08:00
(Received via mailing list)
George Moschovitis schreef:
>
> Does anyone have any idea WHY this happens?

Connection: close
Date: Tue, 18 Dec 2007 06:55:44 GMT
Content-Type: text/html
Set-Cookie:
ns=ef57fd92773b11f13f3fea31d98d76320dc1165b8bf37f1e394392953a101ca02bbfe7789290a2f6bb99b0f712045a4b125b6f39de2dba3fc1a2c4e24c1a9f51;
Domain=.localhost; Path=/
Content-Length: 1427

200 OK



Could that dot in Domain=.localhost; be the problem? Just guessing
really.

(ab)
This topic is locked and can not be replied to.