I have a controller of Workers.
If I logged out, I am redirected to: localhost:3000. I want to be redirect to the sign_in of my devise.
this is my rake routes:
users_sign_out GET /users/sign_out(.:format) devise/sessions#destroy
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
and this is my routes.rb:
devise_for :users do get '/users/sign_out' => 'devise/sessions#destroy' end
resources :tasksadmins
resources :workers
root to: "devise/registrations#create"
It is specified in the devise wiki.
https://github.com/plataformatec/devise/wiki/How-To:-Change-the-redirect-path-after-destroying-a-session-i.e.-signing-out
class ApplicationController < ActionController::Base
private
# Overwriting the sign_out redirect path method
def after_sign_out_path_for(resource_or_scope)
new_user_session_path
end
end
In you can override the after_sign_out_path_for helper in your application controller.
def after_sign_out_path_for(resource_or_scope)
# logic here
end
Related
Basically, I am trying to do token authentication with devise and JWT in rails. I followed this guide: https://dev.to/dhintz89/devise-and-jwt-in-rails-2mlj
When a make a request using postman to the route I believe should register a user, I get the error defined in the controller: 422 Unprocessable entity "email or password is invalid"
My registrations controller:
class RegistrationsController < Devise::RegistrationsController
def create
user = User.new(sign_up_params)
if user.save
token = user.generate_jwt
render json: token.to_json
else
render json: { errors: { 'email or password' => ['is invalid'] } }, status: :unprocessable_entity
end
end
end
My routes:
Rails.application.routes.draw do
devise_for :users,
controllers: {
registrations: :registrations,
sessions: :sessions
}
root 'homepage#index'
get '/*path' => 'homepage#index'
end
When I run rake routes, I get the following:
new_user_session GET /users/sign_in(.:format) sessions#new
user_session POST /users/sign_in(.:format) sessions#create
destroy_user_session DELETE /users/sign_out(.:format) sessions#destroy
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
user_password PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
POST /users/password(.:format) devise/passwords#create
cancel_user_registration GET /users/cancel(.:format) registrations#cancel
new_user_registration GET /users/sign_up(.:format) registrations#new
edit_user_registration GET /users/edit(.:format) registrations#edit
user_registration PATCH /users(.:format) registrations#update
PUT /users(.:format) registrations#update
DELETE /users(.:format) registrations#destroy
POST /users(.:format) registrations#create
root GET / home#index
My request looks like:
http://localhost:4000/users?email=test#gmail.com&password=abc123&password_confirmation=abc123
What am I missing?
I have these 2 routes that are conflicting in my application
destroy_users DELETE /users/:id(.:format) users#destroy
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
the corresponding part of my routes file is as follows
match '/users/:id', to: 'users#destroy', :via => :delete, :as =>:destroy_users
devise_for :users
resources :users
When I try to destroy a session using the following link
<li><%= link_to "Sign out", destroy_user_session_path, method: "delete" %></li>
it tries to navigate to the following route
localhost:3000/users/sign_out
this is going to my UserController and trying to run the destroy method passing sign_out as a parameter rather than a route
Couldn't find User with id=sign_out
The first line in my routes file was to allow a single user to be deleted in the UserController, this is the destroy method. This had to be put before the devise_for otherwise it was trying to route to the edit_user_registration_path of devise using DELETE. Now it seems to be overwriting the devise destroy_user_session_path
I'm not sure how to fix this any advice would be appreciated
Typically, I would think devise_for :users creates the following routes
Prefix Verb URI Pattern Controller#Action
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
using your routes you get
Prefix Verb URI Pattern Controller#Action
destroy_users DELETE /users/:id(.:format) users#destroy <--- oops this should be below all the other routes
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy <--- notice it typically would be
Deleting the first match route should help you... or you can move it below your resources :users. You should skip creating destroy with resources.
Routing Error
No route matches {:action=>"edit", :controller=>"statuses", :id=>nil}
When http://0.0.0.0:3000/users/2
Dont understand why it says :controller=>"statuses" when my route file says:
Treebook::Application.routes.draw do
get "users/show"
resources :credits
resources :merchants
devise_for :users
resources :statuses
root to: 'statuses#index'
get 'users/:id', to: 'users#show'
Rake Route
root / statuses#index
credits GET /credits(.:format) credits#index
POST /credits(.:format) credits#create
new_credit GET /credits/new(.:format) credits#new
edit_credit GET /credits/:id/edit(.:format) credits#edit
credit GET /credits/:id(.:format) credits#show
PUT /credits/:id(.:format) credits#update
DELETE /credits/:id(.:format) credits#destroy
merchants GET /merchants(.:format) merchants#index
POST /merchants(.:format) merchants#create
new_merchant GET /merchants/new(.:format) merchants#new
edit_merchant GET /merchants/:id/edit(.:format) merchants#edit
merchant GET /merchants/:id(.:format) merchants#show
PUT /merchants/:id(.:format) merchants#update
DELETE /merchants/:id(.:format) merchants#destroy
statuses GET /statuses(.:format) statuses#index
POST /statuses(.:format) statuses#create
new_status GET /statuses/new(.:format) statuses#new
edit_status GET /statuses/:id/edit(.:format) statuses#edit
status GET /statuses/:id(.:format) statuses#show
PUT /statuses/:id(.:format) statuses#update
DELETE /statuses/:id(.:format) statuses#destroy
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
user GET /users/:id(.:format) users#show
get "users/show" - that will probably be causing your problem
Here's a better routes file for you:
root to: "statuses#index"
resources :credits, :merchants, :statuses
devise_for :users
resources :users, only: [:show]
Aggggg
The problem was on some footer links that I copy pasted. I just deleted both links since I dont need them. Incredible how can your attention gets stuck in a point far away from the root problem.
|
My Rails version is 3.2.12. I use following commands to create users authentications:
rails generate devise:install
rails generate devise users
rake db:migrate
and following are my routes
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
But i still cant register new users or login into app even though user email and password are saved on db. Following are errors that i got:
NameError in Devise::RegistrationsController#create
uninitialized constant Model
Where i might be wrong?
try this to change this line for devise.rb
config.sign_out_via = :delete
to
config.sign_out_via = :get
It works for me
Try this in your routes,
devise_for :users, :controllers => {:registrations => "devise/registrations"} do
get '/register' => 'devise/registrations#new', :as => :new_user_registration
end
I have simple RoR application with devise.
Here is the output ot rake routes
logout_index GET /logout/index(.:format) logout#index
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
Here is the code in application_controller
class ApplicationController < ActionController::Base
protect_from_forgery
def after_sign_in_path()
abort 'signed'
end
end
The problem is that when a user is signed up the method after_sign_in_path is not executed.
What I miss here ?
I think it should be after_sign_in_path_for. See here.