How to set the default unauthenticated path/model with multiple devise models? - ruby-on-rails

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

Related

Ruby on Rails Separate admin folder, controllers and layouts

I am new to Ruby on Rails. I want to have following structure for admin section.
app/controller/admin/admin_controller.rb and all other admin section controller under app/controller/admin/ folder
app/views/layout/admin/admin.html.erb to keep separate html layout for admin section
At the same time i want to use Devise Gem for admin and front end user authentication.
I executed rails g devise:views admin, rails generate devise Admin and rails g controller admin/home index command that created views, model and controller for admin user. Now what routes and other setting i need to add so that ruby could understand that if i type http://localhost:3000/admin/ then i should be redirected to http://localhost:3000/admins/sign_in/ page and after entering correct admin credentials i should redirected to index method of controllers/admin/home_controller.rb
is it also possible to keep singular convention of Devise admin views like admin/sign_in instead of admins/sign_in ?
I have searched a lot but could not get relevant help. Please provide steps to achieve above.
Thanks in advance.
This is how route file looks like
Rails.application.routes.draw do
namespace :admin do
get 'home/index'
end
devise_for :admins
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
root to: "home#index"
end
When i type http://localhost:3000/admin/ then i get below error
Your problem is that you do not have root route defined for /admin.
I have the same URL convention routes in one of the apps and routes.rb looks like this:
Rails.application.routes.draw do
# Admin part
devise_for :admins, path: '/admin'
scope module: :admin, path: '/admin', as: 'admin' do
root to: 'home#index'
end
# Redirect app root to client part
root to: redirect(path: '/panel', status: 301)
# Client part
devise_for :clients, path: '/panel'
scope module: :panel, path: '/panel', as: 'panel' do
...
end
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

How to route sign_up in Rails 3 using Devise

I am using Devise for authenticating a Rails application. I am now able to successfully route /users/sign_in and /users/sign_out to /sign_in and /sign_out via this code in routes.rb:
devise_for :user, :as => ''
How do I map /registration/sign_up to /sign_up?
So that sign_in, sign_out, and sign_up all have the same pattern.
Note that I am using Devise only for users. No admins.
You need to add the following block to your routes.rb file:
devise_scope :user do
get "/sign_up" => "devise/registrations#new"
end
It's explained in: http://github.com/plataformatec/devise/wiki/How-To:-Change-the-default-sign_in-and-sign_out-routes

Resources