I used Devise 3.0, everything was ok, but since i use Devise 3.2.4 i got lot of problem with my routes...
With Devise 3.0, this code worked :
devise_for :users,
:controllers => { :registrations => "users/registrations",
:passwords => "users/passwords",
:sessions => 'users/sessions'},
:skip => [:sessions] do
get 'identification' => 'users/sessions#new', :as => :new_user_session
post 'identification' => 'users/sessions#create', :as => :user_session
get 'deconnexion' => 'users/sessions#destroy', :as => :destroy_user_session
get 'inscription' => 'users/registrations#new', :as => :new_user_registration
get 'mot-de-passe' => "users/passwords#new", :as => :new_password_user
get 'mon-compte' => "users/registrations#edit", :as => :edit_user_registration
end
devise_scope :user do
get "users/pdf", :controller => "users/registrations", :action => "pdf"
get "users/download_pdf", :controller => "users/registrations", :action => "download_pdf"
get "informations-personnelles", :controller => "users/registrations", :action => "edit_profile0", :as => "users_edit_profile0"
get "vous-et-votre-projet", :controller => "users/registrations", :action => "edit_profile1", :as => "users_edit_profile1"
get "caracteristiques-du-bien", :controller => "users/registrations", :action => "edit_profile2", :as => "users_edit_profile2"
get "details-du-bien", :controller => "users/registrations", :action => "edit_profile3", :as => "users_edit_profile3"
get "quartier", :controller => "users/registrations", :action => "edit_profile4", :as => "users_edit_profile4"
get "lieu", :controller => "users/registrations", :action => "edit_profile5", :as => "users_edit_profile5"
get "budget", :controller => "users/registrations", :action => "edit_profile6", :as => "users_edit_profile6"
get "coup-de-coeur", :controller => "users/registrations", :action => "edit_profile7", :as => "users_edit_profile7"
put "users/update_avatar", :controller => "users/registrations", :action => "update_avatar"
put "users/update_profile0", :controller => "users/registrations", :action => "update_profile0"
put "users/update_profile1", :controller => "users/registrations", :action => "update_profile1"
put "users/update_profile2", :controller => "users/registrations", :action => "update_profile2"
put "users/update_profile3", :controller => "users/registrations", :action => "update_profile3"
put "users/update_profile4", :controller => "users/registrations", :action => "update_profile4"
put "users/update_profile5", :controller => "users/registrations", :action => "update_profile5"
put "users/update_profile6", :controller => "users/registrations", :action => "update_profile6"
put "users/update_profile7", :controller => "users/registrations", :action => "update_profile7"
get "users/heating", :controller => "users/registrations", :action => "heating", :as => :heating
get "users/water_heater", :controller => "users/registrations", :action => "water_heater", :as => :water_heater
end
end
Since i use Devise 3.2.4 i got some problems like :
undefined local variable or method `destroy_user_session_path'
Or
undefined local variable or method `new_user_session_path'
I worked few hours on that problem but i cant find the good solution...
The result of rake routes is :
Prefix Verb URI Pattern Controller#Action
process_webhook POST /webhooks/process(.:format) webhooks#handle
verify_webhook GET /webhooks/process(.:format) webhooks#verify
reset_webhooks DELETE /webhooks/reset(.:format) webhooks#destroy_all
webhooks GET /webhooks(.:format) webhooks#index
POST /webhooks(.:format) webhooks#create
invitations GET /invitations(.:format) invitations#index
POST /invitations(.:format) invitations#create
new_invitation GET /invitations/new(.:format) invitations#new
edit_invitation GET /invitations/:id/edit(.:format) invitations#edit
invitation GET /invitations/:id(.:format) invitations#show
PATCH /invitations/:id(.:format) invitations#update
PUT /invitations/:id(.:format) invitations#update
DELETE /invitations/:id(.:format) invitations#destroy
authenticated_user_root GET / messages#index
authenticated_realtor_root GET / pro/users#index
unauthenticated_root GET / static_pages#home
new_customer GET /customer/new(.:format) customer#new
edit_customer GET /customer/:id/edit(.:format) customer#edit
edit_credit_card_info GET /credit_card_info/:id/edit(.:format) credit_card_info#edit
confirm_customer GET /customer/confirm(.:format) customer#confirm
confirm_credit_card_info GET /credit_card_info/confirm(.:format) credit_card_info#confirm
new_transaction GET /transactions/:product_id/new(.:format) transactions#new
confirm_transaction GET /transactions/confirm/:product_id(.:format) transactions#confirm
update_areas GET /update_areas(.:format) areas#update_areas
about_users GET /a-propos(.:format) static_pages#about
about_realtors GET /pro/a-propos(.:format) static_pages#about_realtors
how_it_works_users GET /comment-ca-marche(.:format) static_pages#how_it_works
home_realtors GET /pro(.:format) static_pages#home_realtors
how_it_works_realtors GET /pro/comment-ca-marche(.:format) static_pages#how_it_works_realtors
pricing_realtors GET /pro/abonnements(.:format) static_pages#pricing
press_users GET /espace-presse(.:format) static_pages#press
press_realtors GET /pro/espace-presse(.:format) static_pages#press_realtors
terms_users GET /conditions-utilisation(.:format) static_pages#terms
terms_realtors GET /pro/conditions-utilisation(.:format) static_pages#terms_realtors
legals_users GET /mentions-legales(.:format) static_pages#legals
legals_realtors GET /pro/mentions-legales(.:format) static_pages#legals_realtors
static_pages_home GET /static_pages/home(.:format) static_pages#home
static_pages_user_mailer GET /static_pages/user_mailer(.:format) static_pages#user_mailer
pro_users_show_partial GET /pro/users/show_partial(.:format) pro/users#show_partial
pro_users_show_complete GET /pro/users/show_complete(.:format) pro/users#show_complete
pro_search_users GET /pro/users/search_users(.:format) pro/users#search_users
load_user_profile GET /pro/users/load_user_profile/:id(.:format) pro/users#load_user_profile
new_map GET /pro/users/new_map(.:format) pro/users#new_map
new_contact GET /pro/users/new_contact/:id(.:format) pro/messages#new_contact
realtors_show GET /realtors/show(.:format) realtors#show
root GET / static_pages#home
list_cities_areas GET /areas/list_cities(.:format) areas#list_cities
list_depts_areas GET /areas/list_depts(.:format) areas#list_depts
city_areas GET /areas/city(.:format) areas#city
dept_areas GET /areas/dept(.:format) areas#dept
GET /areas/kml/:hash(.:format) areas#kml
areas GET /areas(.:format) areas#index
POST /areas(.:format) areas#create
new_area GET /areas/new(.:format) areas#new
edit_area GET /areas/:id/edit(.:format) areas#edit
area GET /areas/:id(.:format) areas#show
PATCH /areas/:id(.:format) areas#update
PUT /areas/:id(.:format) areas#update
DELETE /areas/:id(.:format) areas#destroy
list_children_children GET /children/list_children(.:format) children#list_children
add_child_children GET /children/add_child(.:format) children#add_child
children GET /children(.:format) children#index
POST /children(.:format) children#create
new_child GET /children/new(.:format) children#new
edit_child GET /children/:id/edit(.:format) children#edit
child GET /children/:id(.:format) children#show
PATCH /children/:id(.:format) children#update
PUT /children/:id(.:format) children#update
DELETE /children/:id(.:format) children#destroy
messages GET /messages(.:format) messages#index
POST /messages(.:format) messages#create
new_message GET /messages/new(.:format) messages#new
edit_message GET /messages/:id/edit(.:format) messages#edit
message GET /messages/:id(.:format) messages#show
PATCH /messages/:id(.:format) messages#update
PUT /messages/:id(.:format) messages#update
DELETE /messages/:id(.:format) messages#destroy
load_discussion GET /messages/load_discussion/:id(.:format) messages#load_discussion
faye_alive GET /messages/faye_alive/:id(.:format) messages#faye_alive
GET /invitations(.:format) invitations#index
POST /invitations(.:format) invitations#create
GET /invitations/new(.:format) invitations#new
GET /invitations/:id/edit(.:format) invitations#edit
GET /invitations/:id(.:format) invitations#show
PATCH /invitations/:id(.:format) invitations#update
PUT /invitations/:id(.:format) invitations#update
DELETE /invitations/:id(.:format) invitations#destroy
load_invitation GET /invitations/load_invitation/:id(.:format) invitations#load_invitation
pro_messages GET /pro/messages(.:format) pro/messages#index
POST /pro/messages(.:format) pro/messages#create
new_pro_message GET /pro/messages/new(.:format) pro/messages#new
edit_pro_message GET /pro/messages/:id/edit(.:format) pro/messages#edit
pro_message GET /pro/messages/:id(.:format) pro/messages#show
PATCH /pro/messages/:id(.:format) pro/messages#update
PUT /pro/messages/:id(.:format) pro/messages#update
DELETE /pro/messages/:id(.:format) pro/messages#destroy
pro_discussion_user GET /pro/discussion/:id(.:format) pro/messages#index
pro_load_discussion GET /pro/messages/load_discussion/:id(.:format) pro/messages#load_discussion
pro_users GET /pro/users(.:format) pro/users#index
POST /pro/users(.:format) pro/users#create
new_pro_user GET /pro/users/new(.:format) pro/users#new
edit_pro_user GET /pro/users/:id/edit(.:format) pro/users#edit
pro_user GET /pro/users/:id(.:format) pro/users#show
PATCH /pro/users/:id(.:format) pro/users#update
PUT /pro/users/:id(.:format) pro/users#update
DELETE /pro/users/:id(.:format) pro/users#destroy
pro_list_users GET /pro/acheteurs(.:format) pro/users#index
pro_this_user GET /pro/acheteur/:id(.:format) pro/users#show
pro GET /pro/users/download_pdf/:id(.:format) pro/users#download_pdf
realtor_password POST /realtors/password(.:format) realtors/passwords#create
new_realtor_password GET /realtors/password/new(.:format) realtors/passwords#new
edit_realtor_password GET /realtors/password/edit(.:format) realtors/passwords#edit
PATCH /realtors/password(.:format) realtors/passwords#update
PUT /realtors/password(.:format) realtors/passwords#update
cancel_realtor_registration GET /realtors/cancel(.:format) realtors/registrations#cancel
realtor_registration POST /realtors(.:format) realtors/registrations#create
new_realtor_registration GET /realtors/sign_up(.:format) realtors/registrations#new
edit_realtor_registration GET /realtors/edit(.:format) realtors/registrations#edit
PATCH /realtors(.:format) realtors/registrations#update
PUT /realtors(.:format) realtors/registrations#update
DELETE /realtors(.:format) realtors/registrations#destroy
realtors_credit_card_info GET /realtors/credit_card_info(.:format) realtors/registrations#credit_card_info
realtors_cancel_subscription GET /realtors/cancel_subscription(.:format) realtors/registrations#cancel_subscription
realtors_edit_profile0 GET /pro/informations-personnelles(.:format) realtors/registrations#edit_profile0
realtors_edit_profile1 GET /pro/zone-activite(.:format) realtors/registrations#edit_profile1
realtors_edit_profile GET /realtors/edit_profile(.:format) realtors/registrations#edit_profile
realtors_update_profile1 PUT /realtors/update_profile1(.:format) realtors/registrations#update_profile1
realtors_update_profile0 PUT /realtors/update_profile0(.:format) realtors/registrations#update_profile0
realtors_update_avatar PUT /realtors/update_avatar(.:format) realtors/registrations#update_avatar
realtors_update_logo PUT /realtors/update_logo(.:format) realtors/registrations#update_logo
user_password POST /users/password(.:format) users/passwords#create
new_user_password GET /users/password/new(.:format) users/passwords#new
edit_user_password GET /users/password/edit(.:format) users/passwords#edit
PATCH /users/password(.:format) users/passwords#update
PUT /users/password(.:format) users/passwords#update
cancel_user_registration GET /users/cancel(.:format) users/registrations#cancel
user_registration POST /users(.:format) users/registrations#create
new_user_registration GET /users/sign_up(.:format) users/registrations#new
edit_user_registration GET /users/edit(.:format) users/registrations#edit
PATCH /users(.:format) users/registrations#update
PUT /users(.:format) users/registrations#update
DELETE /users(.:format) users/registrations#destroy
users_pdf GET /users/pdf(.:format) users/registrations#pdf
users_download_pdf GET /users/download_pdf(.:format) users/registrations#download_pdf
users_edit_profile0 GET /informations-personnelles(.:format) users/registrations#edit_profile0
users_edit_profile1 GET /vous-et-votre-projet(.:format) users/registrations#edit_profile1
users_edit_profile2 GET /caracteristiques-du-bien(.:format) users/registrations#edit_profile2
users_edit_profile3 GET /details-du-bien(.:format) users/registrations#edit_profile3
users_edit_profile4 GET /quartier(.:format) users/registrations#edit_profile4
users_edit_profile5 GET /lieu(.:format) users/registrations#edit_profile5
users_edit_profile6 GET /budget(.:format) users/registrations#edit_profile6
users_edit_profile7 GET /coup-de-coeur(.:format) users/registrations#edit_profile7
users_update_avatar PUT /users/update_avatar(.:format) users/registrations#update_avatar
users_update_profile0 PUT /users/update_profile0(.:format) users/registrations#update_profile0
users_update_profile1 PUT /users/update_profile1(.:format) users/registrations#update_profile1
users_update_profile2 PUT /users/update_profile2(.:format) users/registrations#update_profile2
users_update_profile3 PUT /users/update_profile3(.:format) users/registrations#update_profile3
users_update_profile4 PUT /users/update_profile4(.:format) users/registrations#update_profile4
users_update_profile5 PUT /users/update_profile5(.:format) users/registrations#update_profile5
users_update_profile6 PUT /users/update_profile6(.:format) users/registrations#update_profile6
users_update_profile7 PUT /users/update_profile7(.:format) users/registrations#update_profile7
users_edit_delete GET /users/edit_delete(.:format) users/registrations#delete
heating GET /users/heating(.:format) users/registrations#heating
water_heater GET /users/water_heater(.:format) users/registrations#water_heater
Someone can help me?
Passing the block to devise_for is deprecated in newer versions of Devise. This is why the routes that you passed to devise_for in a block are totally ignored. You can notice this in the rake routes result as well, those routes are nowhere to be seen.
And as the routes itself are not existing, you get the error when you access destroy_user_session_path or new_user_session_path.
To resolve this, you need to add those routes in devise_scope instead:
## You need to skip `:registrations` and `:passwords` routes too because you are resetting them as well
devise_for :users,
:controllers => { :registrations => "users/registrations",
:passwords => "users/passwords",
:sessions => 'users/sessions'},
:skip => [:sessions, :registrations, :passwords]
devise_scope :user do
get 'identification' => 'users/sessions#new', :as => :new_user_session
post 'identification' => 'users/sessions#create', :as => :user_session
get 'deconnexion' => 'users/sessions#destroy', :as => :destroy_user_session
get 'inscription' => 'users/registrations#new', :as => :new_user_registration
get 'mot-de-passe' => "users/passwords#new", :as => :new_password_user
get 'mon-compte' => "users/registrations#edit", :as => :edit_user_registration
## Rest of the routes
end
Checkout the Github Devise Issue: devise_for block deprecation warning confuses for your reference.
Related
I've got a Rails 4 app that I'm trying to reconfigure to use subdomains for jobportals. Right now the root path for each portal is /jobportals/:id. What I want is for a user to be able to go to client.example.com and hit that root path. Then for example, if the user edits their profile, the url would be client.example.com/user/edit rather than www.example.com/jobportals/:id/user/edit.
I followed this ASCIIcasts tutorial on setting up subdomain routing, but it's not working for me. When I go to http://client.lvh.me:3000/, I am hitting the root_path of my Rails app, rather than the root_path for the portal.
I think the problem is that Constraints(Subdomain) isn't working with resources :jobportals. How can I reconfigure my routes to accomplish what I'm after?
MyApp::Application.routes.draw do
devise_for :users, :controllers => {:registrations => "registrations"}
root 'general#home'
get '/about' => 'general#about'
get '/team' => 'general#team'
get '/careers' => 'general#careers'
get '/terms' => 'general#terms'
get '/privacy_policy' => 'general#privacy_policy'
constraints(Subdomain) do
resources :jobportals, controller: 'portals/general' do
member do
root 'portals/general#home'
devise_scope :user do
get '/user/sign_in' => 'portals/sessions#new'
post '/user/sign_in' => 'portals/sessions#create'
delete '/user/sign_out' => 'portals/sessions#destroy'
post '/user/password' => 'portals/passwords#create'
get '/user/password/new' => 'portals/passwords#new'
get '/user/password/edit' => 'portals/passwords#edit'
patch '/user/password' => 'portals/passwords#update'
put '/user/password' => 'portals/passwords#update'
post '/user' => 'portals/registrations#create'
get '/user/sign_up' => 'portals/registrations#new'
get '/user/edit' => 'portals/registrations#edit'
patch '/user' => 'portals/registrations#update'
put '/user' => 'portals/registrations#update'
delete '/user' => 'portals/registrations#destroy'
end
get '/jobs' => 'portals/general#jobs'
get '/companies' => 'portals/general#companies'
get '/alljobs' => 'portals/general#alljobs'
resources :applications, controller: 'portals/applications'
get ':id' => 'portals/companies#profile'
get ':id/jobs' => 'portals/companies#jobs'
get ':id/jobfunctions' => 'portals/companies#jobfunctions'
end
end
end
end
Working Code Below
MyApp::Application.routes.draw do
devise_for :users, :controllers => {:registrations => "registrations"}
root 'general#home'
get '/about' => 'general#about'
get '/team' => 'general#team'
get '/careers' => 'general#careers'
get '/terms' => 'general#terms'
get '/privacy_policy' => 'general#privacy_policy'
constraints(Subdomain) do
get '/' => 'portals/general#home'
devise_scope :user do
get '/user/sign_in' => 'portals/sessions#new'
post '/user/sign_in' => 'portals/sessions#create'
delete '/user/sign_out' => 'portals/sessions#destroy'
post '/user/password' => 'portals/passwords#create'
get '/user/password/new' => 'portals/passwords#new'
get '/user/password/edit' => 'portals/passwords#edit'
patch '/user/password' => 'portals/passwords#update'
put '/user/password' => 'portals/passwords#update'
post '/user' => 'portals/registrations#create'
get '/user/sign_up' => 'portals/registrations#new'
get '/user/edit' => 'portals/registrations#edit'
patch '/user' => 'portals/registrations#update'
put '/user' => 'portals/registrations#update'
delete '/user' => 'portals/registrations#destroy'
end
get '/jobs' => 'portals/general#jobs'
get '/companies' => 'portals/general#companies'
get '/alljobs' => 'portals/general#alljobs'
resources :applications, controller: 'portals/applications'
get ':id' => 'portals/companies#profile'
get ':id/jobs' => 'portals/companies#jobs'
get ':id/jobfunctions' => 'portals/companies#jobfunctions'
end
end
It is looking like constraints are not define properly, it is something like get 'jobportals', constraints: {subdomain: 'subdomain_name'}
Checkout the rails guide
I'm really missing something here.
I have read many things about devise redirects (seems like it is hard to implement for most people ...) but in my case I really don't get it.
Sometimes I read that the methods after_<action>_path_for(resource) should be in the ApplicationController, sometimes it is mentionned to be in a specific controller, overriding the devise one.
I'd rather have them in my ApplicationController since it bothers me to create more Controllers just for a redirect, but if it is not possible at the end, I won't insist...
Here's the deal:
I have in my ApplicationController : (and some others, but that's enough for the example)
def after_update_path_for(user)
flash[:notice] = 'Successfully updated password'
edit_user_path(user)
end
def after_inactive_sign_up_path_for(user)
flash[:notice] = 'Welcome! Please follow the steps!'
me_new_path
end
def after_sign_up_path_for(user)
flash[:notice] = 'Welcome! Please follow the steps!'
me_new_path
end
def after_sign_in_path_for(user)
if user.sign_in_count == 1
me_new_path
else
root_path
end
end
And the crazy thing, is that after_sign_in_path_for is called, but not the other ones. Like when the user signs up it's the if user.sign_in_count == 1 that redirects him, not the after_inactive_sign_up_path_for nor the after_sign_up_path_for
How come?
It could be related to my routes, so here's my routes.rb extract:
devise_for :user, :skip => [:sessions, :registrations], :path => ''
devise_scope :user do
get :register, :to => 'devise/registrations#new'
post :register, :to => 'devise/registrations#create'
put :update_password, :to => 'devise/my_registrations#update'
get :login, :to => 'devise/sessions#new'
get :login, :to => 'devise/sessions#new', :as => :new_copasser_session
post :login, :to => 'devise/sessions#create'
delete :logout, :to => 'devise/sessions#destroy'
end
And I'm using Devise 3.1.0 with Ruby 1.9.3 and Rails 3.2.13
Thanks for the help!
EDIT
Thanks #rich-peck for your answer. I updated my routes.rb this way :
devise_for :users, :path => '', :path_names => {
:sign_in => :login,
:registration => :register,
:sign_up => '',
:sign_out => :logout
}
which gives me the same routes as the previous ones (except I can't use login_path helper anymore but it's not a big deal), but I still get the same results concerning redirects.
Here is the result of rake routes:
new_user_session GET /login(.:format) devise/sessions#new
user_session POST /login(.:format) devise/sessions#create
destroy_user_session DELETE /logout(.:format) devise/sessions#destroy
user_password POST /password(.:format) devise/passwords#create
new_user_password GET /password/new(.:format) devise/passwords#new
edit_user_password GET /password/edit(.:format) devise/passwords#edit
PUT /password(.:format) devise/passwords#update
cancel_user_registration GET /register/cancel(.:format) devise/registrations#cancel
user_registration POST /register(.:format) devise/registrations#create
new_user_registration GET /register(.:format) devise/registrations#new
edit_user_registration GET /register/edit(.:format) devise/registrations#edit
PUT /register(.:format) devise/registrations#update
DELETE /register(.:format) devise/registrations#destroy
user_confirmation POST /confirmation(.:format) devise/confirmations#create
new_user_confirmation GET /confirmation/new(.:format) devise/confirmations#new
GET /confirmation(.:format) devise/confirmations#show
Any idea?
Devise relies heavily on a central variable called "resource". This variable defines how devise operates on your system, and is why you have to "attach" Devise to :users or similar
People get problems with Devise because they don't follow the conventions, and put their forms everywhere. If they read the Devise readme, they'd appreciate that it's quite flexible :)
I believe your problem is to do with your routes, in that you might want to consolidate all those static routes into something like this:
devise_for :users, :path => '', :controllers => {:sessions => 'sessions', :registrations => 'registrations'}, :path_names => { :sign_in => 'login', :password => 'forgot', :confirmation => 'confirm', :unlock => 'unblock', :registration => 'register', :sign_up => 'new', :sign_out => 'logout'}
So thanks to #rich-peck's help, we figured it out.
The question was why do after_sign_in_path_for behave differently than after_sign_up_path_for and cie ?
It appears, in the devise sources, that after_sign_in_path_for is defined in a helper, whereas the other ones are methods of their controller (eg. Devise::RegistrationsController < DeviseController)
Hence for after_sign_in_path_for it works overriding in the ApplicationController, whereas for the other ones, it is necessary to create a registrations_controller.rb file, to override the method in it:
class RegistrationsController < Devise::RegistrationsController
protected
def after_sign_up_path_for(copasser)
flash[:notice] = 'Welcome! Please follow the steps!'
me_new_path
end
end
and to set the router.rb this way:
devise_for :copassers, :controllers => {
:registrations => :registrations
}
I suppose the behaviour is different because :registerable, :recoverable etc. are modules of devise, not necessarily used, and the helper wouldn't be appropriate in that case. A devise contributor could help us on this point.
I'm trying to setup Devise 3.1.0 with Rails 4.0.0.
I have configured my router like so:
devise_for :users,
:controllers => {
:registrations => 'users/registrations',
:sessions => 'users/sessions'
},
:path_names => {
:sign_in => 'login',
:sign_out => 'logout',
:sign_up => 'new'
}
new_user_session GET /users/login(.:format) users/sessions#new
user_session POST /users/login(.:format) users/sessions#create
destroy_user_session DELETE /users/logout(.:format) users/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) users/registrations#cancel
user_registration POST /users(.:format) users/registrations#create
new_user_registration GET /users/new(.:format) users/registrations#new
edit_user_registration GET /users/edit(.:format) users/registrations#edit
PATCH /users(.:format) users/registrations#update
PUT /users(.:format) users/registrations#update
DELETE /users(.:format) users/registrations#destroy
I've also turned on scoped views, and overridden the default views and registration controller:
# config/initializers/devise.rb
config.scoped_views = true
rails generate devise:views users
# app/controllers/users/registrations_controller.rb
#
# NOTE: I created this class, so creating new users could only be done by authenticated users.
#
class Users::RegistrationsController < Devise::RegistrationsController
before_filter :authenticate_user!
prepend_before_filter :authenticate_scope!
skip_before_filter :require_no_authentication
end
# app/controllers/users/sessions_controller.rb
# (currently empty)
class Users::SessionsController < Devise::SessionsController
end
The problem:
Why devise is ignoring the :path_names settings above in some situations?
For example, this method will not use the :sign_in setting above, and returns an incorrect path:
new_session_path(resource_name)
=> /users/sign_in
resource_name
=> user
Whereas this method returns the correct path:
new_user_session_path
=> /users/login
The problem is, Devise internally uses the former method, and keeps redirecting to the wrong path when the user is not signed in.
Have I mis-configured something, or is Devise not working correctly? Could this be a Rails 4 issue?
I'm not sure what the issue I was having was. (I'd still be interested to know, if anyone knows).
But I've found another way to express what I wanted with devise, and it seems to work OK:
devise_for :users,
:controllers => {
:registrations => 'users/registrations'
},
:path_names => {
:sign_up => 'new'
}, :skip => [:sessions]
as :user do
get '/users/login' => 'devise/sessions#new', :as => :new_user_session
post '/users/login' => 'devise/sessions#create', :as => :user_session
match '/users/logout' => 'devise/sessions#destroy', :as => :destroy_user_session,
:via => Devise.mappings[:user].sign_out_via
end
I think you trying to solve your question with the wrong feature, in this link show you how to write another routes for devise, in my app I've recreated the routes with this:
devise_scope :user do
get '/cadastrar' => 'devise/registrations#new'
get '/entrar' => 'devise/sessions#new'
get '/editar' => 'devise/registrations#edit'
delete '/sair' => 'devise/sessions#destroy'
end
I'm glad if work!
I try to add an administration area to my website. So I added a namespace that contains the controllers "posts" and "category".
In the routes.rb file, I added
namespace :admin do
resources :posts , :categories
match '/' => 'posts#index', :via => :get
end
but with the url:
myurl.com/admin
i have error:
No route matches {:controller=>"admin/site", :action=>"home", :locale=>:fr}
It seems that another route is called and overwrites the one I added
but in rake routes i have:
admin GET /admin(.:format) admin/posts#index
Here the complete rake routes:
faq GET (/:locale)/faq(.:format) site#faq {:locale=>/[a-zA-Z]{2}/}
rules GET (/:locale)/rules(.:format) site#rules {:locale=>/[a-zA-Z]{2}/}
advanced_rules GET (/:locale)/advanced_rules(.:format) site#advanced_rules {:locale=>/[a-zA-Z]{2}/}
play_now GET (/:locale)/play_now(.:format) play#play {:locale=>/[a-zA-Z]{2}/}
play GET (/:locale)/play(.:format) site#play {:locale=>/[a-zA-Z]{2}/}
home GET (/:locale)/home(.:format) site#home {:locale=>/[a-zA-Z]{2}/}
GET (/:locale)/qrcode(/:code)(.:format) site#qrcode {:locale=>/[a-zA-Z]{2}/}
forgot_password GET (/:locale)/forgot_password/:id(.:format) passwords#forgot_password {:locale=>/[a-zA-Z]{2}/}
android_redirect GET (/:locale)/android_redirect(.:format) android#index {:locale=>/[a-zA-Z]{2}/}
general_conditions GET (/:locale)/general_conditions(.:format) site#general_conditions {:locale=>/[a-zA-Z]{2}/}
support GET (/:locale)/support(.:format) site#support {:locale=>/[a-zA-Z]{2}/}
GET (/:locale)/invitation(/:source)(.:format) invitation#redirection {:locale=>/[a-zA-Z]{2}/}
release_notes GET (/:locale)/release_notes(.:format) site#release_notes {:locale=>/[a-zA-Z]{2}/}
release_note GET (/:locale)/release_notes/:version(.:format) release_notes#show {:locale=>/[a-zA-Z]{2}/}
root GET /(:locale)(.:format) site#home {:locale=>/[a-zA-Z]{2}/}
admin_posts GET /admin/posts(.:format) admin/posts#index
POST /admin/posts(.:format) admin/posts#create
new_admin_post GET /admin/posts/new(.:format) admin/posts#new
edit_admin_post GET /admin/posts/:id/edit(.:format) admin/posts#edit
admin_post GET /admin/posts/:id(.:format) admin/posts#show
PUT /admin/posts/:id(.:format) admin/posts#update
DELETE /admin/posts/:id(.:format) admin/posts#destroy
admin_categories GET /admin/categories(.:format) admin/categories#index
POST /admin/categories(.:format) admin/categories#create
new_admin_category GET /admin/categories/new(.:format) admin/categories#new
edit_admin_category GET /admin/categories/:id/edit(.:format) admin/categories#edit
admin_category GET /admin/categories/:id(.:format) admin/categories#show
PUT /admin/categories/:id(.:format) admin/categories#update
DELETE /admin/categories/:id(.:format) admin/categories#destroy
admin GET /admin(.:format) admin/posts#index
completed GET /payment_completed(.:format) payment#completed
android GET /android(.:format) hockeyapps#android
login POST /login(.:format) registrations#login
loginfacebook POST /loginfacebook(.:format) registrations#loginfacebook
status GET /status(.:format) registrations#status
logout DELETE /logout(.:format) registrations#logout
geturl POST /geturl/:id/:token(.:format) paypals#geturl
getstatus POST /getstatus/:id/:token(.:format) paypals#getstatus
payment_completed GET /payment_completed(.:format) payment#completed
forgot_password GET /forgot_password/:id(.:format) passwords#forgot_password
send_password POST /send_password(.:format) passwords#send_password
Here the complete file routes.rb:
scope "(:locale)", :constraints => {:locale => /[a-zA-Z]{2}/} do
match '/faq' => 'site#faq', :via => :get
match '/rules' => 'site#rules', :via => :get
match '/advanced_rules' => 'site#advanced_rules', :via => :get
match '/play_now' => 'play#play', :via => :get
match '/play' => 'site#play', :via => :get
match '/home' => 'site#home', :via => :get
match '/qrcode/(:code)' => 'site#qrcode', :via => :get
match '/forgot_password/:id' => 'passwords#forgot_password', :via => :get, :as => "forgot_password"
match '/android_redirect' => 'android#index', :via => :get, :as => "android_redirect"
match '/general_conditions' => "site#general_conditions", :via => :get
match '/support' => "site#support", :via => :get
match '/invitation/(:source)' => 'invitation#redirection', :via => :get
match '/release_notes' => 'site#release_notes', :via => :get, :as => "release_notes"
match '/release_notes/:version' => 'release_notes#show', :via => :get, :as => "release_note"
root :to => 'site#home', :via => :get
end
namespace :admin do
resources :posts , :categories
match '/' => 'posts#index', :via => :get
end
get 'payment_completed' => 'payment#completed', :as => "completed"
get 'android' => 'hockeyapps#android', :as => 'android'
post 'login' => 'registrations#login', :as => 'login'
post 'loginfacebook' => 'registrations#loginfacebook', :as => 'loginfacebook'
get 'status' => 'registrations#status', :as => 'status'
delete 'logout' => 'registrations#logout', :as => 'logout'
post 'geturl/:id/:token' => 'paypals#geturl', :as => 'geturl'
post 'getstatus/:id/:token' => 'paypals#getstatus', :as => 'getstatus'
get 'payment_completed' => 'payment#completed', :as => 'payment_completed'
#scope :protocol => 'https://', :constraints => { :protocol => 'https://' } do
# resources :passwords
#end
get 'forgot_password/:id' => 'passwords#forgot_password', :as => 'forgot_password'
post 'send_password' => 'passwords#send_password', :as => 'send_password'
end
Any idea about the problem ?
thanks
match '/' is handled by the root :to => ... and namespaced. So routing tries to call site#home in module (namespace) admin.
Try to put the namespace part in front of the root :to or match '/admin' separately before defining root
Try this
namespace :admin do
resources :posts , :categories
get '', to: 'posts#index', as: '/'
end
scope "(:locale)", :constraints => {:locale => /[a-zA-Z]{2}/} do
...
root :to => 'site#home', :via => :get
...
end
Output of rake routes
admin GET /admin(.:format) admin/posts#index
Tried with get localhost:3000/admin
Started GET "/admin" for 127.0.0.1 at 2013-06-11 17:41:40 +0700
Processing by Admin::PostsController#index as HTML
Rendered admin/posts/index.html.erb within layouts/application (1.0ms)
Completed 200 OK in 1095ms (Views: 1095.1ms | ActiveRecord: 0.0ms)
I'm trying to get Custom Routes working in my Rails application (Ruby 1.9.2 with Rails 3).
This is my config/routes.rb file
match '/dashboard' => 'home#dashboard', :as => 'user_root'
devise_for :user do
get "/login", :to => "devise/sessions#new" # Add a custom sign in route for user sign in
get "/logout", :to => "devise/sessions#destroy" # Add a custom sing out route for user sign out
get "/register", :to => "devise/registrations#new" # Add a Custom Route for Registrations
end
But submitting the form on /login or /register goes to users/sign_in
and users/sign_up. How do I prevent this from happening. Or even better make sure that by default all requests for users/sign_in etc go to the relevant routes and not the default routes generated by Devise.
Also how can I make the login form a partial to include it in any controller? So that I can have the Login Page on the homepage (home#index) and not on users/sign_in?
I'm using Devise 1.1.3 with Rails 3 on Ruby 1.9.2, on Mac OSX Snow Leopard.
Thanks!
With Devise 1.1.3 the following should work
devise_for :user, :path => '', :path_names => { :sign_in => "login", :sign_out => "logout", :sign_up => "register" }
The routes it creates will not be appended with "/user/..." because of the :path parameter being an empty string. The :pathnames hash will take care of naming the routes as you like. Devise will use these routes internally so submitting to /login will work as you wish and not take you to /user/log_in
To add a login form to your front page there's info at the Devise Wiki:
http://github.com/plataformatec/devise/wiki/How-To:-Display-a-custom-sign_in-form-anywhere-in-your-app
Or do something like this:
<%= form_tag new_user_session_path do %>
<%= text_field_tag 'user[email]' %>
<%= password_field_tag 'user[password]' %>
<%= submit_tag 'Login' %>
The following worked for me:
devise_for :users do
get "/login" => "devise/sessions#new"
get "/register" => "devise/registrations#new"
end
Config:
devise_scope :user do
get 'profile/edit' => 'devise/registrations#edit', :as => :edit_user_registration
get 'profile/cancel' => 'devise/registrations#cancel', :as => :cancel_user_registration
end
devise_for :users,
:path => '',
:path_names => { :sign_in => 'login',
:sign_out => 'logout',
:sign_up => '',
:registration => 'register',
:edit => 'edit',
:cancel => 'cancel',
:confirmation => 'verification' }
Routes:
edit_user_registration GET /profile/edit(.:format) devise/registrations#edit
cancel_user_registration GET /profile/cancel(.:format) devise/registrations#cancel
new_user_session GET /login(.:format) devise/sessions#new
user_session POST /login(.:format) devise/sessions#create
destroy_user_session DELETE /logout(.:format) devise/sessions#destroy
user_password POST /password(.:format) devise/passwords#create
new_user_password GET /password/new(.:format) devise/passwords#new
edit_user_password GET /password/edit(.:format) devise/passwords#edit
PATCH /password(.:format) devise/passwords#update
PUT /password(.:format) devise/passwords#update
GET /register/cancel(.:format) registrations#cancel
user_registration POST /register(.:format) registrations#create
new_user_registration GET /register(.:format) registrations#new
GET /register/edit(.:format) registrations#edit
PATCH /register(.:format) registrations#update
PUT /register(.:format) registrations#update
DELETE /register(.:format) registrations#destroy
You just need don't put your special route in devise_for block
match '/dashboard' => 'home#dashboard', :as => 'user_root'
get "/login", :to => "devise/sessions#new" # Add a custom sign in route for user sign in
get "/logout", :to => "devise/sessions#destroy" # Add a custom sing out route for user sign out
get "/register", :to => "devise/registrations#new" # Add a Custom Route for Registrations
devise_for :user
Now /login works. /users/sign_in too.
I created my own auth controller and routed devise sessions controller to my controller
devise_for :users,
:controllers => {
:sessions => 'auth' },
:path => '/',
:path_names => {
:sign_in => 'login',
:sign_out => 'logout' }
This code will add /login and /logout urls.
More about this you can find in source code http://github.com/plataformatec/devise/blob/master/lib/devise/rails/routes.rb
Use this at the top of your routes.rb file
map.connect "users/:action", :controller => 'users', :action => /[a-z]+/i
use this on where your index file is. if it is on your users model, use the above or change accordingly