Why devise_token_auth break vanilla devise routes? - devise

I am using devise and devise_token_auth in my Rails5 app.
Devise omniauth ( facebook/google ) for web and devise_token_auth for mobile app.
i defined routes with:
scope module: 'api' do
namespace :v1 do
mount_devise_token_auth_for 'User', at: 'auth'
end
end
devise_for :users, :controllers => { :omniauth_callbacks => "callbacks" }, as: 'auth'
resources :users
and get some routes
=========================TOKEN AUTH=======================================
new_v1_user_session GET /v1/auth/sign_in(.:format) devise_token_auth/sessions#new
v1_user_session POST /v1/auth/sign_in(.:format) devise_token_auth/sessions#create
destroy_v1_user_session DELETE /v1/auth/sign_out(.:format) devise_token_auth/sessions#destroy
new_v1_user_password GET /v1/auth/password/new(.:format) devise_token_auth/passwords#new
edit_v1_user_password GET /v1/auth/password/edit(.:format) devise_token_auth/passwords#edit
v1_user_password PATCH /v1/auth/password(.:format) devise_token_auth/passwords#update
PUT /v1/auth/password(.:format) devise_token_auth/passwords#update
POST /v1/auth/password(.:format) devise_token_auth/passwords#create
cancel_v1_user_registration GET /v1/auth/cancel(.:format) devise_token_auth/registrations#cancel
new_v1_user_registration GET /v1/auth/sign_up(.:format) devise_token_auth/registrations#new
edit_v1_user_registration GET /v1/auth/edit(.:format) devise_token_auth/registrations#edit
v1_user_registration PATCH /v1/auth(.:format) devise_token_auth/registrations#update
PUT /v1/auth(.:format) devise_token_auth/registrations#update
DELETE /v1/auth(.:format) devise_token_auth/registrations#destroy
POST /v1/auth(.:format) devise_token_auth/registrations#create
new_v1_user_confirmation GET /v1/auth/confirmation/new(.:format) devise_token_auth/confirmations#new
v1_user_confirmation GET /v1/auth/confirmation(.:format) devise_token_auth/confirmations#show
POST /v1/auth/confirmation(.:format) devise_token_auth/confirmations#create
v1_auth_validate_token GET /v1/auth/validate_token(.:format) devise_token_auth/token_validations#validate_token
v1_auth_failure GET /v1/auth/failure(.:format) devise_token_auth/omniauth_callbacks#omniauth_failure
GET /v1/auth/:provider/callback(.:format) devise_token_auth/omniauth_callbacks#omniauth_success
GET|POST /omniauth/:provider/callback(.:format) devise_token_auth/omniauth_callbacks#redirect_callbacks
omniauth_failure GET|POST /omniauth/failure(.:format) devise_token_auth/omniauth_callbacks#omniauth_failure
GET /v1/auth/:provider(.:format) redirect(301)
======================WEB==========================================
new_auth_user_session GET /users/sign_in(.:format) devise/sessions#new
auth_user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_auth_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
new_auth_user_password GET /users/password/new(.:format) devise/passwords#new
edit_auth_user_password GET /users/password/edit(.:format) devise/passwords#edit
auth_user_password PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
POST /users/password(.:format) devise/passwords#create
cancel_auth_user_registration GET /users/cancel(.:format) devise/registrations#cancel
new_auth_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_auth_user_registration GET /users/edit(.:format) devise/registrations#edit
auth_user_registration PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
POST /users(.:format) devise/registrations#create
new_auth_user_confirmation GET /users/confirmation/new(.:format) devise/confirmations#new
auth_user_confirmation GET /users/confirmation(.:format) devise/confirmations#show
POST /users/confirmation(.:format) devise/confirmations#create
auth_user_google_oauth2_omniauth_authorize GET|POST /omniauth/google_oauth2(.:format) callbacks#passthru
auth_user_google_oauth2_omniauth_callback GET|POST /omniauth/google_oauth2/callback(.:format) callbacks#google_oauth2
auth_user_facebook_omniauth_authorize GET|POST /omniauth/facebook(.:format) callbacks#passthru
auth_user_facebook_omniauth_callback GET|POST /omniauth/facebook/callback(.:format)
but routes for web must be like
auth_user_google_oauth2_omniauth_authorize GET|POST /users/auth/google_oauth2(.:format) callbacks#passthru
auth_user_google_oauth2_omniauth_callback GET|POST /users/auth/google_oauth2/callback(.:format) callbacks#google_oauth2
auth_user_facebook_omniauth_authorize GET|POST /users/auth/facebook(.:format) callbacks#passthru
auth_user_facebook_omniauth_callback GET|POST /users/auth/facebook/callback(.:format)
When i comment devise_token_auth initializer all web routes are fine, but when i turn on initializer - it`t broken.
Initializers for devise and token_auth are default.
how i can fix it?

Related

link_to paths not linking after adding bootstrap

I have a rails application that is built on the SAAS stripe application that is hosted here:
http://railsapps.github.com/rails-recurly-subscription-saas/
I added twitter bootstrap to this and my paths are no longer creating links.
For instance my route file has:
resources :users
however no users_path is creating a link to the location in my erb files
<%= link_to 'Admin', users_path %>
Any ideas or additional information i could provide?
Rake routes:
E:\Sites>rake routes
stripe_event /stripe StripeEvent::Engine
content_gold GET /content/gold(.:format) content#gold
content_silver GET /content/silver(.:format) content#silver
content_platinum GET /content/platinum(.:format) content#platinum
root / home#index
root / home#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) registrations#cancel
user_registration POST /users(.:format) registrations#create
new_user_registration GET /users/sign_up(.:format) registrations#new
edit_user_registration GET /users/edit(.:format) registrations#edit
PUT /users(.:format) registrations#update
DELETE /users(.:format) registrations#destroy
update_plan PUT /update_plan(.:format) registrations#update_plan
update_card PUT /update_card(.:format) registrations#update_card
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
So this seems to be caused by bad references to images that occured when i added an asset/images directory from the wrapbootstrap template.
I added this line to my application.rb file below the config.assets.enabled=true
config.assets.paths << Rails.root.join("app", "images")
To get my images to show up after this I had to change them to be in rails format, so background: img(..) became
background: image-url('images/social_icons.png')

How to reroute standard rest actions to devise controller?

I have been building devise into a small Rails app. It's a pretty standard setup but one wrinkle is that the default authentication rules need to be changed for this project. Non-idempotent registration actions must all be restricted to only authenticated users.
To do this, I've written a custom registrations controller and linked it up in the routing. All the custom controller really has to do is alter the filtering requirements. Based on the Rails controller filtering documentation, I got this working with a minimum of effort.
But by itself that only applies to devise-specific actions like /users/sign_up or /users/edit. I would also like to wire in the standard Rails actions (/users/new, /users/1/edit, etc) to the custom controller. I can get the action to instantiate the controller -- leading the horse to water so to speak -- but when the action is new instead of sign_up, it crashes out during the filtering.
The problem appears to be that resource_name yields nil when the action is 'new', but is fine when it's 'sign_up'. I don't understand why devise cares or where the distinction is represented in code. I've looked at other questions like Update the User controller of devise and Override devise registrations controller and Custom Devise controller but they don't get into this issue.
The specific error I am getting is
NoMethodError in RegistrationsController#new
undefined method `name' for nil:NilClass
and the top of the trace is
devise (2.1.2) app/controllers/devise_controller.rb:22:in
resource_name' app/controllers/registrations_controller.rb:104:in
authenticate_scope!'
It is apparently happening the first time devise_mapping is referenced. But as per the above stackoverflow question I have also explicitly provided helpers for devise_mapping, and they don't even get called.
Can anyone explain or help me figure out where to look further?
from routes.rb:
Fs::Application.routes.draw do
devise_for :users, :controllers => { :registrations => 'registrations' }
resources :users, :except => [:index, :show], :controller => 'registrations'
resources :users, :only => [:index, :show]
[...]
rake routes reports:
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) registrations#cancel
user_registration POST /users(.:format) registrations#create
new_user_registration GET /users/sign_up(.:format) registrations#new
edit_user_registration GET /users/edit(.:format) registrations#edit
PUT /users(.:format) registrations#update
DELETE /users(.:format) registrations#destroy
users POST /users(.:format) registrations#create
new_user GET /users/new(.:format) registrations#new
edit_user GET /users/:id/edit(.:format) registrations#edit
user PUT /users/:id(.:format) registrations#update
DELETE /users/:id(.:format) registrations#destroy
GET /users(.:format) users#index
GET /users/:id(.:format) users#show
my registrations_controller.rb:
class RegistrationsController < Devise::RegistrationsController
prepend_before_filter :authenticate_scope!, :except => [:index, :show]
skip_filter :require_no_authentication
end
my users_helper.rb:
module UsersHelper
# used by devise
def resource_name
:user
end
def resource
#resource ||= User.new
end
def devise_mapping
#devise_mapping ||= Devise.mappings[:user]
end
end
using:
Rails 3.2.6
Ruby 1.9.3
devise 2.1.2
Update: pasted in the correct trace output.
So one solution appears to be to add
devise_scope :user do
get '/users/new' => 'registrations#new'
end
into routes.rb. This may not be the best answer but it works.

Devise with restful user

I have devise and users controller
routes.rb
devise_for :users
resources :users do
resources :blogs
end
users_controller.rb
class UsersController < ApplicationController
respond_to :html, :xml, :json
def create
#user = User.create(params[:user])
end
def show
#user = User.find(params[:id])
#asset = Asset.find(params[:id])
respond_with [#user, #asset]
end
def update
#user = User.find(params[:id])
#user.update_attributes(params[:user])
respond_with #user
end
end
when I visit http://localhost:3000/users/sign_out
I get
ActiveRecord::RecordNotFound in UsersController#show
Couldn't find User with id=sign_out
update:
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
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
Any solution?
It happens because your session is destroyed by DELETE method.
From your rake routes:
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
And when you visit http://localhost:3000/users/sign_out, you use GET
You can change method in initializers/devise.rb. Just set config.sign_out_via to :get
Your routes.rb file and your generated routes don't appear to match. Routes are evaluated in order, and the first that matches is served.
You need your devise routes before your custom user routes. Something like this...
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
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
I'd confirm that your routes file looks like what you posted, because it seems inconsistent.

Rails RoutingError: No route matches {:controller=>"sessions", :action=>"destroy"}

Yet when run rake:routes it appears to be there:
users GET /users(.:format) {:action=>"index", :controller=>"users"}
POST /users(.:format) {:action=>"create", :controller=>"users"}
new_user GET /users/new(.:format) {:action=>"new", :controller=>"users"}
edit_user GET /users/:id/edit(.:format) {:action=>"edit", :controller=>"users"}
user GET /users/:id(.:format) {:action=>"show", :controller=>"users"}
PUT /users/:id(.:format) {:action=>"update", :controller=>"users"}
DELETE /users/:id(.:format) {:action=>"destroy", :controller=>"users"}
sessions POST /sessions(.:format) {:action=>"create", :controller=>"sessions"}
new_session GET /sessions/new(.:format) {:action=>"new", :controller=>"sessions"}
session DELETE /sessions/:id(.:format) {:action=>"destroy", :controller=>"sessions"}
signup /signup(.:format) {:controller=>"users", :action=>"new"}
contact /contact(.:format) {:controller=>"pages", :action=>"contact"}
about /about(.:format) {:controller=>"pages", :action=>"about"}
help /help(.:format) {:controller=>"pages", :action=>"help"}
root /(.:format) {:controller=>"pages", :action=>"home"}
Here are the routes from routes.rb:
resources :users
resources :sessions, :only => [:new, :create, :destroy]
match '/signup', to: 'users#new'
match '/contact', to: 'pages#contact'
match '/about', to: 'pages#about'
match '/help', to: 'pages#help'
It's possible that you are not passing the :id param in your route, which is why the route is not matched, since :id is required:
session DELETE /sessions/:id(.:format) {:action=>"destroy", :controller=>"sessions"}
Note: The parentheses around the :format param mean that it is optional.
I got the same error as well.But the reason is the little mistake that in the view page I write
<%= form_for(:session,url:session_path) do |f| %>
which I less the last 's' of 'sessions'.
This looks like an error I was running into when running through http://ruby.railstutorial.org/, and it turned out that I had left a few things out of routes.rb. The addition of the resource route is accompanied by the following two additional routes:
match '/signin', :to => 'sessions#new'
match '/signout', :to => 'sessions#destroy'
It's hard to see because the first route in that group is already there, so I had just glossed over the group (several times) as already being there.
resources controller adds map to method
{:action=>"method", :controller=>"controller"}
when in your case the rails seemed to ask for explicitly the map as
{:controller=>“controller”, :action=>“method”}
The :controller is before :action
This also answered Noach's question why match '/signout', :to => 'sessions#destroy' has to exist, if you rake:routes you will see it added
{:controller=>“sessions”, :action=>“destroy”} while there is already a
{:action=>“destroy”, :controller=>“sessions”} added by resources sessions

devise wrongly routes site controller

I have a problem with devise 1.1.5 on rails 3.0.3.
I have a controller "site" with an action "home". When I do a "rake routes" everything is as it should be, but when I click in my site on the sign_up link, it returns an error:
No route matches {:action=>"home", :controller=>"devise/site"}
This is correct the controller should be "site", not "devise/site".
This is in my routes.rb:
resources :articles
get "site/home"
get "site/about"
devise_for :users
But when I look with "rake routes" everything looks fine. Any ideas? Thanks!
articles GET /articles(.:format) {:action=>"index", :controller=>"articles"}
POST /articles(.:format) {:action=>"create", :controller=>"articles"}
new_article GET /articles/new(.:format) {:action=>"new", :controller=>"articles"}
edit_article GET /articles/:id/edit(.:format) {:action=>"edit", :controller=>"articles"}
article GET /articles/:id(.:format) {:action=>"show", :controller=>"articles"}
PUT /articles/:id(.:format) {:action=>"update", :controller=>"articles"}
DELETE /articles/:id(.:format) {:action=>"destroy", :controller=>"articles"}
site_home GET /site/home(.:format) {:action=>"home", :controller=>"site"}
site_about GET /site/about(.:format) {:action=>"about", :controller=>"site"}
new_user_session GET /users/sign_in(.:format) {:action=>"new", :controller=>"devise/sessions"}
user_session POST /users/sign_in(.:format) {:action=>"create", :controller=>"devise/sessions"}
destroy_user_session GET /users/sign_out(.:format) {:action=>"destroy", :controller=>"devise/sessions"}
user_password POST /users/password(.:format) {:action=>"create", :controller=>"devise/passwords"}
new_user_password GET /users/password/new(.:format) {:action=>"new", :controller=>"devise/passwords"}
edit_user_password GET /users/password/edit(.:format) {:action=>"edit", :controller=>"devise/passwords"}
PUT /users/password(.:format) {:action=>"update", :controller=>"devise/passwords"}
user_registration POST /users(.:format) {:action=>"create", :controller=>"devise/registrations"}
new_user_registration GET /users/sign_up(.:format) {:action=>"new", :controller=>"devise/registrations"}
edit_user_registration GET /users/edit(.:format) {:action=>"edit", :controller=>"devise/registrations"}
PUT /users(.:format) {:action=>"update", :controller=>"devise/registrations"}
DELETE /users(.:format) {:action=>"destroy", :controller=>"devise/registrations"}
root /(.:format) {:action=>"home", :controller=>"site"}
What is the code for your sign up link?
Try changing your routes.rb to (I think the way 'home' and 'about' are currently describe might be causing the error):
devise_for :users
resources :sites do
get :home, :on => :member
get :about, :on => :member
end
resources :articles
also remember to set your root path so that when user does get signed-in they are directed to correct view.