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
Related
So I've been trying to set my routes for devise as follows:
scope '(:locale)' do
devise_for :users, skip: [:registrations]
resources :questions
end
However when I authenticate in my controller, when you're not signed in devise redirects you to localhost:3000/users/sign_in.
What I expect to happen would it to redirect me to localhost:3000/en/users/sign_in. What am I doing wrong or what way is there around this?
So it turns out you can't set the routes in your scope, but rather need to set them manually. At least that's what's worked for me.
devise_for :users, skip: [:sessions]
as :user do
get ':locale/login' => 'devise/sessions#new', :as => :new_user_session
post ':locale/login' => 'devise/sessions#create', :as => :user_session
delete ':locale/logout' => 'devise/sessions#destroy', :as => :destroy_user_session
end
# your normal scope without devise_for in it
scope '(:locale)' do
resources :questions
end
After this the redirects work as expected.
If I enter an invalid username or password in
/admin/sign_in.
Devise redirect to /administrators/sign_in instead of /admin/sign_in.
This is my routes.rb
devise_for :administrators
devise_scope :administrator do
get "/admin/sign_in" => "devise/sessions#new"
end
rake routes | grep sing_in:
new_administrator_session GET /administrators/sign_in devise/sessions#new
administrator_session POST /administrators/sign_in devise/sessions#create
admin_sign_in GET /admin/sign_in devise/sessions#new
I'm running:
ruby '2.1.2'
rails '4.2.0'
devise '~> 3.4.1'
That get only adds to your existing routes. You need to stop Devise from creating the default session routes, and then create your own. Try adding changing your routes to:
devise_for :administrators, :skip => [:sessions]
as :user do
get 'admin/sign_in' => 'devise/sessions#new', :as => :new_user_session
post 'admin/sign_in' => 'devise/sessions#create', :as => :user_session
delete 'admin/sign_out' => 'devise/sessions#destroy', :as => :destroy_user_session
end
More info in the docs here.
Change your routes to like this
devise_for :administrators, :skip => [:sessions]
devise_scope :administrator do
get "/admin/sign_in" => "devise/sessions#new"
end
For some reason this time when I apply
<%= link_to "Sign Out", destroy_user_session_path, method: :delete%>
it returns a user/sign_out not found error
Digging through stackoverflow I found that by applying
devise_scope :user do
get '/users/sign_out' => 'devise/sessions#destroy'
end
to my routes.rb would allow it to work. And it did.
Afterwards, I decided to test it again, so I removed the above code, log in as the user, and magically the sign_out path link works again without the code.
What is going on here?
replice get in your route to delete:
devise_scope :user do
delete '/users/sign_out' => 'devise/sessions#destroy'
end
and i think that it should add other sessions action to your custom route and skip sessions controller in your devise route:
as :user do
delete '/users/sign_out' => 'devise/sessions#destroy', :as => :destroy_user_session
get '/users/signin' => 'devise/sessions#new', :as => :new_user_session
post '/users/signin' => 'devise/sessions#create', :as => :user_session
end
devise_for :users, :skip => [:sessions]
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?
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"