我应该何时在devise_for
区块中嵌套路线?请举一个或两个示例来显示用例。 (路线#1)
如果:foo_object
与:users
相关联,:user
has_one :foo_object
,我是否需要在:foo_object
下嵌套:users
? (路线#2):users
是设计:users
模型。
路线#1:
devise_for :users
resource :foo_object
路线#2:
devise_for :users
resources :users do
resource :foo_object
end
答案 0 :(得分:25)
以下示例:
devise_for :users, :path => 'accounts'
resources :users do
resources :orders
end
以上意味着身份验证路径为"/accounts/sign_in"
,"/accounts_sign_up"
等。有些人可能不知道确认devise_for :users
实际上不是很重要映射到UsersController
和模型。它甚至不是一条资源路线,尽管它看起来很像。这就是为什么我们不能像以下那样对待它:
devise_for :users do
resources: somereosouce
end
所有devise_for
都会映射以下路线:
# 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"}
所以说你可以做以下事情,但会有一些冲突:
devise_for :users
resource :users do
resource :foo_object
end
关于嵌套资源的一点点,如果您有以下内容:
class Users < ActiveRecord::Base
has_many :foo_object
end
class FooObject < ActiveRecord::Base
belongs_to :users
end
然后您的嵌套资源将是
resource :users do
resource :foo_object
end
希望这可以解决问题。您也可以阅读Nested Resource with Devise - Rails3