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)
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 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.
I am attempting to setup my routes.rb so that /sessions/ is not required in the url for logging in and out of the site. Below are my samples to show what I am trying to achieve. Whilst the "second attempt" does in fact do what I want, I'd like to know if there is a more efficient way of doing this. I am very new to rails and I am sure that the routes.rb has some option that can do what I am doing in three large lines.
First attempt
routes.rb
namespace :account do
resources :users
resources :sessions
end
$ rake routes
Prefix Verb URI Pattern Controller#Action
account_users GET /account/users(.:format) account/users#index
...
account_sessions GET /account/sessions(.:format) account/sessions#index
POST /account/sessions(.:format) account/sessions#create
new_account_session GET /account/sessions/new(.:format) account/sessions#new
edit_account_session GET /account/sessions/:id/edit(.:format) account/sessions#edit
account_session GET /account/sessions/:id(.:format) account/sessions#show
PATCH /account/sessions/:id(.:format) account/sessions#update
PUT /account/sessions/:id(.:format) account/sessions#update
DELETE /account/sessions/:id(.:format) account/sessions#destroy
Second attempt
routes.rb
namespace :account do
resources :users
match '/login', :controller => 'sessions', :action => 'new', :via => [:get]
match '/login', :controller => 'sessions', :action => 'create', :via => [:post]
match '/logout', :controller => 'sessions', :action => 'destroy', :via => [:delete]
end
$ rake routes
Prefix Verb URI Pattern Controller#Action
account_users GET /account/users(.:format) account/users#index
...
account_login GET /account/login(.:format) account/sessions#new
POST /account/login(.:format) account/sessions#create
account_logout DELETE /account/logout(.:format) account/sessions#destroy
Can this be done without having to manually specific the match locations? All I want to do is remove /sessions/ as a requirement.
namespace :account do
resources :users #-> account/users
resources :sessions, path: "", path_names: { new: "login", create: "login", destroy: "logout" } #-> accounts/login, accounts/logout
end
I hope you realise you have /login twice in your second example. This simplifies it a bit but you will always have to match each route you want to specify outside any defaults.
namespace :account do
match '/login', to: 'sessions#new', via: [:get]
match '/logout', to: 'sessions#destroy', via: [:delete]
end
In rails3 we should use with_options in following way:
scope '/account' do
match '/login' => "sessions#new", :as => :login
post '/:login' => 'sessions#create', :as => :signup_create
delete '/:logout' => 'sessions#destroy', :as => :logout
end
# Explaining the context
puts "I am learning Rails, building a simple forum application."
puts "I am pretty satisfied to where I got so far but routes... "
puts "...still figuring them out."
puts "Been 2 days trying all sorts of things."
puts "This is where I am now, and something is not working as expected."
puts "Any help/pointers would be appreciated! :)"
# The Problem
puts "I want my forum's create path to be '/helpcenter' and not '/helpcenter/cat'."
puts "When I access the form to create a new forum and I hit submit, "
puts "the form post to '/helpcenter' correctly (firebuged)"
puts "but I get the index, not the create!"
puts "I even put debugger in my create action but it is not being called."
# config/routes.rb
scope "/helpcenter" do
resources :cat, :controller => "forums", :as => :forums do
resources :topics , :controller => "forum_topics", :as => :topics
resources :posts, :controller => "forum_posts", :as => :posts
end
end
match "/helpcenter" => "forums#index", :as => :forums
match "/helpcenter" => "forums#create", :via => :post, :as => :create_forum
I hope this problem is in the way I created the route. I tried many different things.
# _form.html.erb
<%= form_for(#forum) do |f| %>
....
<% end %>
I am using standard form_for helper.
# Rake Routes for Forums
$ CONTROLLER=forums rake routes
delete_forum GET /helpcenter/cat/:id/delete(.:format) forums#delete
forums GET /helpcenter/cat(.:format) forums#index
POST /helpcenter/cat(.:format) forums#create
new_forum GET /helpcenter/cat/new(.:format) forums#new
edit_forum GET /helpcenter/cat/:id/edit(.:format) forums#edit
forum GET /helpcenter/cat/:id(.:format) forums#show
PUT /helpcenter/cat/:id(.:format) forums#update
DELETE /helpcenter/cat/:id(.:format) forums#destroy
forums /helpcenter(.:format) forums#index
create_forum POST /helpcenter(.:format) forums#create
We clearly see a route for POST /helpcenter which is binded to the create action of the forums controller.
# Logs
Started POST "/helpcenter" for 127.0.0.1 at 2012-07-02 12:25:00 -0400
Processing by ForumsController#index as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"d5iVKCh234234=", "forum"=>{"name"=>"", "description"=>""}, "commit"=>"Save Changes"}
The logs clearly shows I am doing a POST on /helpcenter but that it fires up the index action instead of the create action!
# What am I doing wrong?
puts "Thanks!"
I think the request matches your first forums route since you didn't specify a HTTP method. This should work:
match "/helpcenter" => "forums#index", :via => :get, :as => :forums
match "/helpcenter" => "forums#create", :via => :post, :as => :create_forum
Or the shorthand version:
get "/helpcenter" => "forums#index", :as => :forums
post "/helpcenter" => "forums#create", :as => :create_forum
First glance shows that a POST against /helpcenter passes the rule for the forums#index match, which is encountered first, so that's what you get
match "/helpcenter" => "forums#index", :as => :forums
match "/helpcenter" => "forums#create", :via => :post, :as => :create_forum
What about:
match "/helpcenter" => "forums#index", :via => :get, :as => :forums
match "/helpcenter" => "forums#create", :via => :post, :as => :create_forum
I would like 'about' to route to 'abouts/1'
I tried this:
match 'about' => 'abouts#show/1', :via => get
and it doesn't work. Any ideas?
How about:
match 'about' => 'abouts#show', :via => :get, :defaults => {:id => 1}
What about just removing the 1 from the route and retrieve the record you want directly in the controller method?
# routes.rb
match 'about' => 'abouts#show', :via => get
# abouts_controller.rb
def show
#about = About.find(1)
end