Rails Devise, how to correctly nest resources - ruby-on-rails

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

Related

How to configure routes for different user models using devise?

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

using multiple "root to" with 2 devise models

I have 2 devise models which is fine as they capture different values and i felt separating them was the best solution instead of using roles.
That being said, I am trying to root to different views. Here is my routes file:
devise_for :patients, controllers: {
sessions: 'patients/sessions',
registrations: 'patients/registrations'
}
as :patient do
authenticated do
root to: 'dashboard#patient'
end
unauthenticated do
root to: 'shared#home', as: 'unauthenticated_root'
end
end
devise_for :pharmacists, controllers: {
sessions: 'pharmacists/sessions',
registrations: 'pharmacists/registrations'
}
as :pharmacist do
authenticated do
root to: 'dashboard#pharmacist'
end
unauthenticated do
root to: 'shared#home', as: 'unauthenticated_root'
end
end
This is the error message:
Invalid route name, already in use: 'root'
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.
What would be the best way to alleviate this issue ?
You can achieve the same thing using the below hacks without the devise_scope
authenticated :patient do
root to: "patients#show", as: :authenticated_patient
end
authenticated :pharmacist do
root "pharmacists#index", as: :authenticated_pharmacist
end
unauthenticated do
root "pages#home", as: :unauthenticated_user
end
The issue stems from the fact that i was not using "as:" on the actual roots so they end up having the same endpoints. after fixing this issue the correct code looks like this:
devise_for :patients, controllers: {
sessions: 'patients/sessions',
registrations: 'patients/registrations'
}
devise_scope :patient do
authenticated do
root to: 'dashboard#patient', as: 'authenticated_patient_root'
end
unauthenticated do
root to: 'shared#home', as: 'unauthenticated_patient_root'
end
end
devise_for :pharmacists, controllers: {
sessions: 'pharmacists/sessions',
registrations: 'pharamacists/registrations'
}
devise_scope :pharmacist do
authenticated do
root to: 'dashboard#pharmacist', as: 'authenticated_pharmacist_root'
end
unauthenticated do
root to: 'shared#home', as: 'unauthenticated_pharmacist_root'
end
end

Rails & Devise Mapping Path

I'm receiving the following error when trying to go to http://app.mysite.dev/login -
Could not find devise mapping for path "/login".
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]
Now, here is the relevant bits of my routes.rb file:
namespace 'app', path: '', constraints: { subdomain: 'app' } do
devise_for :users, :skip => [:registrations, :confirmations]
devise_for :agents, :skip => :sessions
devise_scope :users do
get "login" => "users/sessions#new"
end
...
end
And the route generated by the get "login" line is as follows (from rake routes)
app_login GET /login(.:format) app/users/sessions#new {:subdomain=>"app"}
I don't know if it matters, but I'm using STI for Users > Agents relationship.
So, I already am defining the scope for devise, and I'm not testing, so any ideas what's going on?
Try to replace your devise_scope with the following instead. Within your namespace 'app' block.
devise_scope :app_user do
get "login" => "users/sessions#new"
end
It appears to be devise was changing the scope it was looking for within a namespace.
For your reference:
https://github.com/plataformatec/devise/issues/2496
And yeah, it should be devise_scope :app_user instead of devise_scope :app_users
It's just a simple typo - devise_scope :users should be devise_scope :user, as stated in the error message.
It seems you didn't define a custom SessionsControllerfor your :users, and Devise cannot use it's default one since you namespaced your devise_scope :users.
I'd define your own custom class App::SessionsController and then add it rewrite your routes like this:
namespace 'app', path: '', constraints: { subdomain: 'app' } do
devise_for :users, controllers: { sessions: 'sessions' }, skip: [:registrations, :confirmations]
devise_scope :users do
get "login" => "sessions#new"
end
end

Adding omniauth to devise routes

I am following this guide https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview
to configure devise with omniauth facebook sign up, now I am at the step where I need to add the omniauth_callbacks route to the devise routes, i have already customized a devise controller like this :
devise_for :users, controllers:{
registrations: "users/registrations"
}
So I added the omniauth route like this
devise_for :users, controllers:{
registrations: "users/registrations"
:omniauth_callbacks => "users/omniauth_callbacks"
}
But now the server is not starting because of a syntax error, I am pretty sure I need a block in the routes.
Please advise.
Thanks
You just have a missing comma
devise_for :users, controllers: {
registrations: "users/registrations",
omniauth_callbacks: "users/omniauth_callbacks"
}

rails routing devise parameters

I'm trying to modify an existing devise route to hold parameters
Here is the working route:
devise_for :contributors, controllers: {registrations: 'contributors/registrations', confirmations: 'contributors/confirmations'}
the working path:
<p>Create Account</p>
I need to be able to pass a parameter with it. Is there a simple way to do this?
I was thinking of doing something like this:
devise_for :contributors, controllers: {registrations: 'contributors/registrations(/:contributor_type)', confirmations: 'contributors/confirmations'}
<p>Create Account</p>
but no luck, it only returns this error: wrong constant name Registrations(
Nevermind, I didn't need to place a parameter inside the devise_for route declaration.
devise_for :contributors, controllers: {registrations: 'contributors/registrations', confirmations: 'contributors/confirmations'}
this works just fine with
<p>Create Account</p>

Resources