Rails 3 级深度嵌套资源 [英] Rails 3 level deep nested resources

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

问题描述

我知道许多 Rails 开发人员说将资源嵌套超过 2 层是错误的.我也同意,因为当您的网址看起来像 mysite.com/account/1/people/1/notes/1 时,它会变得混乱.我试图找到一种使用嵌套资源的方法,但不将它们嵌套 3 层深.

I know many rails developers say that nesting your resources more then 2 levels deep is wrongdoing. I also agree because it gets messy when your urls looks like mysite.com/account/1/people/1/notes/1. I am trying to find a way to use nested resources but without nesting them 3 levels deep.

这是错误的做法,因为 Rails 开发人员不推荐它,而且很难弄清楚如何将它嵌套在控制器或表单视图中.

This is the wrong way of doing it since rails developers do not recommend it and also it's very difficult figuring out how to nest this in the controller or form view.

resources :account do 
  resources :people do
    resources :notes
  end
end

Rails 开发人员说应该这样做的正确方式是这样

The correct way rails developer say this should be done is like so

resources :account do 
  resources :people
end

resources :people do
  resources :notes
end

这是我经常遇到的问题.每当我访问 account/1/people 时,我都可以将一个人添加到该帐户中,并且可以说该 url 就像 mysite.com/account/1/people/1 一样,而且效果很好.

Here's the problem that I always run into. When ever I visit account/1/people I can add a person to the account and lets say the url is like so mysite.com/account/1/people/1 and that works fine.

现在,如果我尝试从帐户 1 访问 mysite.com/people/1/notes,我会收到错误

Now if I try to access the mysite.com/people/1/notes from account 1 I get the error

没有帐户 ID 找不到人

Can't find people without and account id

如何才能使其正常工作?

How can get this to work properly?

推荐答案

你可以将路由嵌套到你喜欢的深度,因为 rails 3.x 允许你使用shallow: true 来扁平化它们

You nest the routes as deep as you like as rails 3.x allows you to flatten them using shallow: true

尝试尝试

resources :account, shallow: true do 
  resources :people do
    resources :notes
  end
end

使用 rake 路线看看你得到了什么 :)

Use rake routes to see what you get :)

根据评论更新

正如我所说,使用 rake 路由看看你能得到什么 url

As I said, play with rake routes to see what url's you can get

resources :account, shallow: true do 
  resources :people, shallow: true do
    resources :notes
  end
end

为您提供这些路线

:~/Development/rails/routing_test$ rake routes
      person_notes GET    /people/:person_id/notes(.:format)        notes#index
                   POST   /people/:person_id/notes(.:format)        notes#create
   new_person_note GET    /people/:person_id/notes/new(.:format)    notes#new
         edit_note GET    /notes/:id/edit(.:format)                 notes#edit
              note GET    /notes/:id(.:format)                      notes#show
                   PUT    /notes/:id(.:format)                      notes#update
                   DELETE /notes/:id(.:format)                      notes#destroy
    account_people GET    /account/:account_id/people(.:format)     people#index
                   POST   /account/:account_id/people(.:format)     people#create
new_account_person GET    /account/:account_id/people/new(.:format) people#new
       edit_person GET    /people/:id/edit(.:format)                people#edit
            person GET    /people/:id(.:format)                     people#show
                   PUT    /people/:id(.:format)                     people#update
                   DELETE /people/:id(.:format)                     people#destroy
     account_index GET    /account(.:format)                        account#index
                   POST   /account(.:format)                        account#create
       new_account GET    /account/new(.:format)                    account#new
      edit_account GET    /account/:id/edit(.:format)               account#edit
           account GET    /account/:id(.:format)                    account#show
                   PUT    /account/:id(.:format)                    account#update
                   DELETE /account/:id(.:format)                    account#destroy

正如所见,您可以访问您决定需要的任何级别的所有模型.剩下的取决于您在控制器操作中的设置.

As can be seen, you have access to all models at whatever level you decide you need. The rest is down to whatever you put in your controller actions.

您确实必须进行操作以确保在未传入 id 参数时采取适当的操作,因此如果您使用特定模型的 id,则检查该 id 是否在参数列表中,如果不采取替代行动.例如如果您不传入帐户 ID,请确保不要尝试使用它

You really have to work on the actions to make sure you take appropriate action when id params are not passed in, so if you make use of an id for a specific model then check that the id is in the params list and if not take alternative action. e.g. if you don't pass the account id in then make sure you don't try to use it

您的评论指出您已经使用了浅层路线,但这不是您在问题中发布的内容?

your comment states that you already use shallow routes, but that's not what you posted in your question?

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

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