I started using custom routes w/Devise so that I could have my 'Sign In' and 'Sign Up' routes go to the same page. However, as soon as I followed the instruction from Devise about custom routes, it seems that every route now has to be explicitly specified. This has now broken my reset password links since that portion is handled by Devise.
What am I doing wrong here? You can see below that I've had to spell out everything for my User and UserSessions model. Shouldn't I only have to specify the ones I want to change?
devise_for :users, :controllers => { :sessions => "user_sessions" ,:registrations=>"users"},:skip => [:sessions] do
get 'users/sign_in' => 'user_sessions#new', :as => :new_user_session
get 'users/sign_up' => 'user_sessions#new', :as => :new_user_session
post 'users/sign_in' => 'user_sessions#create', :as => :user_session
post 'user_sessions' => 'user_sessions#create', :as => :app_sign_in
delete 'users/sign_out' => 'user_sessions#destroy', :as => :destroy_user_session
get 'users/sign_out' => 'user_sessions#destroy', :as => :destroy_user_session
post 'users/:id' => 'users#update', :as =>:update_user
get 'users' => 'users#index'
get 'users/:id/edit' => 'users#edit', :as => :edit_user
get 'users/:id' => 'users#show', :as => :show_user
delete 'users/:id' => 'users#destroy', :as => :destroy_user
end
Can you just use, not sure if this will work for you
devise_for :users
devise_for :users do
get '/users/sign_out' => 'devise/sessions#destroy'
end
Related
I am trying to change the 'users/sign_in' to 'sign-in' route
This is my current route setup
devise_for :users, :controllers => { :registrations => 'registrations', :omniauth_callbacks => "authentications" }
devise_scope :user do
put 'update_plan', :to => 'registrations#update_plan'
put 'update_card', :to => 'registrations#update_card'
put 'charge', :to => 'registrations#charge'
get "/sign-up" => "users/registrations#new", :as => :new_user_registration
get '/sign-in' => "devise/sessions#new", :as => :new_user_session
post '/sign-in' => 'devise/sessions#create', :as => :user_session
get '/sign-out' => 'devise/sessions#destroy', :as => :destroy_user_session
end
but these routes give me the error ArgumentError: Invalid route name, already in use: 'new_user_registration'
can I move the :controllers => { :registrations => 'registrations', :omniauth_callbacks => "authentications" } bit into devise_scope?
If i remove :as => :new_user_registration from my routes then i get a redirect loop. I really can't figure this out
Any help is much appreciated thanks.
You need to tell Devise to skip session ULRs.
devise_for :users :controllers => { :registrations => 'registrations', :omniauth_callbacks => "authentications" }, :skip => [:sessions]
devise_scope :user do
put 'update_plan', :to => 'registrations#update_plan'
put 'update_card', :to => 'registrations#update_card'
put 'charge', :to => 'registrations#charge'
get "/sign-up" => "users/registrations#new", :as => :new_user_registration
get '/sign-in' => "devise/sessions#new", :as => :new_user_session
post '/sign-in' => 'devise/sessions#create', :as => :user_session
get '/sign-out' => 'devise/sessions#destroy', :as => :destroy_user_session
end
See How To: Change the default sign_in and sign_out routes
I am running Rails 3.2.12 and Devise 3.1 and I have in the routes.rb this:
devise_for :users do
get '/login' => 'devise/sessions#new', as: :login
get '/logout' => 'devise/sessions#destroy', as: :logout
end
However, when I hit
127.0.0.1:3000/login
I get
No route matches [GET] "/login"
What works is
127.0.0.1:3000/users/login
Is there anything else I have to do so that I can skip typing /users/ part?
Thank you!
devise_for :users, :path => '', :path_names => { :sign_in => 'login'}
Good info about customizing Devise paths on this StackOverflow post :)
Here's some live code which works for one of our live apps:
#User Management (Devise)
devise_for :users, :path => '', :controllers => {:sessions => 'sessions', :registrations => 'registrations'}, :path_names => { :sign_in => 'login', :password => 'forgot', :confirmation => 'confirm', :unlock => 'unblock', :registration => 'register', :sign_up => 'new', :sign_out => 'logout'}
as :user do
get 'register', :to => 'devise/registrations#new'
delete 'logout', :to => 'sessions#destroy'
end
devise_scope :user do
get "/login" => "devise/sessions#new"
end
See this for more details
On the sign up and forgot password views in Devise, if you get an error, it redirects you to the parent page.
So on the sign up page (/users/sign_up), if you get an error, it redirects you to /users and shows the error.
On the forgot password page (/users/password/new), if you get an error, it redirects you to /users/password and shows the error.
How can I change it so it does the same thing as the sign in page, if there's an error, it stays on the same page and shows the error.
I've looked through Devise and can't find where the redirect is.
Here's my routes for Devise:
devise_for :users, :skip => [:sessions]
as :user do
get 'signin' => 'devise/sessions#new', :as => :new_user_session
post 'signin' => 'devise/sessions#create', :as => :user_session
get 'signup' => 'devise/registrations#new', :as => :new_user
post 'signup' => 'devise/registrations#create', :as => :create_user_session
delete 'signout' => 'devise/sessions#destroy', :as => :destroy_user_session
get "/account" => "devise/registrations#edit"
end
I think the problem is that you have the post 'signup' named incorrectly. What path does your user signup form POST to?
post 'signup' => 'devise/registrations#create', :as => :create_user_session
Should be:
post 'signup' => 'devise/registrations#create', :as => :user_registration
Here's a look at my routes.rb which solved this issue:
as :user do
get "/signin" => "devise/sessions#new", :as => :new_user_session
post "/signin" => "devise/sessions#create", :as => :user_session
delete "/signout" => "devise/sessions#destroy", :as => :destroy_user_session
get "/signup" => "devise/registrations#new", :as => :new_user_registration
post '/signup' => 'devise/registrations#create', :as => :user_registration
end
It doesn't redirect you anywhere, those are the URLs that Devise posts to.
If you want to edit these URLs, see the wiki for a good starting point:
https://github.com/plataformatec/devise/wiki/How-To:-Change-the-default-sign_in-and-sign_out-routes
My routes.rb is look like this.
devise_for :users, :skip => [:sessions]
as :user do
get 'signin' => 'devise/sessions#new', :as => :new_user_session
post 'signin' => 'devise/sessions#create', :as => :user_session
delete 'signout' => 'devise/sessions#destroy', :as => :destroy_user_session
end
When i have run rake routes i got the error
undefined method `as' for #<ActionDispatch::Routing::Mapper:0xa954f20>
/home/ramkishan/vijay_work/kirana/config/routes.rb:4:in `block in <top (required)>'
No need to use as,
devise_for :users, :skip => [:sessions] do
get '/signin' => "devise/sessions#new", :as => :new_user_session
post '/signin' => 'devise/sessions#create', :as => :user_session
get '/signout' => 'devise/sessions#destroy', :as => :destroy_user_session
end
should do.
By default Devise creates a routing for a sign-in as '/sign_in'.
How would I change the path so that it is '/log_in'?
By adding scope
devise_for :users
devise_scope :user do
get '/login' => 'devise/sessions#new'
get '/logout' => 'devise/sessions#destroy'
end
This is how I did it in the end, given that I was already inserting controllers to handle aspects of Devise:
in routes.rb
#Add Devise authentication to users, handling omniauth callbacks in users/omniauth_callbacks_controller
devise_for :users, :skip => [:sessions],
:controllers => { :omniauth_callbacks => 'users/omniauth_callbacks',
:registrations => 'users/registrations'
}
# :skip => [:sessions] tells devise not to create routes for sessions, allowing us to declare our own
as :user do
get 'users/log_in' => 'devise/sessions#new', :as => :new_user_session
post 'users/log_in' => 'devise/sessions#create', :as => :user_session
delete 'users/log_out' => 'devise/sessions#destroy', :as => :destroy_user_session
end