【问题标题】:Rails 3 + Devise - How to get nested resources / routes to work?Rails 3 + Devise - 如何让嵌套资源/路线工作?
【发布时间】:2012-04-15 10:00:57
【问题描述】:

在我的 routes.rb 文件中,我仅有的条目是:

devise_for :users, :path => "accounts"

resources :users do
  resource :profile
end

但是当我运行“rake routes”时,我可以看到仍然有用户的映射资源,即新建、创建、编辑、更新等...这导致与一些设计路径发生冲突,例如 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

我怎样才能摆脱出现在此输出底部的额外用户资源?

【问题讨论】:

    标签: ruby-on-rails rest routing devise routes


    【解决方案1】:

    如果您只想索引和显示,请尝试:

    devise_for :users, :path => "accounts", :only => [:index, :show] do 
      resource :profile
    end
    

    【讨论】:

    • 问题是我只希望设计为用户创建路线,现在他们是一组路线,由设计创建,另一组由“资源:用户”创建。如何将设计路线和地图配置文件保留为嵌套资源?
    • @Jason 我对一组重复的路线有同样的问题。你找到解决办法了吗?
    • 现在添加一些自定义omniauth 控制器?我喜欢将关注点与用户资源分开的想法。
    【解决方案2】:

    最好的方法是使用 'devise_for:' 定义您的设计(非嵌套)路由,然后在单独的块中执行

    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
    

    【讨论】:

      猜你喜欢
      • 2011-05-23
      • 1970-01-01
      • 1970-01-01
      • 2012-03-07
      • 2012-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多