Rails3 + Devise:何时在 devise_for & 中嵌套资源嵌套资源 [英] Rails3 + Devise: When to nest resource in devise_for & nested resources
问题描述
我什么时候应该在
devise_for
块中嵌套路由?请举一两个例子来说明用例.(路线#1)
When should I nest routes in the
devise_for
block? Please give one or two examples to show the use case. (Routes #1)
如果 :foo_object
与 :users
相关联,所以 :user
has_one :foo_object
, do我需要在 :users
下嵌套 :foo_object
吗?(路线#2):users
是设计 :users
模型.
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.
路线#1:
devise_for :users
resource :foo_object
路线#2:
devise_for :users
resources :users do
resource :foo_object
end
推荐答案
下例:
devise_for :users, :path => 'accounts'
resources :users do
resources :orders
end
上面的意思是认证路径是"/accounts/sign_in"
, "/accounts_sign_up"
等等.可能有些人不知道确认很重要devise_for :users
不会实际上映射到 UsersController
和模型.它甚至不是一条资源路线,尽管许多人认为它看起来像它.这就是为什么我们不能像下面这样对待它:
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
devise_for
所做的只是映射以下路由:
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
希望这能解决问题.此外,您可能还想阅读 Nested Resource with Devise - Rails3
Hopefully this clears things up. Also you may want to have a read of Nested Resource with Devise - Rails3
这篇关于Rails3 + Devise:何时在 devise_for & 中嵌套资源嵌套资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!