Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails3 + Devise: When to nest resource in devise_for & nested resources

  1. When should I nest routes in the devise_for block? Please give one or two examples to show the use case. (Routes #1)

  2. If :foo_object is associated with :users so :user has_one :foo_object, do I need to nest :foo_object under :users? (Routes #2) :users is the devise :users model.

Routes #1:

devise_for :users  
resource :foo_object

Routes #2:

devise_for :users
resources :users do      
  resource :foo_object
end
like image 445
HM1 Avatar asked Jun 27 '13 17:06

HM1


1 Answers

The following example:

devise_for :users, :path => 'accounts'

resources :users do
    resources :orders
end

The above means that the authentication path would be "/accounts/sign_in", "/accounts_sign_up"etc.. Some may not know that it is important to acknowledge that the devise_for :users doesn't actually map to the UsersController and the model. Its not even a resource route, even though many beleive it looks like it. Which is why we can't treat it like the following:

devise_for :users do 
   resources: somereosouce
end 

All devise_for does is map the following routes:

# Session routes for Authenticatable (default)
     new_user_session GET  /users/sign_in                    {:controller=>"devise/sessions", :action=>"new"}
         user_session POST /users/sign_in                    {:controller=>"devise/sessions", :action=>"create"}
 destroy_user_session GET  /users/sign_out                   {:controller=>"devise/sessions", :action=>"destroy"}

# Password routes for Recoverable, if User model has :recoverable configured
    new_user_password GET  /users/password/new(.:format)     {:controller=>"devise/passwords", :action=>"new"}
   edit_user_password GET  /users/password/edit(.:format)    {:controller=>"devise/passwords", :action=>"edit"}
        user_password PUT  /users/password(.:format)         {:controller=>"devise/passwords", :action=>"update"}
                      POST /users/password(.:format)         {:controller=>"devise/passwords", :action=>"create"}

# Confirmation routes for Confirmable, if User model has :confirmable configured
new_user_confirmation GET  /users/confirmation/new(.:format) {:controller=>"devise/confirmations", :action=>"new"}
    user_confirmation GET  /users/confirmation(.:format)     {:controller=>"devise/confirmations", :action=>"show"}
                      POST /users/confirmation(.:format)     {:controller=>"devise/confirmations", :action=>"create"}

So in saying that you could do the following but would have some conflicts:

devise_for :users 

resource :users do 
   resource :foo_object
end 

A little bit on nested resources, if you have something like the following:

class Users < ActiveRecord::Base
  has_many :foo_object
end

class FooObject < ActiveRecord::Base
  belongs_to :users
end

Then your nested resource would be

   resource :users do 
     resource :foo_object 
   end

Hopefully this clears things up. Also you may want to have a read of Nested Resource with Devise - Rails3

like image 58
Deej Avatar answered Oct 17 '22 18:10

Deej