Devise conflicting with other routes using delete - ruby-on-rails

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.

Related

How to route my admin sign up to go to an admin page using Devise?

I first used Devise to create authorization for Users. Then I wanted to create a separate Admin user using Devise. I followed these directions from Devise using Option 1: https://github.com/plataformatec/devise/wiki/How-To:-Add-an-Admin-role.
I created my admin model and migration, added an admin into the database Admin.create(etc...)
I added an admin button on the homepage: <%= link_to "Admin", new_admin_session_path %>
My problem is that I click the admin button, the route does takes me to: http://localhost:3000/admins/sign_in, which is correct, but when I sign in, it goes to http://localhost:3000/users/sign_in. I do have flash messages so the flash message says the admin is signed in successfully. I think it has to do with my routes, because the Devise User and Devise Admin routes are the same. How do I configure this to go to admin views(I'm going to create an admin dashboard) and not mess up Devise Admin authorizations?
Here are my current routes:
Prefix Verb URI Pattern Controller#Action
new_admin_session GET /admins/sign_in(.:format) devise/sessions#new
admin_session POST /admins/sign_in(.:format) devise/sessions#create
destroy_admin_session DELETE /admins/sign_out(.:format) devise/sessions#destroy
admin_password POST /admins/password(.:format) devise/passwords#create
new_admin_password GET /admins/password/new(.:format) devise/passwords#new
edit_admin_password GET /admins/password/edit(.:format) devise/passwords#edit
PATCH /admins/password(.:format) devise/passwords#update
PUT /admins/password(.:format) devise/passwords#update
cancel_admin_registration GET /admins/cancel(.:format) devise/registrations#cancel
admin_registration POST /admins(.:format) devise/registrations#create
new_admin_registration GET /admins/sign_up(.:format) devise/registrations#new
edit_admin_registration GET /admins/edit(.:format) devise/registrations#edit
PATCH /admins(.:format) devise/registrations#update
PUT /admins(.:format) devise/registrations#update
DELETE /admins(.:format) devise/registrations#destroy
deals_index GET /deals/index(.:format) deals#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
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

No route matches [GET] "/sign_in"

Set up Authentication with AngularJS and Ruby on Rails. Try to do under this article https://www.airpair.com/ruby-on-rails/posts/authentication-with-angularjs-and-ruby-on-rails. But when run the test with rspec have the same error
Authentication login with valid inputs
Failure/Error: Unable to find matching line from backtrace
ActionController::RoutingError:
No route matches [GET] "/sign_in"
In rake routes have:
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 GET /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
user_confirmation POST /users/confirmation(.:format) devise/confirmations#create
new_user_confirmation GET /users/confirmation/new(.:format) devise/confirmations#new
GET /users/confirmation(.:format) devise/confirmations#show
root GET / devise/sessions#new
user_root GET /persons/profile(.:format) persons#profile
next_page GET /index(.:format) persons#index
GET /omniauth/:provider/callback(.:format) devise_token_auth/omniauth_callbacks#redirect_callbacks
As your routes say...you need to use /users/sign_in with GET request.
You are using /sign_in which doesn't exist...
you can directly use new_user_session_path
if you actually want to use /sign_in,edit your routes and add this
get 'sign_in' => 'devise/sessions#new'

Proper way of rails routing custom Users controller and devise gem

Im having a problem routing my custom Users controller and devise gem.
When i try to reach http://localhost:3000/users
<%= link_to 'Users', users_path, class: 'navbar-brand' %>
I get error:
Could not find devise mapping for path "/users". This may happen for two reasons:
1) You forgot to wrap your route inside the scope block. For example: devise_scope :user do get "/some/route" => "some_devise_controller" end
2) You are testing a Devise controller bypassing the router. If so, you can explicitly tell Devise which mapping to use: #request.env["devise.mapping"] = Devise.mappings[:user]
If i run command rake routes i get this output:
Prefix Verb URI Pattern Controller#Action
root GET / static_pages#home
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
user_confirmation POST /users/confirmation(.:format) devise/confirmations#create
new_user_confirmation GET /users/confirmation/new(.:format) devise/confirmations#new
GET /users/confirmation(.:format) devise/confirmations#show
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
account_update PATCH /account_update(.:format) users#update
And this are my routes
root 'static_pages#home'
devise_for :users
resources :users
as :user do
patch 'account_update' => 'users#update'
get 'users' => 'users#index'
end
How to tell rails that on /users it should go to controller Users and select index page
You can try something like that,
devise_for :users
devise_scope :user do
end

after_sign_in_path not called

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.

Rails 3.2.1: link works on app, is present in routes.rb but doesn't show up on rake routes output

I have these urls:
http://localhost:3000/assets
http://localhost:3000/assets/new
http://localhost:3000/assets/34
http://localhost:3000/assets/34/edit
they work in my app.
I have this routes.rb file:
devise_for :users
match "listings/show_notes" => "listings#show_notes", :as => :show_notes
resources :users
resources :listings
resources :assets
authenticated :user do
root :to => "listings#index"
end
#this route is for file downloads
match "assets/get/:id" => "assets#get", :as => :download
resources :admin_dash_board, :only => :index
I have the following output when I type in rake 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
show_notes /listings/show_notes(.:format) listings#show_notes
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
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
listings GET /listings(.:format) listings#index
POST /listings(.:format) listings#create
new_listing GET /listings/new(.:format) listings#new
edit_listing GET /listings/:id/edit(.:format) listings#edit
listing GET /listings/:id(.:format) listings#show
PUT /listings/:id(.:format) listings#update
DELETE /listings/:id(.:format) listings#destroy
root / listings#index
admin_dash_board_index GET /admin_dash_board(.:format) admin_dash_board#index
As you can see there are no routes for the resource :assets.
Any idea why? Or whats going on?
Thanks
I think I found your answer here and here.
Try to add this line in your application.rb:
config.assets.prefix = "/new_route"
EDIT - also here with some other options to solve the problem....

Resources