Rails routing using devise and subdomain constraint - ruby-on-rails

I am trying to route root to devise sign in path under a subdomain constraint.
My config/routes.rb look something like this
Rails.application.routes.draw
constraints subdomain: 'admin' do
devise_scope :admin do
root to: 'devise/sessions#new'
# here I override devise routes
end
end
root to: 'pages#homepage'
# rest of the routes
end
I am getting the error Could not find devise mapping for path "/".
Any suggestions as to how I route to root path in a subdomain with devise scope?
Thanks

To add an authentication constrain to a route use the devise authenticated method:
Rails.application.routes.draw
constraints subdomain: 'admin' do
authenticated :admin do
# the root page for authenticated users
root 'admin#dashboard', as: :authenticated_root
end
root to: 'devise/sessions#new'
end
# this is for no subdomain
root to: 'pages#homepage'
end

Related

How to use multiple root route in rails 6?

Just updated to rails 6 and having trouble with a conditional root route
devise_scope :user do
authenticated do
root to: 'users#show'
end
unauthenticated do
root to: 'visitors#index'
end
end
I've attempted to check for the logged-in user in visitors#index and do a redirect to users#show but now I have this ugly URL '/users/:id' instead of being able to visit users#show with a clean root URL.
In my case I was able to solve this by adding an :as argument:
devise_scope :user do
authenticated do
root to: 'users#show'
end
unauthenticated do
root to: 'visitors#index', as: :visitors_url
end
end
I think the reasoning behind the change is to make it so that Rails knows where to route root_url.
Note: I found the answer on reddit where it was suggested using namespace would be cleaner.
namespace :visitors, path: nil do
root to: 'visitors#index'
end
I don't know if there is a way to make that work with devise though.

How to set the default unauthenticated path/model with multiple devise models?

I have a rails 5.2 application, using devise 4.4.3, which has two devise models: User and Parent.
The paths are defined the config/routes.rb as:
devise_for :users, path: 'users'
devise_for :parents, path: 'parents'
Currently when I visit an action that required authentication in an unauthenticated session I'm getting redirected to a /parents/sign_in, but I want to specify /users/sign_in as the default unauthenticated path.
How can I achieve this?
Add an unauthenticated call and define the default root route:
unauthenticated do
root to: "users#sign_in"
end

Alias for Devise URL Rails

I am new to Ruby on Rails,I am using Devise gem for authentication on table account_user.
When I do rake routes I get
new_account_user_session GET /account_users/sign_in(.:format)account_user/sessions#new
So my login page is xyz.com/account_users/sign_in.
I want to change the sign-in page to just xyz.com
I don't have any routes for the same in my routes.rb file, I thought devise is automatically generating routes for this.
Is there a way I can add alias/override for this devise generated routes, or redirect user to xyz.com instead of xyz.com/account_users/sign_in
set root to devise sign_in, so in your route file there should be
devise_for :account_users
devise_scope :account_user do
root to: 'devise/sessions#new'
end
this will set your root path to sign_in
or if you want to rename the route to 'login'
devise_for :account_users
devise_scope :account_user do
get 'login', to: 'devise/sessions#new'
end
more here https://github.com/plataformatec/devise/wiki/How-To:-Change-the-default-sign_in-and-sign_out-routes

Ignoring the devise routes does not work

I am trying to write an API for app's sign-in/sign-up custom procedures but want to use the devise's features through my app. The problem is without defining the devise_for :users or devise_scope :user in config/routes.rb the devise's map does not get created and so I cannot use sign_in #user and etc.
Question:
How can I use the devise's features without its routes enabled?
I figured it out
# config/routes.rb
Rails.application.routes.draw do
# root controller#action
root 'home#index'
# define the devise's user and
# disable direct access to devise's routes
devise_for :users, skip: :all
end

`add_route': Invalid route name, already in use: 'root' (ArgumentError)

Im using rails 4.1.1 and ruby 2.1.1 and am having an issue with devise, namely my routes..I have used this many times before
devise_for :users
get 'pages/index'
# Route to Devise Login Page
devise_scope :user do
root to: "devise/sessions#new"
end
# Directing the user after login
authenticated :user do
root :to => 'pages#index'
end
But i get the error
`add_route': Invalid route name, already in use: 'root' (ArgumentError)
when trying to start the server.. I can see that root is being used twice, but like i said i have been able to do this in the past.. Is there a way around this
Thanks
Found this helpful comment here on stackoverflow
For Rails 4.0 you have to make sure you have unique names for the path
helpers, like root to: "dashboard#show", as: :authenticated_root.
Otherwise the authenticated root and the normal root route end up
having the same name for their path helpers, which Rails 4.0 no longer
allows
so I changed my authenticated root to helper like so
# Directing the user after login
authenticated :user do
root :to => 'pages#index', as: :authenticated_root
end

Resources