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

  • Home
  • SEARCH
  • 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 6120225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:38:39+00:00 2026-05-23T15:38:39+00:00

I have a very basic form, with some very basic validations (though I need

  • 0

I have a very basic form, with some very basic validations (though I need to create a custom validation later… you’ll probably see a question on that tomorrow. =P ), but I’m having trouble showing the user the validation errors.

Here’s my main Sinatra file:

$LOAD_PATH.unshift(File.dirname(__FILE__))
require 'sinatra'
require 'rubygems'
require 'datamapper'
require 'dm-core'
require 'dm-validations'
require 'lib/authorization'

DataMapper::setup(:default, "sqlite3://#{Dir.pwd}/entries.db")

class Entry
include DataMapper::Resource

property :id,           Serial
property :first_name,   String, :required => true
property :last_name,    String, :required => true
property :email,        String, :required => true, :unique => true,
                        :format => :email_address, :messages => { 
                            :presence => "You have to   enter your email address", 
                            :is_unique => "You've     already entered", 
                            :format => "That isn't a     valid email address" }
property :created_at,   DateTime    

end
configure :development do
# create, upgrade, or migrate tables automatically
DataMapper.auto_upgrade!
end

helpers do
include Sinatra::Authorization
end

# Set UTF-8 for outgoing
before do
headers "Content-Type" => "text/html; charset=utf-8"
end


get '/' do
@title = "Enter to win a rad Timbuk2 bag!"
erb :welcome
end

post '/' do
@entry = Entry.new(:first_name => params[:first_name], :last_name => params[:last_name], :email => params[:email])

if @entry.save
    redirect("/thanks")
else
    @entry.errors.each do |e|
        puts e
    end
    redirect('/')
end
end


get '/list' do
require_admin
@title = "List of Entries"
@entries = Entry.all(:order => [:created_at.desc])
erb :list
end

get '/thanks' do
erb :thanks
end

get '/delete/:id' do
require_admin
entry = Entry.get(params[:id])
unless entry.nil?
    entry.destroy
end
redirect('/list')
end

So, if a user tries to submit an entry with only a first name, or only a last name, or nothing at all, I can see the errors in my console, but I can’t figure out how to get them in the page displayed by my main handler.

I’ve tried adding a div:

<% unless @entry.errors.empty? %>
<div id="errors">
    <% @entry.errors.each do |e| %>
       <p><%= e %></p>
    <% end %>
</div>
<% end %>

but I get: undefined method `errors’ for nil:NilClass

  • 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-23T15:38:40+00:00Added an answer on May 23, 2026 at 3:38 pm

    What happens in your case is that the redirect will clear the errors again internally. You need to store them temporarily to have them available after the redirect. From Sinatra documentation on how to pass data on through redirects:

    Or use a session:
    
      enable :session
    
      get '/foo' do
        session[:secret] = 'foo'
        redirect to('/bar')
      end
    
      get '/bar' do
        session[:secret]
      end
    

    So in your case this would be

    get '/' do
        @title = "Enter to win a rad Timbuk2 bag!"
        @errors = session[:errors]
        erb :welcome
    end
    

    and

    if @entry.save
        redirect("/thanks")
    else
        session[:errors] = @entry.errors.values.map{|e| e.to_s}
        redirect('/')
    end
    

    for your Sinatra file.

    Your erb file would become

    <% if @errors %>
    <div id="errors">
        <% @errors.each do |e| %>
           <p><%= e %></p>
        <% end %>
    </div>
    <% end %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In it's very basic form I have a WebClient request for some xml in
I realize that this is probably a very basic question, but I have spent
i have very simple problem. I need to create model, that represent element of
I have a very basic rails app. I am playing around with validation. Controller
I have a form, very basic, and when I hit enter key nothing happens
i have very simple basic form in HTML and i want to validate the
i was wondering this is a very basic and probably stupid question. i am
I have a very basic question. I want to retrieve a zip code automatically
I have a very basic understanding of shell scripting, but what I need to
I recently upgraded VS 2005 to VS 2008. Unfortunately I have a very basic

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.