设计中嵌套资源 [英] nested resources in devise

查看:40
本文介绍了设计中嵌套资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Rails的新手,我希望获得一些有关我的路线和正确的路线逻辑的建议.我正在开发一个非常简单的应用程序,用户可以在其中发布列表.用户(设计模型)具有多个列表,并且该列表属于一个用户.我的列表表中有一个user_id:integer.当用户成功登录后,我希望他们在相应的路线页面上查看其列表.我这样创建了嵌套路由:

I am new to rails and I would like some advice as on my routes and the proper routing logic. I am working on a very simple app where users can post lists. The user (devise model) has_many lists and the list belongs_to a user. My list table has a user_id:integer in it. When the user successfully logins then I would like them to see their lists on their corresponding route page. I created the nested routes like so:

devise_for :users, :paths => 'users'

resource :users do
  resource :lists
end

这是我耙路的输出

new_user_session GET    /users/sign_in(.:format)       devise/sessions#new
            user_session POST   /users/sign_in(.:format)       devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)      devise/sessions#destroy
           user_password POST   /users/password(.:format)      devise/passwords#create
       new_user_password GET    /users/password/new(.:format)  devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format) devise/passwords#edit
                         PATCH  /users/password(.:format)      devise/passwords#update
                         PUT    /users/password(.:format)      devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)        devise/registrations#cancel
       user_registration POST   /users(.:format)               devise/registrations#create
   new_user_registration GET    /users/sign_up(.:format)       devise/registrations#new
  edit_user_registration GET    /users/edit(.:format)          devise/registrations#edit
                         PATCH  /users(.:format)               devise/registrations#update
                         PUT    /users(.:format)               devise/registrations#update
                         DELETE /users(.:format)               devise/registrations#destroy
             users_lists POST   /users/lists(.:format)         lists#create
         new_users_lists GET    /users/lists/new(.:format)     lists#new
        edit_users_lists GET    /users/lists/edit(.:format)    lists#edit
                         GET    /users/lists(.:format)         lists#show
                         PATCH  /users/lists(.:format)         lists#update
                         PUT    /users/lists(.:format)         lists#update
                         DELETE /users/lists(.:format)         lists#destroy
                   users POST   /users(.:format)               users#create
               new_users GET    /users/new(.:format)           users#new
              edit_users GET    /users/edit(.:format)          users#edit
                         GET    /users(.:format)               users#show
                         PATCH  /users(.:format)               users#update
                         PUT    /users(.:format)               users#update
                         DELETE /users(.:format)               users#destroy
                    root GET    /                              static_pages#home

如何让我的路线实现这一目标:

How can I have my routes achieve this:

/users/:user_id/lists(.:format)

这也是确保用户只能访问其列表的第一步.

It would also be a stepping point to make sure that the user has access only to his lists.

推荐答案

您可能想使列表资源多元化,并使用户资源单数:

You probably want to pluralize the list resource and make the user resource singular:

resource :user do
  resources :lists
end

对于第二部分,只需根据需要的当前用户加载列表

For part two, just load the lists based on the current user where you need them

@lists = current_user.lists

这篇关于设计中嵌套资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆