Advertisement
Guest User

Untitled

a guest
Nov 17th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. # Homepage (Root path)
  2. get '/' do
  3. @users = User.all # grab all the users from users table, use instance variabe (@) so we can use the data in the erb template
  4. erb :index
  5. end
  6.  
  7. get '/blog' do
  8. erb :blog
  9. end
  10.  
  11. # show the "register" template which has the form
  12. get '/register' do
  13. erb :register
  14. end
  15.  
  16. # we hit this action when the form is submitted (remember the form used 'POST' method and '/register' action
  17. # so we would hit this particular route with the form data. The form data can be accessed through 'params' hash.
  18. post '/register' do
  19. first_name = params[:first_name]
  20. email = params[:email]
  21. password = params[:password]
  22. colour = params[:colour]
  23. age = params[:age]
  24.  
  25. new_user = User.new(
  26. first_name: first_name,
  27. email: email,
  28. password: password,
  29. colour: colour,
  30. age: age
  31. )
  32.  
  33. if new_user.save
  34. # if a new user row was created in the db then redirect back to home page.
  35. redirect '/'
  36. else
  37. # if we couldn't create a new row (user) in the db then refresh the same page
  38. # so user can re-enter the data and submit
  39. erb :register # show the register view with form in it
  40. end
  41. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement