Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying Error Message with Sinatra

I'm writing a simple app that takes standard input from the user. As for the email entry, I have it verify if it is in a standard email format and then have it list the problems like this when a new instance is going to be saved:

u = User.new
u.email = params[:email]
u.save
if u.save
  redirect '/'
else
  u.errors.each do |e|
    puts e
  end
end

I know that if it is correct it should return back to the home page. If it is wrong I want it to return to the home page as well, but I want it to return an error value (so I can have a pop-up or just something onscreen letting the user know that the format of the email was wrong). What would be the best way to do this?

like image 313
captDaylight Avatar asked Aug 21 '11 05:08

captDaylight


People also ask

How do you show error messages?

In order to display error messages on forms, you need to consider the following four basic rules: The error message needs to be short and meaningful. The placement of the message needs to be associated with the field. The message style needs to be separated from the style of the field labels and instructions.

What does the error message indicate?

An error message is information displayed when an unforeseen problem occurs, usually on a computer or other device. On modern operating systems with graphical user interfaces, error messages are often displayed using dialog boxes.

What's in a good error message?

A good error message has three parts: problem identification, cause details if helpful, and a solution if possible. Whenever an error occurs, the user wants to fix it as soon as possible. The error message should have enough information for the user that guides him on how to get out of the erroneous situation.


2 Answers

You can use the 'sinatra-flash' gem to display all kinds of errors/notices etc.

u = User.new
u.email = params[:email]
u.save
if u.save
  redirect '/'
else
  flash[:error] = "Format of the email was wrong."
  redirect '/'
end

Then you need to say where you want the flash[:error] to be displayed. Normally I put this in the layout.haml or (erb) file right above where I yield in the content.

layout.haml:

- if flash[:error]
  %p
    = flash[:error]

Also, make sure you include the gem and enable sessions

require 'sinatra'
require 'sinatra/flash'

enable :sessions

You could also try the 'rack-flash' gem. There is a tutorial for using it at http://ididitmyway.heroku.com/past/2011/3/15/rack_flash_/

like image 189
HeroicEric Avatar answered Oct 23 '22 23:10

HeroicEric


You can save a potentially costly trip back and forth by doing it in Javascript. The way I see it, simple validation like this is a client function, handled by some code attached to an onBlur event, not something I need to verify on my side (except for sanitization, obviously).

To directly answer your question, I've used regular instance variables to store an "error array" in @errors. Form-specific errors, or errors that need to be displayed in a certain place on the page, rather than at the top, get stored in @form_errors or something similar. Then the template checks to see if there are errors and renders them accordingly.

like image 37
wersimmon Avatar answered Oct 23 '22 23:10

wersimmon