Rails 3嵌套资源的简称? [英] Rails 3 nested resources short name?

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

问题描述

我正在将Rails 2.3应用程序升级到Rails3。在Rails 2.3路由器中,可以将嵌套的:name_prefix 设置为nil资源以获得较短的名称。实际的URL仍将完全合格,但是代码可以使用较短的名称。例如:

I'm in the process of upgrading a Rails 2.3 app to Rails 3. In the Rails 2.3 router, it was possible to set a :name_prefix of nil on nested resources to get a shorter name. The actual URL would still be fully qualified, but the code could use a shorter name. E.g.,:


 map.resources :sites do |site|
    site.resources :groups, :as => :groups, :controller => :url_groups, :name_prefix => nil, :member => { :clone => :post } do |group|
      group.resources :tests, :as => :tests, :controller => :test_runs, :name_prefix => nil, :collection => { :latest => :get }
    end
  end

允许一个人使用 latest_tests_path 。我不知道如何在Rails 3中执行相同的操作,因此我受困于 latest_site_group_tests_path 。如果那是需要的方式,那么我可以遍历代码并更改每个实例。但是我想确保我不会首先错过任何东西。不管是好是坏,我确实需要维护URL结构,所以似乎没有解决办法。

would allow one to use latest_tests_path. I can't figure out how to do the same thing with Rails 3, so I'm stuck with latest_site_group_tests_path. If that's the way it needs to be, I can just go through the code and change every instance of it. But I wanted to make sure I didn't miss anything first. And for better or worse, I do need to maintain the URL structure, so shallow routes don't seem to be the answer.

推荐答案

好消息是,Rails 3仍然可以设置任意/缩写的URL帮助程序。您可以使用routes.rb中的 match 声明创建快捷方式URL帮助器,而不是resources方法的参数。

Good news is that Rails 3 still has the ability to setup arbitrary/abbreviated url helpers. Rather than a parameter to the resources method, you can create short-hand url helpers with the match declaration in routes.rb.

说我们有这样的路由设置(请注意,您需要保持3个嵌套级别):

Say we have routes setup like this (noting that you need to maintain the 3 levels of nesting):

resources :sites do
  resources :groups, :controller => :url_groups do
    member do
      post :clone
    end
    resources :test_runs do
      collection do
        get :latest
      end
    end
  end
end

我们获得了所有标准的url帮助器(耙路):

We get all the standard url helpers (rake routes):

           clone_site_group POST   /sites/:site_id/groups/:id/clone(.:format)                    {:action=>"clone", :controller=>"url_groups"}
latest_site_group_test_runs GET    /sites/:site_id/groups/:group_id/test_runs/latest(.:format)   {:action=>"latest", :controller=>"test_runs"}
       site_group_test_runs GET    /sites/:site_id/groups/:group_id/test_runs(.:format)          {:action=>"index", :controller=>"test_runs"}
                           (etc)

但是要创建比 latest_site_group_test_runs_path(site,group)短的内容,请向route.rb添加一个匹配声明,如下所示:

But to create something shorter than latest_site_group_test_runs_path(site,group), add a match declaration to routes.rb like this:

match 'sites/:site_id/groups/:id/test_runs/latest' => 'test_runs#latest', :as => :latest_tests

现在您可以使用latest_tests_path(站点,组)或latest_tests_url(站点,组)来生成

Now you can use latest_tests_path(site,group) or latest_tests_url(site,group) to generate the fully nested path.

如果您的目标是简洁起见,则也可以使用隐式多态路径(只要您的所有模型都与资源路径对齐)。

If your aim is brevity, you could also use implicit polymorphic paths (as long as you have all your models aligned with resource paths).

例如,给定@site#1和@group#1,以下所有内容现在都会生成相同的路径'/ sites / 1 / groups / 1 / test_runs / latest'

For example, given @site #1 and @group #1, all of the following will now generate the same path '/sites/1/groups/1/test_runs/latest':

= link_to "latest tests", latest_site_group_test_runs_path(@site,@group) # std helper
= link_to "latest tests", latest_tests_path(@site,@group) # match helper
= link_to "latest tests", [:latest,@site,@group,:test_runs] # implicit polymorphic path

希望有帮助!似乎您应该能够获得应用程序迁移所需的灵活性。

Hope that helps! Seems like you should be able to get the flexibility you need for the app migration.

NB:我掩盖了一个名为测试的模型的潜在问题,因为这不是主题;-)由于名称空间和关键字冲突,有一些模型名称是无休止的痛苦之源。我的另一个最爱是当我真的想要一个称为案例的模式(因为它最适合问题域。不好的主意,很快就被扭转了!)

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

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