Ruby on Rails undefined method user_registration_path - ruby-on-rails

I'm getting an error:
NoMethodError in Devise/registrations#new
undefined method 'user_registration_path'
at this line:
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
This is my routes:
devise_for :user, :controllers => { :registrations => "devise/registrations" }, :skip => [:registrations, :sessions] do
# devise/registrations
get 'signup' => 'devise/registrations#new', :as => :new_user_registration
post 'signup' => 'devise/registrations#create', :as => :custom_user_registration
end
Rake Routes:
new_user_registration GET /signup(.:format) devise/registrations#new
custom_user_registration POST /signup(.:format) devise/registrations#create
why am I getting the user_registration_path error?

Running rake routes, do you see in output smth like this:
user_registration POST /users(.:format) devise/registrations#create
I think if you wrote this line
post 'signup' => 'devise/registrations#create', :as => :custom_user_registration
Now you have:
custom_user_registration POST /signup(.:format) devise/registrations#create
And should use custom_user_registration_path(resource_name) instead of registration_path(resource_name)

You are skipping the registrations routes and not overriding them all with the custom routes you've defined. Remove :registrations from the skipped routes. In your routes.rb:
This
devise_for :user, :controllers => { :registrations => "devise/registrations" }, :skip => [:registrations, :sessions] do
should be
devise_for :user, :controllers => { :registrations => "devise/registrations" }, :skip => [:sessions] do
Or you can add these custom routes if you want the path to always be /signup:
devise_for :user, :controllers => { :registrations => "devise/registrations" }, :skip => [:registrations, :sessions] do
get 'signup' => 'devise/registrations#new', :as => :new_user_registration
post 'signup' => 'devise/registrations#create', as => :user_registration
delete 'signup' => 'devise/registrations#destroy', as => :destroy_user_registration
end
I don't advise changing the helper names (as => :whatever) since the devise controllers and views use them. It's fine to add new ones. I'm not sure you need to specify the controller either if it's the default.

Related

Ruby on Rails & Devise, change 'users/sign_in' to 'sign-in' using omniauth_callbacks and registrations

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

Devise custom routing links not working

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

Rake routes error with devise gem

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.

Devise custom routes slippery slope

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

How to change the login and signup urls in devise plugin Rails

I am using devise plugin in my new Rails App. My issue is devise plugin has default roots for login and signup
/users/sign_in
/users/sign_up
I need to change this to
/login
/signup
For this I used the following routing
devise_for :users do
get "login", :to => "devise/sessions#new"
get "signup", :to => "devise/registrations#new"
end
With this I need to specify 'login_path' and 'signup_path' everywhere in my views where new_user_session_path and new_user_registration_path comes
What I want is a configuration in routes which maps '/login' and '/signup' to new_user_session_path and new_user_registration_path.
I have seen a post which route /users/sign_in and /users/sign_up to /sign_in and /sign_up using the below shown routing.
devise_for :user, :as => ''
I need some routing technique like this which routes /users/sign_in and /users/sign_up to /login and /signup.
Could anyone please help me with this.
UPDATE: I have changed my routes.rb file to
devise_for :users,
:controllers => { :sessions => 'devise/sessions'},
:skip => [:sessions] do
get '/login' => "devise/sessions#new", :as => :new_user_session
post '/login' => 'devise/sessions#create', :as => :user_session
get '/signout' => 'devise/sessions#destroy', :as => :destroy_user_session
get '/signup' => 'devise/registrations#new', :as => :new_user_registration
end
But still when I use link_to 'new_user_registration' in my views its not showing as '/signup' in the browser
Here are a little bit more options than you asked but it's clear:
devise_for :users,
:controllers => { :registrations => "users/registrations",
:confirmations => "users/confirmations",
:sessions => 'devise/sessions'},
: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
get "/signup" => "users/registrations#new", :as => :new_user_registration
end
Even more, with :registrations => "users/registrations" we can additionally customize redirects:
class Users::RegistrationsController < Devise::RegistrationsController
protected
def after_sign_up_path_for(resource)
welcome_path # it's not a home path
end
def after_update_path_for(resource)
edit_user_registration_path
end
end
Devise has a good wiki.
I was able to fix my issue by using the following code in my routes
devise_for :users,
:controllers => { :sessions => 'devise/sessions'},
:skip => [:sessions] do
get '/login' => "devise/sessions#new", :as => :new_user_session
post '/login' => 'devise/sessions#create', :as => :user_session
get '/signout' => 'devise/sessions#destroy', :as => :destroy_user_session
get "/signup" => "devise/registrations#new", :as => :new_user_registration
end
But still in my views if I use
link_to "Register", new_user_registration_path
In my browser its showing as
/user/sign_up and not as /signup
But if I directly type /signup I will get the registraion page. Is there any mapping I need to do here.
i hope i'm not too late ;)
here is how it works (just paste it in your routes.rb and you good to go):
devise_for :users, :controllers => {:sessions => 'devise/sessions'}, :skip => [:sessions] do
get 'login' => 'devise/sessions#new', :as => :new_user_session
post 'login' => 'devise/sessions#create', :as => :user_session
get 'logout' => 'devise/sessions#destroy', :as => :destroy_user_session
get 'register' => 'devise/registrations#new', :as => :new_user_registration
end
edit config/routes.rb
devise_for :users, path: ''
devise_for :users, path: '', path_names: { sign_in: 'login', sign_out: 'logout', password: 'secret', confirmation: 'verification', unlock: 'unblock', registration: 'register', sign_up: 'cmon_let_me_in' }
new_user_session GET /login(.:format) devise/sessions#new
user_session POST /login(.:format) devise/sessions#create
destroy_user_session DELETE /logout(.:format) devise/sessions#destroy
user_password POST /secret(.:format) devise/passwords#create
new_user_password GET /secret/new(.:format) devise/passwords#new
edit_user_password GET /secret/edit(.:format) devise/passwords#edit
PATCH /secret(.:format) devise/passwords#update
PUT /secret(.:format) devise/passwords#update
cancel_user_registration GET /register/cancel(.:format) devise/registrations#cancel
user_registration POST /register(.:format) devise/registrations#create
new_user_registration GET /register/cmon_let_me_in(.:format) devise/registrations#new
edit_user_registration GET /register/edit(.:format) devise/registrations#edit
PATCH /register(.:format) devise/registrations#update
PUT /register(.:format) devise/registrations#update
DELETE /register(.:format) devise/registrations#destroy
As this may still be the n°1 result people get when looking for that question, it might be useful to note that there is now a simple way to do that:
devise_for :users, path: '', path_names: { sign_in: 'login', sign_up: 'signup'}
Reference: https://github.com/heartcombo/devise/wiki/How-To:-Change-the-default-sign_in-and-sign_out-routes
If I am reading and understanding your question correctly, you are after 'match'.
devise_for :users
match "/login" => "devise/sessions#new"
match "/signup" => "devise/registrations#new"
Be sure to have them in the correct order as they are matched based on line numbers in the file.
More can be found at: http://guides.rubyonrails.org/routing.html

Resources