Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Homepage (Root path)
- get '/' do
- @users = User.all # grab all the users from users table, use instance variabe (@) so we can use the data in the erb template
- erb :index
- end
- get '/blog' do
- erb :blog
- end
- # show the "register" template which has the form
- get '/register' do
- erb :register
- end
- # we hit this action when the form is submitted (remember the form used 'POST' method and '/register' action
- # so we would hit this particular route with the form data. The form data can be accessed through 'params' hash.
- post '/register' do
- first_name = params[:first_name]
- email = params[:email]
- password = params[:password]
- colour = params[:colour]
- age = params[:age]
- new_user = User.new(
- first_name: first_name,
- email: email,
- password: password,
- colour: colour,
- age: age
- )
- if new_user.save
- # if a new user row was created in the db then redirect back to home page.
- redirect '/'
- else
- # if we couldn't create a new row (user) in the db then refresh the same page
- # so user can re-enter the data and submit
- erb :register # show the register view with form in it
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement