jwharrow of Joe Public
2/7/2017 - 7:28 PM

Users_controllers

Users_controllers

#profile view
get '/users/:user_id' do
  @user = User.find_by (id: params[:user_id])
  if session[:user_id] == @user.id
#    @SOMETHING = USER'S STUFF
    erb :'/users/show'
  else
    erb :'404'
  end
end
#log in form
get '/sessions/new' do
  erb :'sessions/new'
end

# log in submit
# requires authenticate user model method
# modify 14&15 if using _errors?
post '/sessions' do
  if user = User.authenticate(params[:account][:email], params[:account][:password])
    session[:user_id] = user.id
    redirect("/users/#{user.id}")
  else
    @errors = ['Email or password incorrect. Please try again.']
    erb :'sessions/new'
  end  
end

#log out
delete '/sessions' do
  session[:user_id] = nil
  redirect('/')
end

# log out for testing
# don't leave in production or restful purists will come after you
get '/logout' do
  session[:user_id] = nil
  redirect('/')
end
#sign up  
get '/users/new' do
  erb :'users/new'
end

# create account
# modify @errors if using _errors?
# consider set_user model method
post '/users' do
  @user = User.new(params[:account])
  if @user.save
    session[:user_id] = @user.id
    redirect("/users/#{@user.id}")
    # or redirect "/"
  else
    @errors = @user.errors.full_messages
    erb :'users/new'
  end  
end