Rails 3.1 has_one嵌套资源:路由不生成“所有”路径

时间:2011-08-11 16:40:19

标签: ruby-on-rails ruby ruby-on-rails-3 routing nested-resources

我有一个has_one关系:

# supplier.rb

  has_one :presentation
...

# presentation.rb

  belongs_to :supplier
...

以及它们的下列嵌套路线:

# routes.rb

resources :suppliers do
  resource :presentation
end

运行rake routes给出:

    supplier_presentation POST ... {:action=>"create", :controller=>"presentations"}
 new_supplier_presentation GET ... {:action=>"new", :controller=>"presentations"}
edit_supplier_presentation GET ... {:action=>"edit", :controller=>"presentations"}
                           GET ... {:action=>"show", :controller=>"presentations"}
                           PUT ... {:action=>"update", :controller=>"presentations"}
                        DELETE ... {:action=>"destroy", :controller=>"presentations"}

为什么show_helper没有show_helper?

我可以通过以下方式覆盖问题:

resources :suppliers do
  resource :presentation, :except => :show do
    get "" => "presentations#show", as: "presentation"
  end
end

给出路​​线:

presentation_supplier_presentation GET ... {:controller=>"presentations", :action=>"show"}

但我们现在都不是处理它的正确方法..

有什么建议?

-

(适用编辑)

supplier_presentation_path(@supplier)

确实有效,但为什么? ...在我的shell上执行rake routes时不会出现...

2 个答案:

答案 0 :(得分:3)

我真的不知道为什么当你执行rake routes时没有显示它,但是你是否尝试使用代码执行supplier_presentation_path(@supplier)?它应该根据你的路线工作。

答案 1 :(得分:0)

它永远不会对你有用。试试这个:

link_to "Presentation", [@suplier, @presentation]

link_to "Presentation", suplier_presentation_path(@suplier, @presentation)