routes having the same name using the `:as` option - devise custom paths - ruby-on-rails

I'm trying to use custom paths for the registration and session with Devise.
My problem is that the routes can't have the same 'as'.
My code is this:
#Devise
devise_for :users, skip: [:sessions,:registration]
as :user do
get 'signup', to: 'users/registrations#new', as: :new_user_registration
post 'signup', to: 'users/registrations#create', as: :user_registration
get 'edit', to: 'users/registrations#edit', as: :edit_user_registration
put 'edit', to: 'users/registrations#update' , as: :user_registration
get 'signin', to: 'users/sessions#new', as: :new_user_session
post 'signin', to: 'users/sessions#create', as: :user_session
delete 'signout', to: 'users/sessions#destroy', as: :destroy_user_session
end
How you can see the problem is the put request for 'edit' has to have the same as: as the post
How can I fix this? with a match?

devise_for :users, skip: [:sessions, :registrations, :passwords]
devise_scope :user do
# sessions
get 'login', to: 'devise/sessions#new', as: :new_user_session
post 'login', to: 'devise/sessions#create', as: :user_session
delete 'logout', to: 'devise/sessions#destroy', as: :destroy_user_session
# registrations
put '/account', to: 'devise/registrations#update'
delete '/account', to: 'devise/registrations#destroy'
post '/account', to: 'devise/registrations#create'
get '/register', to: 'devise/registrations#new', as: :new_user_registration
get '/account', to: 'devise/registrations#edit', as: :edit_user_registration
patch '/account', to: 'devise/registrations#update', as: :user_registration
get '/account/cancel', to: 'devise/registrations#cancel', as: :cancel_user_registration# passwords
# passwords
get 'new-pass', to: 'devise/passwords#new', as: :new_user_password
get 'edit-pass', to: 'devise/passwords#edit', as: :edit_user_password
patch 'edit-pass', to: 'devise/passwords#update', as: :user_password
post 'new-pass', to: 'devise/passwords#create', as: :user_password
end
check the code below for customizing devise routes

According to the naming conventions, your patch/put helper should be named user_registration as you have done already, and you post helper should be named user_registrations, that's to say in the plural form.
Please, check out the http://guides.rubyonrails.org/routing.html#resource-routing-the-rails-default to be more confident in rails routing DSL and routing conventions.

Related

Change default routes Devise Rails

I have this routes.rb:
devise_for :users, :path => '', path_names:
{ sign_in: "login", sign_out: "logout", sign_up: "registration"}
I changed sign_in and sign_up routes and if you go to sign_up you will get 404 error, instead /registration will work. What I want is to change and add other routes like forgotten password in the same way.
If I type in the console rake routes, I see this for forgotten password:
new_user_password GET /password/new(.:format) devise/passwords#new
How can add the additional routes in a way that my custom defined route will work, but not the default?
Be sure to checkout the ActionDispatch::Routing::Mapper#devise_for documentation here.
You can just simply do something like this-
devise_for :users, path: 'auth', path_names: { sign_in: 'login',
sign_out: 'logout',
password: 'secret',
confirmation: 'verification',
unlock: 'unblock',
registration: 'register',
sign_up: 'cmon_let_me_in' }
Here is an example for the sessions, registrations, and passwords controller actions/routes:
devise_for :users, skip: [:sessions, :registrations, :passwords]
devise_scope :user do
# sessions
get 'login', to: 'devise/sessions#new', as: :new_user_session
post 'login', to: 'devise/sessions#create', as: :user_session
delete 'logout', to: 'devise/sessions#destroy', as: :destroy_user_session
# registrations
put '/account', to: 'devise/registrations#update'
delete '/account', to: 'devise/registrations#destroy'
post '/account', to: 'devise/registrations#create'
get '/register', to: 'devise/registrations#new', as: :new_user_registration
get '/account', to: 'devise/registrations#edit', as: :edit_user_registration
patch '/account', to: 'devise/registrations#update', as: :user_registration
get '/account/cancel', to: 'devise/registrations#cancel', as: :cancel_user_registration# passwords
# passwords
get 'new-pass', to: 'devise/passwords#new', as: :new_user_password
get 'edit-pass', to: 'devise/passwords#edit', as: :edit_user_password
patch 'edit-pass', to: 'devise/passwords#update', as: :user_password
post 'new-pass', to: 'devise/passwords#create', as: :user_password
end
As seen in the 4th code sample block in this wiki
You need to skip passwords and rebuild its routes as you want,
devise_for :users, skip: [:passwords]
devise_scope :user do
match '/forgotten-password' => 'devise/passwords#create', as: :user_password, via: [:post]
match '/forgotten-password' => 'devise/passwords#update', via: [:put, :patch]
get 'forgotten-password', to: 'devise/passwords#new', as: :new_user_password
end
Your custom routes will work. And it skip all routes in that modules.
NOTE : You need to overwrite all remaining routes of that module as you want.

Devise : overriding the devise routes and controllers at the same time

i want to ovverride my devise routes and the sessions controller from this gem at the same time. How do i do this ?
I thought about :
devise_for :admins, :skip => [:sessions],
controllers: { sessions: "admins/sessions" }
devise_scope :admin do
get 'login' => 'devise/sessions#new', :as => :new_admin_session
post 'login' => 'devise/sessions#create', :as => :admin_session
delete 'logout' => 'devise/sessions#destroy', :as => :destroy_admin_session
end
but my paths are chaging but the controller - not. How can I implement this right ?
When you specify controllers: { sessions: "admins/sessions" }, that implies that you have a file called sessions_controller.rb in this path: app/controllers/admins/sessions_controller.rb, and that it starts with:
module Admins
class SessionsController < Devise::SessionsController
If this is the controller you want your app to use, then in the devise_scope block, you must tell it to use admins/sessions, not devise/sessions, like this:
devise_scope :admin do
get 'login' => 'admins/sessions#new', :as => :new_admin_session
post 'login' => 'admins/sessions#create', :as => :admin_session
delete 'logout' => 'admins/sessions#destroy', :as => :destroy_admin_session
end
What about something like:
devise_for :admin, exclude: [:sessions] do
get '/login', to: 'sessions#new', as: :new_admin_session
post '/login', to: 'sessions#create', as: :admin_session
delete '/logout', to: 'sessions#destroy', as: :destroy_admin_session
end

Devise: Issue with routing using devise_scope

I'm trying to route my User model sign up form to /sign_in. Tried several things and just used the example given in the devise docs.
I copied in to my routes.rb
devise_scope :users do
get "sign_in", to: "devise/sessions#new"
end
and rake routes says it should be working and shows what I expected.
http://s1.postimg.org/do335u8v3/Screen_Shot_2014_06_18_at_11_34_34.png
What's going wrong here?
Try this instead.
devise_for :users do
get '/users/sign_in', :to => 'devise/sessions#new', :as => :new_user_session
get '/users/sign_out', :to => 'devise/sessions#destroy', :as => :destroy_user_session
get "/users/sign_up", :to => "registrations#new"
end

devise won't redirect to after_sign_up_path when using customized devise routes

I invoke the after_sign_up_path_for(resource) by defining it in an inherited registrations_controller:
class RegistrationsController < Devise::RegistrationsController
protected
def after_sign_up_path_for(resource)
new_user_profile_path(resource)
end
end
This redirect works fine if I don't customize the routes. however, when I do, it no longer works. My routes file has:
devise_for :users, skip: [:sessions], controllers: { registrations: "registrations" }
devise_scope :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
root to: 'pages#home'
end
Specifically, the post 'signup' => 'devise/registrations#create', as: :user_registration line messes things up. If that's not there, it works fine. However, if that's not there, then when registration fails due to validation error, the URL defaults to /users rather than /signup, which is an undesired behavior.
Anyone know if this is something I am doing wrong or if there's a bug in devise?
The issue with your custom routes is that you are pointed back to Devise controllers:
post 'signup' => 'devise/registrations#create', as: :user_registration
That obviously is not going to work. Point to your own controller instead:
post 'signup' => 'registrations#create', as: :user_registration
Just briefly looking at your code provided, I don't think the route you want is new_user_profile_path. You probably want user_profile_path, can you run rake routes and paste the output here?

Having trouble routing to profile homepage, rails

I'm having trouble having the root url route to a signed in user's profile page.
This is my config file-
get "profiles/show"
resources :players
devise_for :users
devise_scope :user do
get 'register', to: "devise/registrations#new", as: :register
get 'login', to: "devise/sessions#new", as: :login
get 'logout', to: "devise/sessions#destroy", as: :logout
end
resources :logistics
resources :notes
root :to => 'notes#index'
get '/:id', to: 'profiles#show'
I want that 'root to' to show the 'profiles#show' view. Just as the get '/:id' route does right below it.
I've tried and researched what I thought would work to no avail. Thanks for your help on this.
Hey try something like
match "/:id" => "profiles#show"

Resources