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

后端 未结 1 740
失恋的感觉
失恋的感觉 2020-12-29 10:45
  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_ob

相关标签:
1条回答
  • 2020-12-29 11:32

    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

    0 讨论(0)
提交回复
热议问题