How to configure routes for different user models using devise? - ruby-on-rails

I have two different user groups, User and Flyer.
I have generated views and controllers for both the models using,
rails g devise:controllers users/flyers
and for views:
rails g devise:views users/flyers
This is my routes.rb:
Rails.application.routes.draw do
devise_for :flyers
devise_for :admins
resources :currencies
resources :broadcasts
devise_for :users, controllers: {
sessions: 'users/sessions',
registrations: 'flyers/registrations'
}
devise_for :flyers, controllers: {
sessions: 'flyers/sessions',
}
end
But I am getting error for devise for flyers controllers route:
Invalid route name, already in use: 'new_flyer_session' You may have defined two routes with the same name using the `:as` option, or you may be overriding a route already defined by a resource with the same naming. For the latter, you can restrict the routes created with `resources` as explained here: http://guides.rubyonrails.org/routing.html#restricting-the-routes-created
How can I have different routes?
Thanks

Remove devise_for :flyers from top

Related

Rails Devise, how to correctly nest resources

Here's my issue, I want to correctly nest my devise routes from :
user_je_session POST /users/jes/:je_id/sign_in(.:format)
to :
user_je_session POST /jes/:je_id/users/sign_in(.:format)
The objective is that my users can have multiple accounts, depending on the "je" they want to access.
there's my actual routes :
root to: 'pages#home'
devise_for :admins, controllers: { sessions: 'admin/sessions', registrations: 'admin/registrations' }
resources :jes, param: :nom do
devise_for :users, controllers: { sessions: 'users/sessions', registrations: 'users/registrations' }
end
Thanks for your help
I think it's easier to use devise_scope. You can customize the devise routes as you wish. Something like this might be work:
devise_for :admins, controllers: {
sessions: 'admin/sessions',
registrations: 'admin/registrations'
}
as :admin do
# Please change to: value to your actual controller or action
post '/jes/:je_id/users/sign_in', to: 'admin/sessions#create'
end
Please read more on the documentation page.
https://github.com/heartcombo/devise/wiki/How-To:-Change-the-default-sign_in-and-sign_out-routes

Devise scoped member routes issue

I'm using devise for sign up and ActiveStorage for image upload. For the delete/purge function to work I have this route
devise_scope :user do
scope module: :users do
resources :registrations do
member do
delete :delete_image_attachment
end
end
end
end
But another place in my routes file I have this route
devise_for :users, controllers: {:registrations => "users/registrations"
}
It makes some of my pages not working. I have read somewhere that it's because registrations are declared two times. How can I make it work?
Any help would be much appreciated
If you use resources :registrations, only: [] do... that will create the parent route that you need without overwriting any of the routes provided by devise. Allowing you to make your nested routes :D

add your own custom route to devise

i've seen a bunch of posts on how to rename already-declared routes in devise. I want to expand devise to have my own route check for and idle session. I am implementing a simple js check every 1 minute that I want to hit 'check_active' in the devise sessions controller. I tried this but no luck:
devise_scope :sessions do
get 'check_active'
end
Is there way to expand devise with a custom route (not rename an already-existing one) ?
UPDATE - almost there, i did this
# already had this in routes
devise_for :users, :controllers =>
{ registrations: 'registrations',
confirmations: 'confirmations',
sessions: 'sessions',
passwords: 'passwords',
omniauth_callbacks: "omniauth_callbacks"}
# added this
devise_scope :sessions do
get '/check_active' => 'sessions#check_active'
end
I have a js firing, i have it get '/check_active' as rake routes shows this:
check_active GET /check_active(.:format)
But when it fires, the controller 404s with
AbstractController::ActionNotFound (Could not find devise mapping for path "/check_active".
This may happen for two reasons:
1) You forgot to wrap your route inside the scope block. For example:
devise_scope :user do
get "/some/route" => "some_devise_controller"
end
2) You are testing a Devise controller bypassing the router.
If so, you can explicitly tell Devise which mapping to use:
#request.env["devise.mapping"] = Devise.mappings[:user]
):
If you are overwriting Devise's default controllers, it is not any different from any other controller to add your own route.
After you create your devise controllers to overwrite, do the following:
Under sessions_controller declare a method
# app/controllers/devise/sessions_controller.rb
def check_active
# do what you want to do
end
And in your router:
# config/routes.rb
devise_scope :sessions do
get 'check_active', to: "devise/sessions#check_active"
end
I was trying the same thing and realized that the scope should be for user and not sessions, also ensure that it has to be singular.
devise_scope :user do
get '/check_active' => 'sessions#check_active'
end
Edit: Adding link to help docs for better understanding

Is this a valid devise route?

Hey guys is this a valid route to declare in the config/routes file? I want to a user to directed to a certain registration controller based on a condition but it doesn't seem to generate the route (when I do a rake routes command)
if Rails.application.config_for(:app)['app_for'] == 'vodacom'
devise_for :users, controllers: { registrations: "vodacom/users/registrations"}
else
devise_for :users, controllers: { registrations: "users/registrations"}
end
No, routes are generated on app load, so whatever the initial value is what is going to be the route created. So only one of these two routes will work.
You can check in the controller, and if your condition is true, redirect them to the other controller. But just create both without the conditional and do the conditional in the controller and redirect.

rails 4 with devise, order of routes definition makes redirection loop with redirects

I was doing a sample project with devise, and have this on my routes:
resources :admins
devise_for :admins
It causes a redirection loop. I changed the order of the lines to look like:
devise_for :admins
resources :admins
And then it worked.
Why?
Rails routes are matched in the order they are specified and both resources and devise_for are shorthand for multiple routes. Here is a list of routes generated by resources and here is a list of the one's generated by Devise.
I can't answer what's specifically causing the redirect loop without knowing all of the '-able' devise categories you have (confirmable, recoverable etc.), but it seems like the problem would be the two POST routes generated for creating a new admin. This is why it's generally considered best practice not to use resources in the routes.
If you want to know for sure you can run rake routes in your terminal to see all the routes that those resources have made, identify any potential duplicates and then put those duplicates behind an except: in the resources to see if you've caught the problem :
resources :admins, except: :create
devise_for :admins

Resources