Ví dụ sau đây:
devise_for :users, :path => 'accounts'
resources :users do
resources :orders
end
Trên đây có nghĩa là con đường xác thực sẽ "/accounts/sign_in"
, "/accounts_sign_up"
vv .. Một số có thể không biết rằng điều quan trọng là phải thừa nhận rằng devise_for :users
không thực sự là ánh xạ tới UsersController
và mô hình. Nó thậm chí không phải là một con đường tài nguyên, mặc dù nhiều người tin rằng nó trông giống như nó. Đó là lý do tại sao chúng ta không thể đối xử với nó như sau:
devise_for :users do
resources: somereosouce
end
Tất cả devise_for
làm là lập bản đồ các tuyến đường sau:
# 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"}
Vì vậy, khi nói rằng bạn có thể làm như sau nhưng sẽ có một số mâu thuẫn :
devise_for :users
resource :users do
resource :foo_object
end
Một chút về tài nguyên lồng nhau, nếu bạn có một cái gì đó như sau:
class Users < ActiveRecord::Base
has_many :foo_object
end
class FooObject < ActiveRecord::Base
belongs_to :users
end
Sau đó, tài nguyên lồng nhau của bạn sẽ là
resource :users do
resource :foo_object
end
Hy vọng rằng điều này sẽ xóa mọi thứ lên. Ngoài ra, bạn có thể muốn có một bản đọc của Nested Resource with Devise - Rails3
Cảm ơn bạn đã làm rõ câu hỏi 'devise_for'. Lời giải thích tốt nhất mà tôi đã đọc! – HM1
Đối với Q # 2, tôi biết đó là những gì tôi sẽ làm để làm tổ tài nguyên ... Tôi đoán câu hỏi là nhiều hơn là nó cần thiết hay tôi phải làm tổ tài nguyên cho các mô hình liên quan cho trường hợp này? Vì trong trường hợp này, tôi có thể sử dụng 'current_user' trong bộ điều khiển và xây dựng/cập nhật mô hình': foo_object'. Tôi tự hỏi nếu có những hậu quả để làm điều đó theo cách này. – HM1
@ HM1 nó không phải là chính xác cần thiết không, nhưng nếu bạn muốn xác định một số logic ứng dụng của bạn thì có. Đó là cách phân nhóm tài nguyên của bạn một cách rõ ràng hơn. Tuy nhiên trong trường hợp của bạn, bạn biết rằng 'user' được liên kết với' foo_object'. Vì vậy, khi bạn thực hiện một hành động với 'user', bạn cũng sẽ thực hiện một hành động' foo_object'. Vì vậy, URL của bạn sẽ có khả năng giống như một cái gì đó như thế này '/ người dùng/3/foo_object/4' Một lý do tại sao nó sẽ là cần thiết để có các tuyến đường lồng nhau là vì RESTful một trong những nguyên tắc quan trọng trong Rails. – David