Error Handling and Reloading


#1

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…

... #{flash[:err]} ...

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


#2

Matthew B Gardner schreef:

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

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


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ß
removed_email_address@domain.invalid


#3

Arne B. 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ß
removed_email_address@domain.invalid


#4

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ß
removed_email_address@domain.invalid


#5

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


#6

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

Thank you very much, again!
-Matt


#7

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.


#8

George M. 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)