Two Devise Models, With Association Issue - ruby-on-rails

I have two models for devise. Client, and Engineer.
The Client belongs to the Engineer. From the Engineer#show page, I'm trying to link to the client but I'm getting a devise mapping error.
Could not find devise mapping for path "/clients/10".
I have two controllers, one for devise and one regular (controllers/client/registration_controller and controllers/client_controller)
My routes file looks like this...
devise_for :clients, :controllers => {:registrations => "client/registrations"}
resources :clients, :only => [:show, :new, :create]
devise_for :engineers, :controllers => {:registrations => "engineer/registrations"}
resources :engineers, :only => [:show]
devise_scope :client do
get "clients/:id", to: "clients#show"
end
Are the resources :clients competing with the scope declaration?
For good measure my rake routes:
new_client_session GET /clients/sign_in(.:format) devise/sessions#new
client_session POST /clients/sign_in(.:format) devise/sessions#create
destroy_client_session DELETE /clients/sign_out(.:format) devise/sessions#destroy
client_password POST /clients/password(.:format) devise/passwords#create
new_client_password GET /clients/password/new(.:format) devise/passwords#new
edit_client_password GET /clients/password/edit(.:format) devise/passwords#edit
PATCH /clients/password(.:format) devise/passwords#update
PUT /clients/password(.:format) devise/passwords#update
cancel_client_registration GET /clients/cancel(.:format) client/registrations#cancel
client_registration POST /clients(.:format) client/registrations#create
new_client_registration GET /clients/sign_up(.:format) client/registrations#new
edit_client_registration GET /clients/edit(.:format) client/registrations#edit
PATCH /clients(.:format) client/registrations#update
PUT /clients(.:format) client/registrations#update
DELETE /clients(.:format) client/registrations#destroy
clients POST /clients(.:format) clients#create
new_client GET /clients/new(.:format) clients#new
client GET /clients/:id(.:format) clients#show
new_engineer_session GET /engineers/sign_in(.:format) devise/sessions#new
engineer_session POST /engineers/sign_in(.:format) devise/sessions#create
destroy_engineer_session DELETE /engineers/sign_out(.:format) devise/sessions#destroy
engineer_password POST /engineers/password(.:format) devise/passwords#create
new_engineer_password GET /engineers/password/new(.:format) devise/passwords#new
edit_engineer_password GET /engineers/password/edit(.:format) devise/passwords#edit
PATCH /engineers/password(.:format) devise/passwords#update
PUT /engineers/password(.:format) devise/passwords#update
engineer GET /engineers/:id(.:format) engineers#show
GET /clients/:id(.:format) clients#show
root GET / pages#home
pages_clients GET /pages/clients(.:format) pages#clients
pages_engineers GET /pages/engineers(.:format) pages#engineers

I'm pretty sure you should just drop your devise_scope block, since you have the following line:
resources :clients, :only => [:show, :new, :create]
This line already gives you client_path:
client GET /clients/:id(.:format) clients#show
but your devise_scope gives you a duplicate (but unnamed) path:
GET /clients/:id(.:format) clients#show
So, your routes file should look like:
devise_for :clients, :controllers => {:registrations => "client/registrations"}
resources :clients, :only => [:show, :new, :create]
devise_for :engineers, :controllers => {:registrations => "engineer/registrations"}
resources :engineers, :only => [:show]

Related

admin/login route is going post page

my routes.rb
get '/admin/login' => 'admin/sessions#new'
get '/admin/logout' => 'admin/sessions#destroy'
get '/admin' => 'admin/dashboard#index'
get 'blog' => 'blogs#index'
get ':id' => 'posts#show'
get 'posts/:id' => redirect('%{id}')
get 'blog/:id' => 'blogs#show'
get 'category/:id' => 'posts#index'
namespace :admin do
resources :sessions, only: [:new, :create, :destroy]
resources :login, only: [:index, :edit]
resources :categories
resources :admins, only: [:index, :edit, :update, :new, :create, :destroy]
resources :dashboard, only:[:index]
resources :settings, only:[:new, :create, :edit, :update]
resources :posts
resources :blogs
end
resources :home, only:[:index]
resources :posts, only:[:index, :show]
resources :blogs, only:[:index, :show]
when i try go to the /admin/login page it is seems like post page or something. but i can see the login form. just it is not admin page (i mean there is no my admin css or something it is seems like admin login form in my normal front end post page. and when i try login with my username and password it is giving this error:
ActionView::MissingTemplate in Admin::Posts#new
then im deleting this part from routes.rb
get 'blog' => 'blogs#index'
get ':id' => 'posts#show'
get 'posts/:id' => redirect('%{id}')
get 'blog/:id' => 'blogs#show'
get 'category/:id' => 'posts#index'
then i can go to the admin page. then im pasting this part again and still can use admin panel with no errors i dont understand.
can someone help? (sorry for my english)
here is the rake routes
Prefix Verb URI Pattern Controller#Action
root GET / home#index
admin_login GET /admin/login(.:format) admin/sessions#new
admin_logout GET /admin/logout(.:format) admin/sessions#destroy
admin GET /admin(.:format) admin/dashboard#index
blog GET /blog(.:format) blogs#index
GET /:id(.:format) posts#show
GET /posts/:id(.:format) redirect(301, %{id})
GET /blog/:id(.:format) blogs#show
GET /category/:id(.:format) posts#index
admin_sessions POST /admin/sessions(.:format) admin/sessions#create
new_admin_session GET /admin/sessions/new(.:format) admin/sessions#new
admin_session DELETE /admin/sessions/:id(.:format) admin/sessions#destroy
admin_login_index GET /admin/login(.:format) admin/login#index
edit_admin_login GET /admin/login/:id/edit(.:format) admin/login#edit
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
PATCH /admin/categories/:id(.:format) admin/categories#update
PUT /admin/categories/:id(.:format) admin/categories#update
DELETE /admin/categories/:id(.:format) admin/categories#destroy
admin_admins GET /admin/admins(.:format) admin/admins#index
POST /admin/admins(.:format) admin/admins#create
new_admin_admin GET /admin/admins/new(.:format) admin/admins#new
edit_admin_admin GET /admin/admins/:id/edit(.:format) admin/admins#edit
admin_admin PATCH /admin/admins/:id(.:format) admin/admins#update
PUT /admin/admins/:id(.:format) admin/admins#update
DELETE /admin/admins/:id(.:format) admin/admins#destroy
admin_dashboard_index GET /admin/dashboard(.:format) admin/dashboard#index
admin_settings POST /admin/settings(.:format) admin/settings#create
new_admin_setting GET /admin/settings/new(.:format) admin/settings#new
edit_admin_setting GET /admin/settings/:id/edit(.:format) admin/settings#edit
admin_setting PATCH /admin/settings/:id(.:format) admin/settings#update
PUT /admin/settings/:id(.:format) admin/settings#update
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
PATCH /admin/posts/:id(.:format) admin/posts#update
PUT /admin/posts/:id(.:format) admin/posts#update
DELETE /admin/posts/:id(.:format) admin/posts#destroy
admin_blogs GET /admin/blogs(.:format) admin/blogs#index
POST /admin/blogs(.:format) admin/blogs#create
new_admin_blog GET /admin/blogs/new(.:format) admin/blogs#new
edit_admin_blog GET /admin/blogs/:id/edit(.:format) admin/blogs#edit
admin_blog GET /admin/blogs/:id(.:format) admin/blogs#show
PATCH /admin/blogs/:id(.:format) admin/blogs#update
PUT /admin/blogs/:id(.:format) admin/blogs#update
DELETE /admin/blogs/:id(.:format) admin/blogs#destroy
home_index GET /home(.:format) home#index
posts GET /posts(.:format) posts#index
post GET /posts/:id(.:format) posts#show
blogs GET /blogs(.:format) blogs#index
GET /blogs/:id(.:format) blogs#show
Seems like the issue is on these two lines.
get ':id' => 'posts#show'
get 'posts/:id' => redirect('%{id}')
Try this
get 'posts/:id' => 'posts#show'

Rails 4 + Devise: Missing "admins/sign_up" route

I have a Rails app, v4, and just need ot modify some "old" code - I need to add some attributes to the admin model.
When I tried to add a new admin user and set to the browser "/admins/sign_up", I got an error message that the route doesn't exist.
Okay, so I checked all the Devise routes:
new_admin_session GET /admins/sign_in(.:format) devise/sessions#new
admin_session POST /admins/sign_in(.:format) devise/sessions#create
destroy_admin_session DELETE /admins/admin_logout(.:format) devise/sessions#destroy
admin_password POST /admins/password(.:format) devise/passwords#create
new_admin_password GET /admins/password/new(.:format) devise/passwords#new
edit_admin_password GET /admins/password/edit(.:format) devise/passwords#edit
PATCH /admins/password(.:format) devise/passwords#update
PUT /admins/password(.:format) devise/passwords#update
new_user_session GET /users/login(.:format) devise/sessions#new
user_session POST /users/login(.:format) devise/sessions#create
destroy_user_session DELETE /users/logout(.:format) devise/sessions#destroy
user_password POST /users/secret(.:format) devise/passwords#create
new_user_password GET /users/secret/new(.:format) devise/passwords#new
edit_user_password GET /users/secret/edit(.:format) devise/passwords#edit
PATCH /users/secret(.:format) devise/passwords#update
PUT /users/secret(.:format) devise/passwords#update
How is possible that the routes for a new admin sign up is missing? Did I miss something?
Anyway, how to add this missing route?
When I look to the views, I see that in admins/registrations/ is new.html.erb and there's a sign up form for a new admin.
How to display (under which URL) form and use it?
Thank you in advance.
EDIT: Routes
devise_for :admins, :path_names => {:sign_out => 'admin_logout'}
devise_for :users, :path_names => { :sign_in => 'login', :sign_out => 'logout', :password => 'secret',
:confirmation => 'verification', :unlock => 'unlock', :registration => 'register',
:sign_up => 'signup' }, :controllers => {:omniauth_callbacks => "omniauth_callbacks", :registrations => "registrations"}
You can add the missing /admins/sign_up route like the following:
devise_for :admins do
get '/admins/sign_up' => 'devise/registrations#new'
end
This generates this route:
new_admin_registration GET /admins/sign_up(.:format) devise/registrations#new
and should fix your problem.
Update:
Remove this from your current routes file:
devise_for :admins, :path_names => {:sign_out => 'admin_logout'}
Add this to your routes file:
devise_for :admins do
get '/admins/sign_up' => 'devise/registrations#new'
end

adding a method to non-restful routes in rails 4

i would like to perform an action on some of my routes, the problem is they are not in a 'resources' block because I need named methods for each. I want to be able to toggle the state of each an attribute in each item in an index type view. I wwas attempting to incorporate this tutorial.
devise_for :admins # The priority is based upon order of creation: first created -> highest priority.
root 'home#index'
resources :entries, :only => [:index, :new, :create]
namespace :admin do
namespace :entries do
match :pending, :via => [:get, :post], :collection => { :toggle_approve => :put}
match :rejected, :via => [:get, :post], :collection => { :toggle_approve => :put}
match :approved, :via => [:get, :post], :collection => { :toggle_approve => :put}
end
end
entries controller
class Admin::EntriesController < ApplicationController
expose(:entries){#entries}
def index
end
def show
end
def approved
#entries = Photo.with_approved_state
end
def pending
#entries = Photo.with_pending_state
end
def rejected
#entries = Photo.with_rejected_state
end
def toggle_approve
#a = Photo.find(params[:id])
#a.toggle!(:workflow_state)
render :nothing => true
end
rake routes
Prefix Verb URI Pattern Controller#Action
new_admin_session GET /admins/sign_in(.:format) devise/sessions#new
admin_session POST /admins/sign_in(.:format) devise/sessions#create
destroy_admin_session DELETE /admins/sign_out(.:format) devise/sessions#destroy
admin_password POST /admins/password(.:format) devise/passwords#create
new_admin_password GET /admins/password/new(.:format) devise/passwords#new
edit_admin_password GET /admins/password/edit(.:format) devise/passwords#edit
PATCH /admins/password(.:format) devise/passwords#update
PUT /admins/password(.:format) devise/passwords#update
cancel_admin_registration GET /admins/cancel(.:format) devise/registrations#cancel
admin_registration POST /admins(.:format) devise/registrations#create
new_admin_registration GET /admins/sign_up(.:format) devise/registrations#new
edit_admin_registration GET /admins/edit(.:format) devise/registrations#edit
PATCH /admins(.:format) devise/registrations#update
PUT /admins(.:format) devise/registrations#update
DELETE /admins(.:format) devise/registrations#destroy
root GET / home#index
entries GET /entries(.:format) entries#index
POST /entries(.:format) entries#create
new_entry GET /entries/new(.:format) entries#new
admin_entries_pending GET|POST /admin/entries/pending(.:format) admin/entries#pending {:collection=>{:toggle_approve_article=>:put}}
admin_entries_rejected GET|POST /admin/entries/rejected(.:format) admin/entries#rejected {:collection=>{:toggle_approve_article=>:put}}
admin_entries_approved GET|POST /admin/entries/approved(.:format) admin/entries#approved {:collection=>{:toggle_approve_article=>:put}}
I don't know where the collection option is from (I literally can't find reference to it anywhere)
Implementing non-resourceful routes is actually relatively simple:
#config/routes.rb
resources :entries, only: [:index, :new, :create] do
collection do
match :pending, via: [:get, :post] #-> domain.com/entries/pending
match :rejected, via: [:get, :post] #-> domain.com/entries/rejected
match :approved, via: [:get, :post] #-> domain.com/entries/approved
end
end
I don't understand the collection option - I don't think that belongs in your routes. Although having thought about it, I guess you're trying to make it so that if you receive a request to domain.com/entries/toggle_approve_article/pending you'll want to handle the reqeust?
If that's the case, why not just do this:
#config/routes.rb
resources :entries, only: [:index, :new, :create] do
put :toggle_approve #-> domain.com/entries/15/toggle_approve
collection do
match :pending, via: [:get, :post] #-> domain.com/entries/pending
match :rejected, via: [:get, :post] #-> domain.com/entries/rejected
match :approved, via: [:get, :post] #-> domain.com/entries/approved
end
end
i had to understand the difference between member and collections. collections are routes additional to a resource (eg, the restful actions). members are extra routes which can perform actions on their the block.
This...
resources :entries, :only => [:index, :new, :create]
namespace :admin do
resources :entries do
get :pending, on: :collection
get :approved, on: :collection
get :rejected, on: :collection
member do
get :toggle_approve_field
get :toggle_reject_field
end
end
end
yielded this rake routes
entries GET /entries(.:format) entries#index
POST /entries(.:format) entries#create
new_entry GET /entries/new(.:format) entries#new
pending_admin_entries GET /admin/entries/pending(.:format) admin/entries#pending
approved_admin_entries GET /admin/entries/approved(.:format) admin/entries#approved
rejected_admin_entries GET /admin/entries/rejected(.:format) admin/entries#rejected
toggle_approve_field_admin_entry GET /admin/entries/:id/toggle_approve_field(.:format) admin/entries#toggle_approve_field
toggle_reject_field_admin_entry GET /admin/entries/:id/toggle_reject_field(.:format) admin/entries#toggle_reject_field
admin_entries GET /admin/entries(.:format) admin/entries#index
POST /admin/entries(.:format) admin/entries#create
new_admin_entry GET /admin/entries/new(.:format) admin/entries#new
edit_admin_entry GET /admin/entries/:id/edit(.:format) admin/entries#edit
admin_entry GET /admin/entries/:id(.:format) admin/entries#show
PATCH /admin/entries/:id(.:format) admin/entries#update
PUT /admin/entries/:id(.:format) admin/entries#update
DELETE /admin/entries/:id(.:format) admin/entries#destroy
It took a lot of faffing around and I not sure I'd be able to do it upon request without more headache but I certainly have a better idea of rails routing all together. Thanks for the help from #RichPeck

How do you create links in Rails using link.to

Hi im doing a kind of blog to learn Rails, using the Getting started tutorials like these
http://guides.rubyonrails.org/getting_started.html
http://guides.rubyonrails.org/routing.html
I have manage to do the posts sections and i also do a admin/posts section, this is the problem now..
Ths system is "conflicting" and admin goes to domain.com/posts instead of admin/posts.
I think the problem is the way i build the links..
In the tutorial to link a item yo do
<h2><%= link_to post.title, post %></h2>
I have tried
<h2><%= link_to post.title, admin_post_path %></h2>
And similars but i get
undefined local variable or method `admin_post_path' for #<#<Class:0x007fe3e990ef28>:0x007fe3e6e3b508>
How does this works i mean i have done rake routes and i see there the routes, but i cant use them
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 GET /admin/users(.:format) admin/users#index
POST /admin/users(.:format) admin/users#create
new_user GET /admin/users/new(.:format) admin/users#new
edit_user GET /admin/users/:id/edit(.:format) admin/users#edit
user GET /admin/users/:id(.:format) admin/users#show
PUT /admin/users/:id(.:format) admin/users#update
DELETE /admin/users/:id(.:format) admin/users#destroy
posts GET /admin/posts(.:format) admin/posts#index
POST /admin/posts(.:format) admin/posts#create
new_post GET /admin/posts/new(.:format) admin/posts#new
edit_post GET /admin/posts/:id/edit(.:format) admin/posts#edit
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
players GET /players(.:format) players#index
POST /players(.:format) players#create
new_player GET /players/new(.:format) players#new
edit_player GET /players/:id/edit(.:format) players#edit
player GET /players/:id(.:format) players#show
PUT /players/:id(.:format) players#update
DELETE /players/:id(.:format) players#destroy
player_steps GET /player_steps(.:format) player_steps#index
POST /player_steps(.:format) player_steps#create
new_player_step GET /player_steps/new(.:format) player_steps#new
edit_player_step GET /player_steps/:id/edit(.:format) player_steps#edit
player_step GET /player_steps/:id(.:format) player_steps#show
PUT /player_steps/:id(.:format) player_steps#update
DELETE /player_steps/:id(.:format) player_steps#destroy
coach_steps GET /coach_steps(.:format) coach_steps#index
POST /coach_steps(.:format) coach_steps#create
new_coach_step GET /coach_steps/new(.:format) coach_steps#new
edit_coach_step GET /coach_steps/:id/edit(.:format) coach_steps#edit
coach_step GET /coach_steps/:id(.:format) coach_steps#show
PUT /coach_steps/:id(.:format) coach_steps#update
DELETE /coach_steps/:id(.:format) coach_steps#destroy
candidates GET /candidates(.:format) candidates#index
POST /candidates(.:format) candidates#create
new_candidate GET /candidates/new(.:format) candidates#new
edit_candidate GET /candidates/:id/edit(.:format) candidates#edit
candidate GET /candidates/:id(.:format) candidates#show
PUT /candidates/:id(.:format) candidates#update
DELETE /candidates/:id(.:format) candidates#destroy
payment_notifications GET /payment_notifications(.:format) payment_notifications#show
post GET /posts/:id(.:format) posts#show
posts GET /posts(.:format) posts#index
admin_posts_path GET /admin/posts(.:format) admin/posts#index
admin_posts_path POST /admin/posts(.:format) admin/posts#index
admin_post_path GET /admin/posts/:id(.:format) admin/posts#show
new_admin_post_path GET /admin/posts/new(.:format) admin/posts#new
/*a(.:format) errors#routing
choose GET /user_type(.:format) home#user_type
root / devise/sessions#new
Also tried this
<h2><%= link_to post.title, url_for([#post]) %></h2>
this throw:::: Nil location provided. Can't build URI.
=( any documentation on doing this, ?? do you know where i can find it
Routes.rb
Consult::Application.routes.draw do
devise_for :users, :controllers => { :registrations => "registrations" }
scope "/admin" do
resources :users, :controller => 'admin/users'
resources :posts, :controller => 'admin/posts'
end
resources :players
resources :player_steps
resources :coach_steps
resources :candidates
resource :payment_notifications, :only => :show
#match 'candidates' => 'candidates#index'
#resources :posts
get '/posts/:id', to: 'posts#show', as: 'post'
get '/posts/', to: 'posts#index', as: 'posts'
get '/admin/posts/', to: 'admin/posts#index', as: 'admin_posts_path'
post '/admin/posts/', to: 'admin/posts#index', as: 'admin_posts_path'
get '/admin/posts/:id', to: 'admin/posts#show', as: 'admin_post_path'
get '/admin/posts/new', to: 'admin/posts#new', as: 'new_admin_post_path'
match '*a', :to => 'errors#routing'
# The priority is based upon order of creation:
# first created -> highest priority.
# Sample of regular route:
# match 'products/:id' => 'catalog#view'
# Keep in mind you can assign values other than :controller and :action
# Sample of named route:
# match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase
# This route can be invoked with purchase_url(:id => product.id)
# Sample resource route (maps HTTP verbs to controller actions automatically):
# resources :products
# Sample resource route with options:
# resources :products do
# member do
# get 'short'
# post 'toggle'
# end
#
# collection do
# get 'sold'
# end
# end
# Sample resource route with sub-resources:
# resources :products do
# resources :comments, :sales
# resource :seller
# end
# Sample resource route with more complex sub-resources
# resources :products do
# resources :comments
# resources :sales do
# get 'recent', :on => :collection
# end
# end
# Sample resource route within a namespace:
# namespace :admin do
# # Directs /admin/products/* to Admin::ProductsController
# # (app/controllers/admin/products_controller.rb)
# resources :products
# end
# You can have the root of your site routed with "root"
# just remember to delete public/index.html.
#root :to => "devise/sessions#new"
get 'user_type', to: 'home#user_type', as: :choose
devise_scope :user do
root :to => "devise/sessions#new"
end
# See how all your routes lay out with "rake routes"
# This is a legacy wild controller route that's not recommended for RESTful applications.
# Note: This route will make all actions in every controller accessible via GET requests.
# match ':controller(/:action(/:id))(.:format)'
end
Try using namespace, as opposed to the scope, as the namespace is much better for nesting. your routes file should look something like this.
namespace :admin do
resources :users
resources :posts
end
resources :players
resources :player_steps
resources :coach_steps
resources :candidates
resource :payment_notifications, :only => :show
#match 'candidates' => 'candidates#index'
resources :posts
you can remove
get '/posts/:id', to: 'posts#show', as: 'post'
get '/posts/', to: 'posts#index', as: 'posts'
get '/admin/posts/', to: 'admin/posts#index', as: 'admin_posts_path'
post '/admin/posts/', to: 'admin/posts#index', as: 'admin_posts_path'
get '/admin/posts/:id', to: 'admin/posts#show', as: 'admin_post_path'
get '/admin/posts/new', to: 'admin/posts#new', as: 'new_admin_post_path'
as the namespace and the resources handle their generation
full routes file should be
Consult::Application.routes.draw do
devise_for :users, :controllers => { :registrations => "registrations" }
namespace :admin do
resources :users
resources :posts
end
resources :players
resources :player_steps
resources :coach_steps
resources :candidates
resources :posts
resource :payment_notifications, :only => :show
get 'user_type', to: 'home#user_type', as: :choose
devise_scope :user do
root :to => "devise/sessions#new"
end
end

Devise not obeying :path_names setting for sign_in route

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!

Resources