[英]Rails 3 + Devise - How to get nested resources / routes to work?
在我的routes.rb文件中,我僅有的條目是:
devise_for :users, :path => "accounts"
resources :users do
resource :profile
end
但是當我運行“ rake route”時,我可以看到仍然為用戶映射了資源,即新建,創建,編輯,更新等...這導致與某些設計路徑(例如new_user_registration_path)發生沖突
new_user_session GET /accounts/sign_in(.:format) devise/sessions#new
user_session POST /accounts/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /accounts/sign_out(.:format) devise/sessions#destroy
user_password POST /accounts/password(.:format) devise/passwords#create
new_user_password GET /accounts/password/new(.:format) devise/passwords#new
edit_user_password GET /accounts/password/edit(.:format) devise/passwords#edit
PUT /accounts/password(.:format) devise/passwords#update
cancel_user_registration GET /accounts/cancel(.:format) devise/registrations#cancel
user_registration POST /accounts(.:format) devise/registrations#create
new_user_registration GET /accounts/sign_up(.:format) devise/registrations#new
edit_user_registration GET /accounts/edit(.:format) devise/registrations#edit
PUT /accounts(.:format) devise/registrations#update
DELETE /accounts(.:format) devise/registrations#destroy
user_profile POST /users/:user_id/profile(.:format) profiles#create
new_user_profile GET /users/:user_id/profile/new(.:format) profiles#new
edit_user_profile GET /users/:user_id/profile/edit(.:format) profiles#edit
GET /users/:user_id/profile(.:format) profiles#show
PUT /users/:user_id/profile(.:format) profiles#update
DELETE /users/:user_id/profile(.:format) profiles#destroy
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
如何擺脫顯示在此輸出底部的其他用戶資源?
最好的方法是使用“ devise_for:”定義您的Devise(非嵌套)路由,然后在單獨的塊中執行
resources :users, :only => :none do
resource :profile
end
使用':except =>:all'可以阻止所有非嵌套Users
路由的定義並覆蓋您的Devise路由,但仍會創建您的所有users/3/profile
路由。 然后添加:path => "accounts"
替換users
所以你的代碼看起來像
devise_for :users, :path => "accounts"
resources :users , :path => "accounts", :only => :none do
resource :profile
end
如果您只想索引和顯示,請嘗試:
devise_for :users, :path => "accounts", :only => [:index, :show] do
resource :profile
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.