双嵌套路由

时间:2009-08-05 00:26:28

标签: ruby-on-rails

目前我有步骤属于程序:

map.resources :procedures, :has_many => :steps

这对我来说很好,我得到的网址看起来像/ procedures / 3 / steps / 5。

但是,假设我想再添加一个图层,即属于步骤的图形,以获得此结果:/ procedures / 3 / steps / 5 / figures / 1

除了URL的怪物之外,我究竟会如何为此做路由?

编辑:也许我不应该把它放在一边,应该我这样做?数字只是图像的容器,我将在步骤中显示,所以它不像用户实际上“访问”任何图形,我只需要从图中获取图像。

3 个答案:

答案 0 :(得分:2)

map.resources :procedures do |procedure|
  procedure.resources :steps do |step|
    step.resources :figures
  end
end

如果您需要像/ figures /这样的路线,请使用:

map.resources :procedures, :shallow => true do |procedure|
  procedure.resources :steps do |step|
    step.resources :figures
  end
end

在您的观看中,它类似于:

<%= link_to "Figure", figure_url(@procedure, @step, @figure) -%>

答案 1 :(得分:1)

blog post解释得比我头脑更好。基本上,您可以在定义路线时使用:name_prefix option来实现您所寻找的目标。

答案 2 :(得分:1)

从您的评论中,我认为您是对的:您不需要路由到数字......而且您永远不会不必要地过度复杂化您的路线。

听起来你走的最远的将是你的步骤的Show动作......你只需要向ActiveRecord询问该步骤的数字列表。也就是说,你将拥有

class Step < ActiveRecord::Base
  has_many :figures
end

class Figure < ActiveRecord::Base
  belongs_to :step
  has_many :images #perhaps?
end

但是对于路由,您只需关心将被REST请求的资源 - 在这种情况下,它看起来就像您的procedures及其关联的steps。您在问题中列出的路线看起来像我一样!