Earnest Claran
Joined
10 Experience
0 Lessons Completed
0 Questions Solved
Activity
Posted in Restrict devise routes
To restrict access to the Devise sign-up route for only logged-in users, you can override the new action in the Users::RegistrationsController. Here's an example of how you can modify the controller to achieve this:
ruby
class Users::RegistrationsController < Devise::RegistrationsController
before_action :authenticate_user!, except: [:new]
def new
if user_signed_in?
redirect_to root_path, alert: "You are already signed in."
else
super
end
end
# ...
end
In this updated code, the before_action is used to authenticate the user for all actions except the new action. Within the new action, you can check if the user is already signed in using user_signed_in?.