I have 2 Rails 4.0.1 projects using the same version of devise. They have identical config/initializers/devise.rb and both have this line in the config/routes.rb
devise_for :users, controllers: {omniauth_callbacks: "users/omniauth_callbacks", registrations: "users"}
In the application that works I get this output in the rake routes, but not in the app that doesn't:
user_omniauth_authorize GET|POST /users/auth/:provider(.:format) users/omniauth_callbacks#passthru {:provider=>/github/}
user_omniauth_callback GET|POST /users/auth/:action/callback(.:format) users/omniauth_callbacks#(?-mix:github)
Does anyone know why this would work in one app but not another? Am I missing something obvious here?
Related
I have created an Admin model with the Devise gem. Using the Devise controller generator, I now have a app/controllers/admins folder containing all the stock controllers for me to modify if I choose, such as sessions_controller, passwords_controller, etc.
However, I can't figure out how to just get an Admin controller and simple admin routes like admin_path or new_admin_path.
Here's my rake routes | grep admin
new_admin_session GET /admin/sign_in(.:format) admins/sessions#new
admin_session POST /admin/sign_in(.:format) admins/sessions#create
destroy_admin_session DELETE /admin/sign_out(.:format) admins/sessions#destroy
new_admin_password GET /admin/password/new(.:format) devise/passwords#new
edit_admin_password GET /admin/password/edit(.:format) devise/passwords#edit
admin_password PATCH /admin/password(.:format) devise/passwords#update
PUT /admin/password(.:format) devise/passwords#update
POST /admin/password(.:format) devise/passwords#create
admin_root GET /admin(.:format) admins/sessions#portal
admin_sign_out GET /admin/sign_out(.:format) admin/sessions#destroy
And here are the relevant parts of my routes.rb
devise_for :admins, path: 'admin', controllers: { sessions: 'admins/sessions' }
devise_scope :admin do
get "/admin", to: 'admins/sessions#portal', as: 'admin_root'
get "/admin/sign_out", to: 'admin/sessions#destroy', as: 'admin_sign_out'
end
You'll see that I've currently got a portal method in my Admin::SessionsController, which is my current workaround. I know the right place for that page is in an AdminsController but I can't figure out how to set that up.
Adding admins: 'admins/admins' to the devise_for :admins, controllers: block doesn't give me any new routes. I tried adding an AdminsController with methods but that doesn't help either, trying to go to /admin/new or /admins/new says no route matches.
This is how I have my device and namespaces set up
# config/routes.rb
Rails.application.routes.draw do
devise_for :admins, :controllers => { registrations: 'admins/registrations',
sessions: 'admins/sessions',
passwords: 'admins/passwords',
confirmations: 'admins/confirmations'
}
authenticate :admin do
namespace :admins do
...
root 'dashboards#index'
end
end
...
root 'pages#index'
Now controllers are also important.
#app/controllers/admin_controller.rb
class AdminController < ApplicationController
layout 'admins/application'
before_filter :authenticate_admin!
end
The Devise controllers I have them set like this
#app/controllers/admins/sessions_controller.rb
class Admins::SessionsController < Devise::SessionsController
layout 'admins/application'
...
end
Repeat this process for all your other device controllers
Let me know if this helps you out
My Rails 5 app running on Heroku uses Devise for auth and it is currently nested. Looks like this in routes.rb:
resources :teams do
member do
get :about
get :foundation
end
devise_for :users, :path_prefix => 'auth',
:controllers => {
:masquerades => "admin/masquerades",
:registrations => "registrations",
:sessions => "sessions",
:password => "passwords"
}
When I try to set up forgot password I get error after error with wrong routes because all standards from devise will point to new_user_session_path and not new_user_team_session_path.
Currently I get this error: undefined method new_user_session_path for <ActionDispatch::Routing::RoutesProxy:0x007fd6e50b1328>
My idea has been to generate the devise views and controllers and fix all the links. However, the current error does not appear in the passwords controller I took from the source: https://github.com/plataformatec/devise/blob/master/app/controllers/devise/passwords_controller.rb
My problem is this: I want to add a 'Reset password' feature and have nested Devise routes. How do I solve this?
EDIT:
Here is output from rake routes:
new_user_team_session GET /auth/users/teams/:team_id/sign_in(.:format) sessions#new
user_team_session POST /auth/users/teams/:team_id/sign_in(.:format) sessions#create
destroy_user_team_session DELETE /auth/users/teams/:team_id/sign_out(.:format) sessions#destroy
new_user_team_password GET /auth/users/teams/:team_id/password/new(.:format) devise/passwords#new
edit_user_team_password GET /auth/users/teams/:team_id/password/edit(.:format) devise/passwords#edit
user_team_password PATCH /auth/users/teams/:team_id/password(.:format) devise/passwords#update
PUT /auth/users/teams/:team_id/password(.:format) devise/passwords#update
POST /auth/users/teams/:team_id/password(.:format) devise/passwords#create
cancel_user_team_registration GET /auth/users/teams/:team_id/cancel(.:format) registrations#cancel
new_user_team_registration GET /auth/users/teams/:team_id/sign_up(.:format) registrations#new
edit_user_team_registration GET /auth/users/teams/:team_id/edit(.:format) registrations#edit
user_team_registration PATCH /auth/users/teams/:team_id(.:format) registrations#update
PUT /auth/users/teams/:team_id(.:format) registrations#update
DELETE /auth/users/teams/:team_id(.:format) registrations#destroy
POST /auth/users/teams/:team_id(.:format) registrations#create
back_user_team_masquerade_index GET /auth/users/teams/:team_id/masquerade/back(.:format) admin/masquerades#back
user_team_masquerade GET /auth/users/teams/:team_id/masquerade/:id(.:format) admin/masquerades#show
Cheers
I have the following code in routes.rb:
devise_for :admin_users, controllers: {
registrations: 'tap/registrations',
sessions: 'tap/sessions',
passwords: 'tap/passwords',
confirmations: 'tap/confirmations'
}
The code above creates the following routes:
new_admin_user_session GET /admin_users/sign_in(.:format) tap/sessions#new
admin_user_session POST /admin_users/sign_in(.:format) tap/sessions#create
destroy_admin_user_session DELETE /admin_users/sign_out(.:format) tap/sessions#destroy
admin_user_password POST /admin_users/password(.:format) tap/passwords#create
new_admin_user_password GET /admin_users/password/new(.:format) tap/passwords#new
edit_admin_user_password GET /admin_users/password/edit(.:format) tap/passwords#edit
PATCH /admin_users/password(.:format) tap/passwords#update
PUT /admin_users/password(.:format) tap/passwords#update
For some reason, the registrations and confirmations controller are not appearing in routes. How do I fix this?
inside model (admin_users.rb) have you added this line below, probably this link can help you more
devise :database_authenticatable, :registerable
Background:
The Rails 4 application I am working on has differing logic for API and web registrations, and part of this logic makes their integration extremely difficult. To that end, I'm attempting to separate the routes to deal with issues arising from inheritance, new reCAPTCHA gem, and new logic. (both actions call registrations#create after their respective logic.) I've solved most of the issues arising from separating these two; however, getting the routes working has proven difficult as well.
I'd greatly appreciate any help!
Desired Result:
I'm trying to define a route to a custom action using Devise, and prevent it from creating the default route as well. I've gotten one of them working, but not the second. Here's the excerpt from my routes.rb:
Registry::Application.routes.draw do
devise_for :user,
controllers: {
passwords: 'users/passwords',
sessions: 'users/sessions',
registrations: 'users/registrations'
}
devise_scope :user do
post 'users', to: 'users/registrations#custom_one'
end
# ...
namespace :api do
namespace :v1 do
# ...
devise_scope :user do
post 'users', to: 'registrations#custom_two'
end
end
end
end
Issues:
The issue is that this code generates two nearly-identical routes. Excerpt from rake routes:
user_registration POST /users(.:format) users/registrations#create
users POST /users(.:format) users/registrations#custom_one
api_v1_users POST /api/v1/users(.:format) api/v1/registrations#custom_two
I also want the custom route to have the correct prefix/route name (user_registration), though I've been unable to do this.
I've found plenty of documentation on custom names for Devise routes, but not for custom actions. Especially not when using devise_for.
To summarize:
I need to disable the default users/registrations#create route
and specify a route to a custom action (users/registrations#custom_one)
with the correct prefix/name (user_registration)
hopefully as elegantly as possible, as I would rather avoid specifying each route independently.
You can achieve this by using the :skip option to devise_for:
devise_for :users, :skip => [:registrations] do
get "/admin" => "devise/registrations#new", :as => :new_user_registration
post "/admin" => "devise/registrations#create", :as => :user_registration
end
I'm trying to use Devise and Omniauth to allow a user to sign in to multiple providers with a Rails 3.2 app. I got it to work but only if it goes to a twitter, facebook or tumblr method in the controller where I want all the providers to go to a create method. This is what I have the routes to do this: match '/auth/:provider/callback' => 'authentications#create'
resources :authentications
match '/auth/:provider/callback' => 'authentications#create'
devise_for :users, path_names: {sign_in: "login", sign_out: "logout"},
controllers: {omniauth_callbacks: "authentications"}
root to: 'authentications#index'
My rake routes show:
user_omniauth_authorize /users/auth/:provider(.:format) authentications#passthru {:provider=>/twitter|facebook|tumblr/}
user_omniauth_callback /users/auth/:action/callback(.:format) authentications#(?-mix:twitter|facebook|tumblr)
I want the user_omni_auth_callback route to to to authentications#create and not the specific providers. With the current setup, I get "The action 'twitter' could not be found for AuthenticationsController" because I'm not including it. What's the best way to do this?