I need some help here.
I'm trying to eliminate the route paths destroy and cancel from the Devise registerable module but I haven't been succeed.
Route.rb
devise_for :users, :skip => [:registrations], controllers: {
sessions: 'users/sessions',
confirmations: 'users/confirmations',
passwords: 'users/passwords'
}
get '/users/sign_up', to: 'users/registrations#new'
get '/users/edit', to: 'users/registrations#edit'
post '/users', to: 'users/registrations#create'
put '/users', to: 'users/registrations#update'
Model.rb
devise :database_authenticatable, :registerable,
:recoverable, :trackable, :validatable, :confirmable
Rails route
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
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
As you can see in Rails routes, Devise is creating the route paths devise/registrations even when set the option :skip up for the devise_for method. Someone already face this problem?
Thanks in advance.
Along with skip also add skip_helpers: true.
skip_helpers: skip generating Devise url helpers like new_session_path(#user).
This is useful to avoid conflicts with previous routes and is false by default.
It accepts true as option, meaning it will skip all the helpers for the controllers
given in :skip but it also accepts specific helpers to be skipped:
devise_for :users, skip: [:registrations, :confirmations], skip_helpers: true
devise_for :users, skip_helpers: [:registrations, :confirmations]
Source:
From devise Documentation
https://github.com/plataformatec/devise/blob/master/lib/devise/rails/routes.rb#L160
Related
I am trying to override the passwords controller in Devise and when changing the devise_for :users routes in my routes.rb file the routes are not being populated when running rails routes.
I have a file called users/passwords_controller.rb with a puts in there as an example.
class User::PasswordsController < Devise::PasswordsController
def create
puts 'creating user profile'
super
end
end
Here is the route defination.
devise_for :users, controllers: {
sessions: 'user/sessions',
passwords: 'user/passwords',
registrations: 'user/registrations'
}
The rails routes command returns these and only these devise routes.
new_user_session GET /users/sign_in(.:format) user/sessions#new
user_session POST /users/sign_in(.:format) user/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) user/sessions#destroy
cancel_user_registration GET /users/cancel(.:format) user/registrations#cancel
new_user_registration GET /users/sign_up(.:format) user/registrations#new
edit_user_registration GET /users/edit(.:format) user/registrations#edit
user_registration PATCH /users(.:format) user/registrations#update
PUT /users(.:format) user/registrations#update
DELETE /users(.:format) user/registrations#destroy
POST /users(.:format) user/registrations#create
When running rails routes I would expect to see the passwords routes, however they are not there and this leads to 404's when trying to hit these resources. I am unable to find any documentation about this behavior anywhere. I am currently running Ruby version 3.1.2, Rails version 7.0.4.2, and Devise version 4.8.1.
I have looked into this post, however it does not describe my problem and is over 10 years old at this point.
Make sure you have :recoverable in you User model:
class User < ApplicationRecord
devise :database_authenticatable, :recoverable # there's also :trackable, :validatable, :confirmable
end
This enables the passwords routes.
Rails 5.1
Devise
My routes.rb file:
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
devise_for :users, controllers: {
sessions: 'users/sessions',
confirmations: 'users/confirmations',
passwords: 'users/passwords',
registrations: 'users/registrations',
unlocks: 'users/unlocks',
invitations: 'users/invitations'
}
root to: "landings#index"
resources :users
resources :followeds
resources :followers
resources :locations
resources :fw_exports
get 'import_spreadsheet', to: :upload_spreadsheet, controller: 'fw_exports'
post 'parse_imported_spreadsheet_and_confirm', to: :process_imported_spreadsheet_and_confirm, controller: 'fw_exports'
post 'process_parsed_spreadsheet', to: :process_parsed_spreadsheet, controller: 'fw_exports'
scope module: :landings do
get 'visitors'
end
end
When I generate routes, here's what I get:
Prefix Verb URI Pattern Controller#Action
new_user_session GET /users/sign_in(.:format) users/sessions#new
user_session POST /users/sign_in(.:format) users/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) users/sessions#destroy
new_user_password GET /users/password/new(.:format) users/passwords#new
edit_user_password GET /users/password/edit(.:format) users/passwords#edit
user_password PATCH /users/password(.:format) users/passwords#update
PUT /users/password(.:format) users/passwords#update
POST /users/password(.:format) users/passwords#create
cancel_user_registration GET /users/cancel(.:format) users/registrations#cancel
new_user_registration GET /users/sign_up(.:format) users/registrations#new
edit_user_registration GET /users/edit(.:format) users/registrations#edit
user_registration PATCH /users(.:format) users/registrations#update
PUT /users(.:format) users/registrations#update
DELETE /users(.:format) users/registrations#destroy
POST /users(.:format) users/registrations#create
accept_user_invitation GET /users/invitation/accept(.:format) users/invitations#edit
remove_user_invitation GET /users/invitation/remove(.:format) users/invitations#destroy
new_user_invitation GET /users/invitation/new(.:format) users/invitations#new
user_invitation PATCH /users/invitation(.:format) users/invitations#update
PUT /users/invitation(.:format) users/invitations#update
POST /users/invitation(.:format) users/invitations#create
root GET / landings#index
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
followeds GET /followeds(.:format) followeds#index
POST /followeds(.:format) followeds#create
new_followed GET /followeds/new(.:format) followeds#new
edit_followed GET /followeds/:id/edit(.:format) followeds#edit
followed GET /followeds/:id(.:format) followeds#show
PATCH /followeds/:id(.:format) followeds#update
PUT /followeds/:id(.:format) followeds#update
DELETE /followeds/:id(.:format) followeds#destroy
followers GET /followers(.:format) followers#index
POST /followers(.:format) followers#create
new_follower GET /followers/new(.:format) followers#new
edit_follower GET /followers/:id/edit(.:format) followers#edit
follower GET /followers/:id(.:format) followers#show
PATCH /followers/:id(.:format) followers#update
PUT /followers/:id(.:format) followers#update
DELETE /followers/:id(.:format) followers#destroy
locations GET /locations(.:format) locations#index
POST /locations(.:format) locations#create
new_location GET /locations/new(.:format) locations#new
edit_location GET /locations/:id/edit(.:format) locations#edit
location GET /locations/:id(.:format) locations#show
PATCH /locations/:id(.:format) locations#update
PUT /locations/:id(.:format) locations#update
DELETE /locations/:id(.:format) locations#destroy
fw_exports GET /fw_exports(.:format) fw_exports#index
POST /fw_exports(.:format) fw_exports#create
new_fw_export GET /fw_exports/new(.:format) fw_exports#new
edit_fw_export GET /fw_exports/:id/edit(.:format) fw_exports#edit
fw_export GET /fw_exports/:id(.:format) fw_exports#show
PATCH /fw_exports/:id(.:format) fw_exports#update
PUT /fw_exports/:id(.:format) fw_exports#update
DELETE /fw_exports/:id(.:format) fw_exports#destroy
import_spreadsheet GET /import_spreadsheet(.:format) fw_exports#import_spreadsheet
parse_imported_spreadsheet_and_confirm POST /parse_imported_spreadsheet_and_confirm(.:format) fw_exports#parse_imported_spreadsheet_and_confirm
process_parsed_spreadsheet POST /process_parsed_spreadsheet(.:format) fw_exports#process_parsed_spreadsheet
visitors GET /visitors(.:format) landings#visitors
Am I not supposed to also be getting routes for confirmatins and unlocks?
The problem is you have commented the lockable & confirmable. You need to uncomment it inorder to make it work.
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :confirmable, :lockable
end
From my rake routes:
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
So when I click submit button in registration form it moves me to /users.
How can I change it so user_registration POST would be /users/sign_up(.:format) ?
I tried something like this:
devise_for :users
as :user do
post 'sign_up' => 'devise/registrations#create', as: 'user_registration'
end
But there is conflict as user_registration prefix is already generated by devise_for
To do what you want, you need to stop generating default registrations#create action. Unfortunately there is no easy way to do so (or customize it). The best approach I can find is to skip generating registration routes for users and define all of them later with devise_scope method:
devise_for :users, skip: :registration
devise_scope :user do
resource :registration, :as => :user_registration, :only => [ :new, :edit, :update, :destroy ], :path=> "/users", :path_names=> { :new =>"sign_up" }, :controller=>"devise/registrations" do
get :cancel
post :sign_up, action: :create, as: ''
end
end
This could be probably cleaned up a bit but it generates what you expect:
user_registration POST /users/sign_up(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
P.S. related problem is discussed in this thread
I'm following along with a Railscast http://railscasts.com/episodes/235-devise-and-omniauth-revised?view=comments about implementing omniauth into devise, specifically adding Twitter signin. When I add the link to sign into Twitter to my app, I'm getting this error visiting the homepage
<li><%= link_to "Sign In With Twitter", user_omniauth_authorize_path(:provider) %></li>
No route matches {:controller=>"omniauth_callbacks", :action=>"passthru", :provider=>:provider}
If I do rake routes (see below), it shows that I have 'user_omniauth_callback' route, but it doesn't specify a request type (GET, POST) etc, which I'm not sure is significant or not.
I searched this error on SO, and one person had a similar problem that resulted from not including the following code in routes.rb, but I have it included.
devise_for :users, path_names: {sign_in: "login", sign_out: "logout"},
controllers: {omniauth_callbacks: "omniauth_callbacks"}
The error arose before getting to the part of the episode where he adds the omniauth controller; I added it just to be sure but I'm still getting the same error:
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
def all
request.env["omniauth.auth"]
end
alias_method :twitter, :all
end
I'm not really sure what else I can look at. In the devise initializer, I set up config for Twitter
config.omniauth :twitter, ENV["TWITTER_KEY"], ENV["TWITTER_SECRET"]
This is my user model
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :omniauthable,
:recoverable, :rememberable, :trackable, :validatable
attr_accessible :email, :password, :password_confirmation, :remember_me, :name, :image, :provider, :uid
validates_uniqueness_of :name, :email, :case_sensitive => false
end
Can you make any suggestions about what I might do to fix this problem?
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_omniauth_authorize /users/auth/:provider(.:format) devise/omniauth_callbacks#passthru {:provider=>/twitter/}
user_omniauth_callback /users/auth/:action/callback(.:format) devise/omniauth_callbacks#(?-mix:twitter)
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
The mistake was that I was passing :provider rather than :twitter as an argument in the link.
Incorrect
<%= link_to "Sign In With Twitter", user_omniauth_authorize_path(:provider) %>
Correct:
<%= link_to "Sign In With Twitter", user_omniauth_authorize_path(:twitter) %>
I'm coming from Sinatra to Rails and still quite new to Rails. My problem is that after I create a User account I am just directed to the index.html page in the /public folder and can't seem to access any other routes, I can't sign_out the user and I can't add another user.
I am using the devise gem to manage my user model and authentication. After installing the gem I followed the instructions on the devise github page.
ie:
rails generate devise:install
I also added to the 'config/environments/development.rb' file
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
and I added to the 'config/routes.rb' file
root :to => "home#index"
and I added to the 'app/views/layouts/application.html.erb' file
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>
Then I ran
rails generate devise User
And finally
rake db:migrate
Here is my user model
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
# attr_accessible :title, :body
end
Then I navigated to "/users/sign_up" and I entered an email and password after which I was redirected to the index.html page in the public folder.
The problem is I just seem to be stuck there. '/users/sign_out' yields
Routing Error
No route matches [GET] "/users/sign_out"
Try running rake routes for more information on available routes.
And running 'rake routes' yields
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
root / home#index
Seems like '/users/sign_up' should be a working route.
I am wondering if the problem is that there is no email service setup and this account is trying to be validated trough email? If so, how do I disable that?
Thanks! And let me know if you need more information or for me to clarify something.
===============UPDATE===================
The 'users/edit' route does work and I think possibly the problem lies in the fact that the route that is setup for 'users/sign_out' is a DELETE route. I forget the terminology about this, but I know that there is some sort of trickery in making a DELETE route out of a GET route. So is this where my problem lies?
You can do one of two things here.
Create a link to do the logout, something like this:
<%= link_to "Logout", destroy_user_session_path, method: :delete %>
Or add an additional route like this to your routes.rb file:
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
get 'signout' => 'devise/sessions#destroy', :as => :destroy_user_session
end
The above example is from https://github.com/plataformatec/devise/wiki/How-To:-Change-the-default-sign_in-and-sign_out-routes
To sign out, you should be calling:
destroy_user_session_path
Devise stores sessions, and DELETE just removes your session.
For the routing error, can you post up your HomeController?
And for the edit, are you calling:
edit_user_registration_path
A few moments ago I was having this problem and instead of looking for sites and more sites, it was when i stop here and one of the codes shown above is also shown below worked fine for me thanks
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
get 'signout' => 'devise/sessions#destroy', :as => :destroy_user_session
end