Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6856441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:53:12+00:00 2026-05-27T01:53:12+00:00

To manage errors in my address book app i initialize an array like this

  • 0

To manage errors in my address book app i initialize an array like this

err = Array.new

and then when i post something it checks if there are empty fields. If yes, for each empty field it adds a record in the array, and then redirect to /add page, like this

post '/' do
if params[:fname] == ""
    err.push "Insert a valid first name"
end
if params[:lname] == ""
    err.push "insert a valid last name"
end
if params[:phone] == ""
    err.push "insert a valid phone number"
end
if params[:mail] == ""
    err.push "insert a valid e-mail address"
end
if err.empty?
    c = Contatto.new
    c.fname = params[:fname]
    c.lname = params[:lname]
    c.phone = params[:phone]
    c.mail = params[:mail]
    c.save
    redirect '/'
else
    redirect '/add'
end
end

then the add page reads if the array has any record and if yes, cycles it to print each message

get '/add' do
  @err = err
  @title = 'Aggiungi'
  erb :aggiungi
end



<% if @err.any? %>
<div class="error">
    <% @err.each do |err| %>
        <%= err %><br>
    <% end %>
</div>
<% end %>

i think the error is that it re-initialize the array every time it changes from post ‘/’ to get ‘/add’ and so the result is an empty array…
How can i solve? thank you everyone!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:53:12+00:00Added an answer on May 27, 2026 at 1:53 am

    If you want data for a specific visitor to persist between requests you need to be storing the error array in either a session or a cookie (session probably makes the most sense).

    Luckily sessions in Sinatra are pretty easy: http://www.sinatrarb.com/intro#Using%20Sessions . Once enabled you can put pretty much anything you want into the session hash, so initializing with session[:errors] = [] and pushing with session[:errors] << "An error" should give you the persistence you are looking for.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to do something like this: if params[:q] loc = Geocoder.search(params[:q])[0] logger.info
I'm doing this project to manage a bank and I'm trying to implement code
In my program on this line: int value = MTEConnect(auth_string, err); I receive such
I have WCF service hosted by Windows service. This is my app.config file (server-side)
D:\zjm_code\basic_project>python manage.py syncdb Error: One or more models did not validate: topics.topic: Accessor for
I'm getting the following error when I run ./manage.py reset app1 : Error: Error:
I'm very infrequently seeing the following error logged on a website I manage: Arguments
I'm using YUI's browser history manager, and my browsers no longer report runtime errors.
I manage a small group and I'd keep my work breakdown in project. However,
I manage a web application for a client with the following specs: ASP.net 3.5

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.