本文介绍了Rails 3,浅层路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在rails 2.x中,我使用了浅层路线,但在rails 3中似乎没有使用(至少在API )。

In rails 2.x I used shallow routes, but this seems to be missing from rails 3 (at least in the API http://apidock.com/rails/ActionController/Resources/resources).

当我在Rails 3中传递此选项时,它不会抛出任何异常错误,但我也没有得到我期望的所有路线。

When I pass this option in rails 3 it doesn't throw any errors, but I'm also not getting all of the routes I expected.

轨道3条路线。rb

  resources :users, :shallow=>true do
    resources :recipe do
      resources :categories do
        resources :sections do
          resources :details do
          end
        end
      end
    end
  end

等效于rails 2.x生成的路线 missing 是(仅是配方资源的示例):

The routes missing that were generated with the rails 2.x equivalent are (just a sample for the recipe resource):

获取new_recipe(我只有new_user_recipe),

GET new_recipe (I only have new_user_recipe), and

POST配方(要创建新配方,我只有POST使用r_recipe)

POST recipe (to create a new recipe, I only have POST user_recipe)

从某种意义上讲,不会生成这些路由,但是我的旧代码通过以每种形式传递user_id来解决了该问题(不太优雅,已达成共识) )。

It kind of makes sense that these routes wouldn't be generated, but my old code worked around it by passing the user_id in each form (less elegant, agreed).

问题是:在rails 3中是否有浅路线的文档?有没有办法从rails 2.x生成我所缺少的路线?

Question is: Is there documentation for 'shallow' routes in rails 3? Is there a way to generate the routes I'm missing from rails 2.x?

谢谢,
Mike

Thanks,Mike

推荐答案

您需要将:shallow选项应用于嵌套资源。这应该会给您您想要的东西:

You need to apply the :shallow option to the nested resources. This should give you what you want:

  resources :users do
    resources :recipe, :shallow=>true do
      resources :categories do
        resources :sections do
          resources :details do
          end
        end
      end
    end
  end

这篇关于Rails 3,浅层路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 19:04
查看更多