Skip to content
Browse files

refactoring validation into a subtemplate

  • Loading branch information...
1 parent 4e7e5f6 commit 63a7a9532e0914692a8f727e4910edcb92d5dfc0 @aaronpowell committed Feb 18, 2011
Showing with 20 additions and 11 deletions.
  1. +13 −6 site.rb
  2. +2 −0 views/login.haml
  3. +1 −5 views/register.haml
  4. +4 −0 views/validation.haml
View
19 site.rb
@@ -12,6 +12,14 @@
helpers do
+ def display_errors(errors)
+ if errors.length > 0
+ haml :validation, :layout => false, :locals => { :errors => errors }
+ else
+ ''
+ end
+ end
+
def h(source)
escape_html(source).gsub(' ', '%20')
end
@@ -34,10 +42,10 @@ def login(username, password)
session[:username] = username
session[:api_key] = u['_id']
session[:authenticated] = true
- true
+ return true
end
end
- u
+ false
end
def logout
@@ -64,7 +72,7 @@ def encrypt_password(password)
if authorized?
redirect '/'
end
- haml :register
+ haml :register, :locals => { errors: [] }
end
post '/register' do
@@ -114,16 +122,15 @@ def encrypt_password(password)
if authorized?
redirect '/'
end
- haml :login
+ haml :login, :locals => { errors: [] }
end
post '/login' do
login(params[:username], params[:password])
if authorized?
redirect '/'
else
- @error = 'Password incorrect'
- haml :login
+ haml :login, :locals => { errors: ['Username and password combination is not valid'] }
end
end
View
2 views/login.haml
@@ -1,5 +1,7 @@
%form{:action => '/login', :method => 'POST'}
%fieldset.form-body
+ %legend= display_errors(errors)
+
.editor-label
%label{:for => 'username'} Username:
.editor-field
View
6 views/register.haml
@@ -1,10 +1,6 @@
%form{:action => '/register', :method => 'POST'}
%fieldset.form-body
- - if errors.length
- %dl.validation-message
- %dt.heading Please correct the following errors:
- - errors.each do |s|
- %dt= s
+ %legend= display_errors(errors)
.editor-label
%label{:for => 'username'} Username:
View
4 views/validation.haml
@@ -0,0 +1,4 @@
+%dl.validation-message
+ %dt.heading Please correct the following errors:
+ - errors.each do |s|
+ %dt= s

0 comments on commit 63a7a95

Please sign in to comment.
Something went wrong with that request. Please try again.