I’m using Ruby 2.3 with Rails 5. If someone visits the login page in my application and they are already signed in, I want to redirect them to their user home page. However, following this link — Redirect user after log in only if it's on root_path, I can’t get it to work. I added these to my config/routes.rb file
root :to => "users/", :constraints => {user_signed_in?}
root :to => redirect('/login')
And I created the file lib/authenticated_user.rb with this content
class AuthenticatedUser
def self.matches?(request)
user_signed_in?
end
end
However, when I visit my root page, I get this error
/Users/nataliab/Documents/workspace/sims/config/routes.rb:17: syntax error, unexpected '}', expecting =>
root :to => "users/", :constraints => {user_signed_in?}
What else do I need to do to get this working?
Edit: This is my complete config/routes.rb file, edited after the answer given
Rails.application.routes.draw do
get '/signup', to: 'users#new'
get '/forgot_password', to: 'users#forgot_password'
get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy'
get '/dashboard' => 'users#show', as: :dashboard
resources :users
resources :scenarios do
get :download
resources :confidential_memos
end
resources :scenario_files, :only => %i[ show ]
root :to => "/dashboard", constraints: lambda { |user| user.user_signed_in? }
root :to => redirect('/login')
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
I have this defined in app/helpers/sessions_helper.rb
def current_user
@current_user ||= User.find_by(id: session[:user_id])
end
# Returns true if the user is logged in, false otherwise.
def logged_in?
!current_user.nil?
end